How to put spaces between text in html? - html

How do you to put spaces between text in html? For example,
<p>a b c</p>
What do you put to have space between b and c?

Try using white-space with value pre.
pre Sequences of whitespace are preserved. Lines are only broken at newline characters in the source and at <br> elements.
p {
white-space: pre;
}
<p>a b c</p>
Use a monospaced font if you need each black space to take exactly one character width.
p {
white-space: pre;
font-family: monospace;
}
<p>a b c</p>

You can try to put between them. Like this:
<p>a b c</p>

There are several ways.
Pre Tag
One is to use the <pre> tag, which will render content with the whitespace intact.
Space Entities
Another is to add space entities, like (non-breaking space). Keep in mind with this approach, there are several space entities, and this particular entity does not word-wrap, which may not be the behavior you wanted. renders a normal space, which the browser should not collapse, and which will word-break as expected.
Here is a list of different Unicode spaces you can use.
CSS
Also, you usually should not use HTML entities to create space between text which is not part of one uniform sentence. This is because in this case, the better solution is to use margin or padding to separate the two, and keep each in its own element.
As another answer mentions, CSS letter-spacing is also an option if the spacing is consistent but larger than normal.
Here's an example of all these approaches (the CSS is not important here):
th, td {
border: 1px solid black;
padding: 3px;
}
th {
background-color: darkgrey;
color: white;
}
table {
border-collapse: collapse;
}
body {
font-family: sans-serif;
}
<table>
<thead>
<tr>
<th colspan="2">HTML Spacing</th>
</tr>
<tr>
<th>Method</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>Normal</td><td><p>a b c</p></td>
</tr>
<tr>
<td>Pre Tag</td><td><pre>a b c</pre></td>
</tr>
<tr>
<td>Margin Left</td><td><span>a </span><span>b</span><span style="margin-left: 3.5em;">c</span></td>
</tr>
<tr>
<td>Non-breaking Space</td><td><p>a b c</p></td>
</tr>
<tr>
<td>Em Space</td><td><p>a b       c</p></td>
</tr>
<tr>
<td>Letter-Spacing</td><td><span>a </span><span style="letter-spacing: 3.5em;">bc</span></td>
</tr>
</tbody>
</table>

Use . This will not breaks line too.

Use the CSS property word-spacing to set space between words. You can also use <span> and apply margin or padding.
So:
span { margin-left:1em }
<p>a b <span>c</span></p>

As others have said, you can add a space with :
But also add tabs with :   and  

Related

table cell, wrapped text, fit width to content, remove extra right hand white space

I have a set width table container, it will contain 3 text elements separated by single characters (>).
These text elements may contain text that cannot be fit in the container on a single line along with the rest, and must be wrapped.
The issue is when the text wraps, the cell will contain extra white space on the right hand side that then forces the other elements to wrap, where normally, without the white space, the succeeding elements would each fit on a single line.
Here is the desired behavior:
Where the first text element cannot fit on a single line and must wrap.
But any of the other text elements may also not fit on a single line and must wrap, leaving no extra white space.
Using a basic table layout:
<table class="table">
<tbody>
<tr>
<td>Membership Clubs and Organizations</td>
<td>></td>
<td>Books Wholesaler</td>
<td>></td>
<td>Music Management</td>
</tr>
</tbody>
</table>
.table {
width:450px;
border:1px solid black;
}
Here there is extra whitespace, causing the succeeding elements to also wrap.
After a lot of research, the closest i have come is by setting width:0.1% for the text elements.
Unfortunately this results in the separating characters having their own extra white space, which i have not been able to remove, i have been unable to reduce their width to fit their contents.
<table class="table">
<tbody>
<tr>
<td class="text">Membership Clubs and Organizations</td>
<td class="separator">></td>
<td class="text">Books Wholesaler</td>
<td class="separator">></td>
<td class="text">Music Management</td>
</tr>
</tbody>
</table>
.text {
width:0.1%;
}
.table {
width:450px;
border:1px solid black;
}
I settled on using tables because it got me closer to what i need, but i am open to use any format, the only requirement is that it be in pure css, and not use any javascript.
.text {
width:0.1%;
}
.table {
width:450px;
border:1px solid black;
}
<table class="table">
<tbody>
<tr>
<td class="text">Membership Clubs and Organizations</td>
<td class="separator">></td>
<td class="text">Books Wholesaler</td>
<td class="separator">></td>
<td class="text">Music Management</td>
</tr>
</tbody>
</table>
The width of the table is forcing the white-space to be there, no matter what. table-cells have extra space, so the words wrap when necessary, or the cells have no extra space, so the words wrap on every word.
I think the only option for zero whitespace is
td { word-break: break-all; }
.table {
width:450px;
border:1px solid black;
}
td {
word-break: break-all;
}
<table class="table">
<tbody>
<tr>
<td class="text">Membership Clubs and Organizations</td>
<td class="separator">></td>
<td class="text">Books Wholesaler</td>
<td class="separator">></td>
<td class="text">Music Management</td>
</tr>
</tbody>
</table>
just remove
.text {
width:0.1%;
}

