Using CSS, how can I style the following:
<dl>
<dt>Mercury</dt>
<dd>Mercury (0.4 AU from the Sun) is the closest planet to the Sun and the smallest planet.</dd>
<dt>Venus</dt>
<dd>Venus (0.7 AU) is close in size to Earth, (0.815 Earth masses) and like Earth, has a thick silicate mantle around an iron core.</dd>
<dt>Earth</dt>
<dd>Earth (1 AU) is the largest and densest of the inner planets, the only one known to have current geological activity.</dd>
</dl>
so the content of the dt
show in one column and the content of the dd
in another column, with each dt
and the corresponding dd
on the same line? I.e. producing something that looks like:
CSS Grid layout
To change the column sizes, take a look at the
grid-template-columns
property.See jsFiddle demo
I needed a list exactly as described for a project that showed employees at a company, with their photo on the left, and information on the right. I managed to accomplish the clearing by using psuedo-elements after every
DD
:In addition, I wanted the text to only display to the right of the image, without wrapping under the floated image (pseudo-column effect). This can be accomplished by adding a
DIV
element with the CSSoverflow: hidden;
around the content of theDD
tag. You can omit this extraDIV
, but the content of theDD
tag will wrap under the floatedDT
.After playing with it a while, I was able to support multiple
DT
elements perDD
, but not multipleDD
elements perDT
. I tried adding another optional class to clear only after the lastDD
, but subsequentDD
elements wrapped under theDT
elements (not my desired effect… I wanted theDT
andDD
elements to form columns, and the extraDD
elements were too wide).By all rights, this should only work in IE8+, but due to a quirk in IE7 it works there as well.
this works to display them as table, with border, it should be responsive with 3em the width of the first column. The word-wrap just breaks any words wider than the column
Comparison of
<table>
with<dl>
:I recently needed to mix inline and non-inline dt/dd pairs, by specifying the class
dl-inline
on<dt>
elements that should be followed by inline<dd>
elements.I need to do this and have the
<dt>
content vertically centered, relative to the<dd>
content. I useddisplay: inline-block
, together withvertical-align: middle
See full example on Codepen here