Need help aligning a text
开发者_运维技巧Age: 23
Location: Bronx,NY
Nationality: Puerto Rican
Ocupation: Tailor
You can see a live example Here
I think I'd use a dl
(Definition list) here as you are defining a term - dt
, then giving 'description' - dd
, on each term.. the good thing is you get the two parts you need to align however you'd like ;)
Definition Lists:
Definition lists vary only slightly from other types of lists in that list items consist of two parts: a term and a description. The term is given by the DT element and is restricted to inline content. The description is given with a DD element that contains block-level content.
then you can float the dt
's
CSS:
dl {border: 1px solid #000;}
dt {float: left; width: 180px; text-align: right; margin-right: 20px;}
HTML:
<div id="personal">
<dl>
<dt>Age:</dt> <dd>23</dd>
<dt>Location:</dt> <dd>Bronx,NY</dd>
<dt>Nationality:</dt><dd>Puerto Rican</dd>
<dt>Ocupation:</dt><dd>Tailor</dd>
</dl>
</div>
Example: HERE
I updated it for you here: http://jsfiddle.net/MUFcb/11/
Added <span>
with CSS to give the alignment you were looking for in your example.
Here's the code:
HTML
<div id="personal">
<ul>
<li><span class="label">Age:</span> 23</li>
<li><span class="label">Location:</span> Bronx,NY</li>
<li><span class="label">Nationality:</span> Puerto Rican</li>
<li><span class="label">Ocupation:</span> Tailor</li>
</ul>
</div>
CSS
#personal ul li span.label{
display: block;
float: left;
width: 75px;
text-align: right;
padding-right: 10px;
}
You would probably want to use a table for that sort of data, not an unordered list...
<table>
<tr>
<td>Age:</td>
<td>23</td>
</tr>
...
...
</table>
You probably want to use a table here.
<table>
<tr>
<td>Age:</td>
<td>23</td>
</tr>
<tr>
<td>Location:</td>
<td>Bronx,NY</td>
</tr>
<tr>
<td>Nationality:</td>
<td>Puerto Rican</td>
</tr>
<tr>
<td>Ocupation:</td>
<td>Tailor</td>
</tr>
</table>
what about this one ? if you want the spaces less then just change the padding value http://jsfiddle.net/MUFcb/8/
If you dont want to use a table you can also use divs that have a style of float:left
<div style="float:left;">
Age: <br />
Location: <br />
Nationality: <br />
Ocupation: <br />
</div>
<div style="float:left;">
23<br/>
Bronx,NY<br/>
Puerto Rican<br/>
Tailor<br/>
</div>
If you want the age to be right-aligned, you can add text-align:right to that div.
精彩评论