Less margin-top/top

I'm developing a website for a company right now. I've done a table and now they say that the td must be also a link. The problem is that one td has three lines and the three lines must be the same link tag. I used a paragraph tag inside the link to change the line height, because they want the three lines with less line space, but it leaves a top space that must be removed. I've tried changing the margin-top, padding-top, td height... Nothing seems to work. If someone can help me I'll appreciate it.
Thank you.
Here's my code:
<table>
<tr>
<th>COMUNITAT</th>
<td>Bé comú</td>
<td>Glocalitat</td>
<td>Cercle</td>
<td>Lideratge</td>
<td>Comunicació</td>
<td>Famílies</td>
</tr>
<tr>
<th>EDUACIÓ</th>
<td>Aprenentatge</td>
<td>Qualitat</td>
<td>Idiomes</td>
<td>Personalització</td>
<td>Llenguatges</td>
<td>Creativitat</td>
</tr>
<tr>
<th>CARÀCTER</th>
<td>Identitat</td>
<td>Espiritualitat</td>
<td>Fet religiós</td>
<td>Equitat</td>
<td>Passió</td>
<td>Rol educador</td>
</tr>
<tr style='border:none !important; vertical-align: top;'>
<th>ENTORN</th>
<td>
<a href="#">
<p>Escola<br/>Universitat<br/>Empresa</p>
</a>
</td>
<td>Oberts</td>
<td>Tecnologia</td>
<td>Arquitectura</td>
<td>Oferta</td>
<td>Transformadors</td>
</tr>
</table>
And here's my actual CSS:
table a p {
font-size: 100%;
text-align: left;
line-height: 1;
}
Edit: This is how it looks currently.

How to combine JSP fragments into seamless table regardless of ordering

