If you absolutely need to utilize a table
for this purpose, I recommend the following approach (assuming that your label td
elements are always the odd-numbered elements):
td:nth-child(odd) {
text-align: right;
}
td:nth-child(even) {
text-align: left;
}
Check out the JS Fiddle demo here.
However, I highly recommend transitioning to more semantic HTML, like using a definition list:
<dl>
<dt>Name</dt>
<dd>Bob</dd>
<dt>Age</dt>
<dd>20</dd>
</dl>
With the corresponding CSS:
dt, dd {
display: inline-block; /* enables side-by-side placement */
width: 5em;
margin: 0; /* eliminates default margins */
padding: 0.2em 0; /* enhances aesthetics and overrides defaults */
}
dt {
text-align: right; /* aligns the 'label' towards the right */
}
dd {
text-align: left;
text-indent: 0.5em; /* adds slight separation for visual appeal; adjust as needed */
}
View the JS Fiddle demo for reference.