Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a definition list with equalized definition terms of variable length?

Tags:

html

css

flexbox

I am trying to create a definition list (with the help of this guide) with its dt and dd elements having different content. Ofcourse i could hardcode a width for these elements, but i'd like to have the list takes the longest dt element and make its siblings to have its length. Also, when the content of the dd element is longer than the available space the new line must not begin below the dt element but rather where the dd element begins like on the second screenshot in this question.

I thought i could solve this with the display: flex attribute but stuck with a non-working solution. Is it not possible or am a on the wrong way?

Here is what i've got so far (fiddle):

HTML:

<dl>
   <dt>dt: Lorem Ipsum</dt>
   <dd>dd: Lorem imperdiet</dd>
   <dt>dt: Lorem id libero in Ipsum and so on and so on</dt>
   <dd>dd: Lorem id libero in ipsum dolor</dd>
   <dt>dt: Lorem Ipsum</dt>
   <dd>dd: I should be sitting to the right of the previous dt and not wrapping round below it.</dd>
   <dt>dt: Lorem id libero in Ipsum</dt>
   <dd>dd: Lorem in ipsum dolor</dd>
</dl>

CSS:

dl {
   display: flex;
   flex-flow: column nowrap;
}
dt {
   background: gold;
   display: flex;
   flex-flow: row wrap;
}
dd {
   background: yellowgreen;
   display: flex;
   flex-flow: row wrap;
   width: auto;
   margin: 0;
   padding: 0;
}
like image 541
user1014412 Avatar asked Feb 10 '15 12:02

user1014412


1 Answers

I don't think that it is possible to archive what you are trying. Flexbox won't help at this case. You also cannot use display table and so on, because the dl syntax unfortunately lacks wrapping of dt+dd groups. You would have to use JS to calc widths of the dt elements, but it wouldn't be also so easy because of multiline variants.

But how about this a little bit modified "traditional" approach: make them min-width but allow the dt to overflow to the dd.

dl:after {
  content: '';
  display: block;
  clear: both;
}
dt {
  float: left;
  clear: left;
  padding-top: 1em;
  margin-right: 1em;
}
dd {
  padding-top: 1em;
  margin-left: 15em;
}

http://codepen.io/ThiemelJiri/pen/KrZrxG

like image 199
actimel Avatar answered Nov 15 '22 07:11

actimel