I have multiple JSPs that each contain one or more tables.
fragment1.jsp:
<table class="foo">
<tr>
<td>stuff</td> <td>stuff2</td>
</tr>
</table>
fragment2.jsp
<table class="foo">
<tr>
<td>more stuff</td> <td>more stuff2</td>
</tr>
</table>
<table class="bar">
<tr>
<td>whatever</td>
</tr>
</table>
They are used by wrappers in different configurations:
wrapper1.jsp
<s:include value="fragment1.jsp" />
<s:include value="fragment2.jsp" />
wrapper2.jsp
<s:include value="fragment2.jsp" />
If fragment2.jsp comes after fragment1.jsp, I want these two tables to have no margin between them and appear as one table. However, if either one is alone, I want them to be formatted normally as "foo" tables are styled.
Is there a way to indicate this styling preference in some way such that the two "foo" tables will appear as a single table when adjacent, but otherwise style themselves normally?
I am somewhat new to Struts/JSP and dealing with some kludged legacy code, so please help me understand if this problem would be better solved through another approach.
It is actually possible with CSS only, without JavaScript and without changing the HTML.
A full screen demo is worth a thousand words...
Basically, you need to use border-collapse: collapse; and specify the following settings:
When the table is alone, all the borders and margins normally set:
table {
border-collapse : collapse;
border : 4px solid black;
margin-top : 20px;
margin-bottom : 20px;
}
When the table is adjacent to one or more other tables:
If it's not the first, remove margin-top and border-top:
table + table {
margin-top : 0;
border-top : none;
}
If it's not the last: remove margin-bottom and border-bottom:
table:not(:last-child) {
border-bottom : none;
margin-bottom : 0;
}
Also ensure to avoid setting a border on the last <tr> of each table:
tr:not(:last-child) {
border: 1px solid silver;
}
It's better to use javascript and jquery for doing such works. So I think this can be helpful:
$(document).ready(function(){
var secondTable = $(".bar").html();
$(".foo").append(secondTable);
});
Note, you need to include jquery library if you have not included it already.
There's no need to use tables if you don't use tabular data. You can always use div or span tags.
<div class="foo">
<span>more stuff</span> <span>more stuff2</span>
</div>
<div class="bar">
<span>whatever</span>
</div>
The CSS selector table.foo + table.foo will select every element of table.foo that appears after another element of the same type.
You can use it to remove the margin from all following table.foo elements:
table.foo + table.foo {
margin-top: 0px;
margin-bottom: 0px;
}
If it should apply to the second element only, use table.foo:nth-child(2) instead.

Apply a space character before a table in HTML

I have a table with two rows and one column which is surrounded by P tag.
After P I have inserted some " " for applying some space to the table from the start of the line, but these spaces don't get applied to the table and the table was shown from the start of the line.
table {float: left; background: green;}
<p>
<strong>
<span style="font-family:b titr">
<table>
<tr>
<td>
<textarea id='MainSign' name='MainSign' style='background-color:transparent; width: 150px; height: 150px; border: none'></textarea>
</td>
</tr>
<tr>
<td>
<br />شیطون<br />مدیریت کل
</td>
</tr>
</table>
</span>
</strong>
</p>
The <table> tag creates a newline when it is used. So all your spaces are created above the table. The best way to make it possible is use inline CSS code:
<table style="margin-left: 2cm;">
There isn't any need of making the table within a <p> tag.
I guess that you want to move your table away from the left border. You can do this easily with CSS, without adding .
Check this fiddle Fiddle.
I changed the background color to red for easy to understand. Then I gave a id for the table. As id='MainSign' with CSS and then I changed the margin-left:
#mainTable{
margin-left: 100px;
}
You can use any value instead of "100px;".
Table can't be inside a paragraph (paragraph is closed when table begins).
Use float to indent table by spaces in a paragraph. And you can remove span and strong too; they are empty and closed when table begins.
p {float: left; background: red; margin: 0;}
table {float: left; background: green;}
http://jsfiddle.net/5439pj0p/
Note:
HTML, which is rendered by the browser from your code, is:
<p>
<strong>
<span></span>
</strong>
</p>
<table>...</table>
<strong></strong>
<p></p>
Here is a fiddle where you can see that there are really two strong elements (yellow, size of 10x10 pixels).
http://jsfiddle.net/5439pj0p/1/

W3C Validator tells me I have an Error, not sure how to fix

