What would be the best code to have two bits of text in a single paragraph, one left aligned, the other right aligned, that also is:
Least amount of markup possible (you only need one span):
<p>This text is left. <span>This text is right.</span></p>
How you want to achieve the left/right styles is up to you, but I would recommend an external style on an ID or a class.
The full HTML:
<p class="split-para">This text is left. <span>This text is right.</span></p>
And the CSS:
.split-para { display:block;margin:10px;}
.split-para span { display:block;float:right;width:50%;margin-left:10px;}
I have used this in the past:
html
January<span class="right">2014</span>
Css
.right {
margin-left:100%;
}
Demonstration
Ok what you probably want will be provide to you by result of:
in CSS:
div { column-count: 2; }
in html:
<div> some text, bla bla bla </div>
In CSS you make div to split your paragraph on to column, you can make them 3, 4...
If you want to have many differend paragraf like that, then put id or class in your div:
I wouldn't put it in the same <p>
, since IMHO the two infos are semantically too different. If you must, I'd suggest this:
<p style="text-align:right">
<span style="float:left">I'll be on the left</span>
I'll be on the right
</p>
The only half-way proper way to do this is
<p>
<span style="float: right">Text on the right</span>
<span style="float: left">Text on the left</span>
</p>
however, this will get you into trouble if the text overflows. If you can, use div
s (block level elements) and give them a fixed width
.
A table (or a number of div
s with the according display: table / table-row / table-cell
properties) would in fact be the safest solution for this - it will be impossible to break, even if you have lots of difficult content.
If the texts has different sizes and they must be underlined this is the solution:
<table>
<tr>
<td class='left'>January</td>
<td class='right'>2014</td>
</tr>
</table>
css:
table{
width: 100%;
border-bottom: 2px solid black;
/*this is the size of the small text's baseline over part (≈25px*3/4)*/
line-height: 19.5px;
}
table td{
vertical-align: baseline;
}
.left{
font-family: Arial;
font-size: 40px;
text-align: left;
}
.right{
font-size: 25px;
text-align: right;
}
demo here