I have this html:
<dl>
<dt><label for='txaText'>Text</label></dt>
<dd><textarea id='txaText'></textarea></dd>
</dl>
and this css:
dt {
float: left;
text-align: right;
width: 30%;
padding-right:5px;
}
dd {
width: 70%;
margin: 0;
}
But I get this:
And I wanted this:
How can I achieve this vertical alignment of the dt tag in relation to its respective dd tag? Can I do this without horrible hacks like creating divs or specifying the height in pixels for each label?
First we have the dl (definition list) tag to hold the whole set of data, next we have dt (defines the item in the list) tag and dd (describes the item in the list) tag to hold the title and the data. Over at CSS, we will need to float the dt tag, so that the title for the list data will align to the left.
Now, let’s look at using HTML dl, dt, dd tags for listing the data. First we have the dl (definition list) tag to hold the whole set of data, next we have dt (defines the item in the list) tag and dd (describes the item in the list) tag to hold the title and the data.
There is also one more method to align elements vertically. You can use the vertical-align property, which commonly applies to inline, inline-block, and table-cell elements. But it cannot be used to align block-level elements vertically. Earlier, it was used with the valign attribute, but now this attribute is deprecated.
Over at CSS, we will need to float the dt tag, so that the title for the list data will align to the left. The rest of the styling is up to you.
I think I came up with a solution that you may like, you can set your elements to display:table-cell
and vertical-align:middle
to align them.
CSS:
dl{
border: 1px solid red;
}
dt {
display:table-cell;
vertical-align:middle;
width: 30%;
padding-right:5px;
}
dd {
display:table-cell;
vertical-align:middle;
width: 70%;
margin: 0;
}
Updated CODEPEN DEMO for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With