So I am writing a css page for my class, and I noticed that the style which I applied to span is not applying at all, so I went to http://validator.w3.org/ to check what I did wrong and it gave me this error message
"Line 32, Column 6: Start tag span seen in table."
This is my line 32
<span><tr><td>Mars needs moms</td><td>$150,000,000</td><td>$38,992,758</td><td>$130,503,621</td><td>2011</td></tr></span>
Here is the code for that particular style
span{background-color=:#666;font-weight:bold;color:white;}
Basically my goal is to make this table haveevery other row in the table with a background color being black with the text being white
This is the full code, incase the error made which isn't applying this style is somewhere else. there are other styles in there which don't apply to anything yet, as this is not finished yet
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Lowest Grossing Movies of all time</title>
<style>
span{background-color=:#666;font-weight:bold;color:white;}
p{text-decoration:underline;line-height:200%;}
h1{text-align:center;font-size:125%;}
table{border-collapse:collapse;}
th,td{padding:25px;}
</style>
</head>
<body>
<h1> Lowest Grossing Movies of All Time </h1>
<table border="1">
<tr><th>Title</th><th>Production Budget</th><th>World Wide Gross</th><th>Net Loss</th> <th>Release Year</th></tr>
<span><tr><td>Mars needs moms</td><td>$150,000,000</td><td>$38,992,758</td> <td>$130,503,621</td><td>2011</td></tr></span>
<tr><td>The 13th Warrior</td><td>$160,000,000</td><td>$61,698,899</td><td>$129,150,551</td><td>1999</td></tr>
<span><tr><td>The Lone Ranger</td><td>$225,000,000</td><td>$243,377,083</td><td>$103,311,459</td><td>2013</td></tr></span>
<tr><td>R.I.P.D.</td><td>$130,000,000</td><td>$66,627,120</td><td>$96,6865,440</td><td>2013</tr>
<span><tr><td>John Carter</td><td>$250,00,00</td><td>$282,778,100</td><td>$108,610,950</td><td>2012</td></tr></span>
</table>
</body>
</html>
The biggest problem you're facing is that there are limited elements that are valid children of the HTML table element, these are:
colgroup,
caption,
thead,
tfoot,
tbody, and
tr
So removing the span elements from the table solves that problem. Also, you'd forgotten to close one of the td elements (you closed the tr, but forgot the td); this is why readable HTML is easier to maintain (it's simply easier to see the code, and omissions, when it's indented and white-spaced).
Incidentally, using your original HTML, had you used your browser's developer tools (such as Web Inspector under Chromium, or Firebug under Mozilla), you'd have been able to inspect the DOM, which would've shown you the brower's (unpredictable and unreliable) reordering of the HTML in order to produce a valid document). For example, Web Inspector shows:
JS Fiddle 'source' for above image.
Note the three span elements moved before the table element, from the table itself.
Your corrected HTML:
<h1> Lowest Grossing Movies of All Time </h1>
<table>
<tr>
<th>Title</th>
<th>Production Budget</th>
<th>World Wide Gross</th>
<th>Net Loss</th>
<th>Release Year</th>
</tr>
<tr>
<td>Mars needs moms</td>
<td>$150,000,000</td>
<td>$38,992,758</td>
<td>$130,503,621</td>
<td>2011</td>
</tr>
<tr>
<td>The 13th Warrior</td>
<td>$160,000,000</td>
<td>$61,698,899</td>
<td>$129,150,551</td>
<td>1999</td>
</tr>
<tr>
<td>The Lone Ranger</td>
<td>$225,000,000</td>
<td>$243,377,083</td>
<td>$103,311,459</td>
<td>2013</td>
</tr>
<tr>
<td>R.I.P.D.</td>
<td>$130,000,000</td>
<td>$66,627,120</td>
<td>$96,6865,440</td>
<td>2013</td> <!-- you omitted a closing </td> tag here -->
</tr>
<tr>
<td>John Carter</td>
<td>$250,00,00</td>
<td>$282,778,100</td>
<td>$108,610,950</td>
<td>2012</td>
</tr>
</table>
Using CSS:
table {
border-collapse: collapse;
}
th,
td {
border: 1px solid #000;
}
/* using ':nth-child(odd)' to style the 'td' elements
of the alternate/odd rows of the table */
tbody tr:nth-child(odd) td {
background-color: #ffa;
}
JS Fiddle demo.
References:
<table>.
:nth-child().
In most modern browsers this could be achieved with css:
tr:nth-child(even) {
background-color:#666;
font-weight:bold;
color:white;
}
No span tags required. (Remove them)
JS Fiddle: http://jsfiddle.net/uB2GR/
You've placed <span> elements between two <tr> elements. This is not valid HTML. You need to place your entire <span> inside a table cell.
<tr><td><span>Some stuff</span></td><td><span>More stuff</span></td></tr>
if you're doing this for styling purposes there's probably a better way with classes applied to the <td> elements