Definition list with inline pairs
Asked Answered
S

8

27

I'm trying to create a definition list of term-definition pairs, each pair existing on a single, separate line. I've tried making dts and dds display:inline, but then I lose the line breaks between the pairs. How do I make sure I have a line for each pair (and not for each individual term/definition)?

Example:

<dl>
<dt>Term 1</dt><dd>Def 1</dd>
<dt>Term 2</dt><dd>Def 2</dd>
</dl>

yielding:

Term 1 Def 1
Term 2 Def 2

The CSS for making them inline would be:

dt,dd{display:inline;}

yielding:

Term 1 Def 1 Term 2 Def 2

...which is not what I want (line breaks between pairs missing).

Spermary answered 19/12, 2013 at 21:53 Comment(5)
Please provide a complete, working code example, as per the question requirements in the FAQ.Saba
Does it need to be more complete than the one I added in the edit?Spermary
Please include the related CSS.Saba
You just want more space between pairs?Saba
(I didn't vote to close)Saba
S
35

Try this:

dt, dd { float: left }
dt { clear:both }

Add margin-bottom to dt dd if you'd like more space between them..

Saba answered 19/12, 2013 at 22:6 Comment(3)
This broke other things for me like p that followed dl ran over dl.Celeski
@Celeski To avoid that you also need dl { overflow: hidden; }Cottontail
Adding dl { overflow: hidden; } did not helpMotorist
E
39

Another solution:

dt:before {
  content: "";
  display: block;
}
dt, dd {
  display: inline;
}
Exemplificative answered 19/4, 2014 at 6:53 Comment(4)
This works in new browsers, but not in stock android 4.3 or beforeDowd
This is much better than float based solutions, because it maintains the vertical height of the <dl> element.Unique
This solution is just what I wanted as it works well with text-align: center too. My only minor issue is that copying and pasting the text from the HTML results in one long line, because pseudo-elements are not selected.Demars
I used dd:after {...} Works a charm! ThanksAbrahamsen
S
35

Try this:

dt, dd { float: left }
dt { clear:both }

Add margin-bottom to dt dd if you'd like more space between them..

Saba answered 19/12, 2013 at 22:6 Comment(3)
This broke other things for me like p that followed dl ran over dl.Celeski
@Celeski To avoid that you also need dl { overflow: hidden; }Cottontail
Adding dl { overflow: hidden; } did not helpMotorist
T
6

I tried these answers, finally I end up with this.

Which I simplified to:

  dl {    
    padding: 0.5em;
  }
  dt {
    float: left;
    clear: left;
    width: 100px;
    text-align: right;
    font-weight: bold;    
  }
  dt:after {
    content: ":";
  }
  dd {
    margin: 0 0 0 110px;        
  }
Tecu answered 7/8, 2015 at 15:21 Comment(0)
G
2

Another solution is to use inline-block and percentage widths. As long as the combined width of dd + dt is greater than 50%.

dt, dd {
  display: inline-block;
}

dt {
  width: 50%;
}

dd {
  min-width: 5%;
}

I found this gave me a more consistent positioning of the dd elements.

Graz answered 19/8, 2014 at 14:48 Comment(1)
Actually, the combined width of dt + dd + dt needs to be more than 100% for this to work. I initially tried with a dt width of 25% and a dd min-width of 26%, but it didn't have the desired effect.Cluny
W
2

Another simple solution

dt {
    display: block;
    float: left;
    min-width: 100px;
}

https://jsbin.com/vumeyowana/edit?html,css,output

Waadt answered 20/9, 2016 at 15:0 Comment(2)
Best answer, IMHO. I would prefer right-padding: 1em; over 'min-width`Internode
this works only for descriptions that fit in one line.Dishrag
D
2

Andrey Fedoseev's answer does not work in old android (stock browser 4.3).

This does work for that browser and all others I have checked:

dt::before {
  content: "\A";
  white-space: pre-wrap;
  display: block;
  height: .5em;
}

dt, dd {
  display: inline;
  margin: 0;
}
<dl>
  <dt>AM</dt>
  <dd>Description for am that should span more than one line at narrow screen widths.</dd>
  <dt>PM</dt>
  <dd>this means afternoon.</dd>
</dl>
Dowd answered 14/12, 2016 at 15:47 Comment(0)
W
0

If you have a "zebra" background or border separating each DT-DD pair, then this will work nicely:

dt, dd {
    padding: 8px 6px;
    display: block;
}
dt {
    font-weight: 600;
    float: left;
    min-width: 50%;
    margin: 0 8px 0 0;
}
dd:nth-of-type(odd) {
    background: #eee;
}

This will appear to wrap both items in a row with a grey background. As long as the DT has less vertical height than the DD - which is usually the case.

(Haven't browser tested yet.)

Wilmawilmar answered 15/1, 2015 at 17:12 Comment(0)
D
0

I wanted to be able to create a list with inline pairs, centrally aligned. Using dt:before { display; block} worked well for this, but when copying text from a browser, pseudo elements are ignored so would end up being pasted like this:

Term 1: Def 1Term 2: Def 2

This is a minor issue, but there's another approach documented at MDN:

WHATWG HTML allows wrapping each name-value group in a <dl> element in a <div> element. This can be useful when using microdata, or when global attributes apply to a whole group, or for styling purposes.

The following solution works well for this:

dt, dd {
  display: inline;
}

<dl>
  <div><dt>Term 1:</dt> <dd>Def 1</dd></div>
  <div><dt>Term 2:</dt> <dd>Def 2</dd></div>
</dl>
Demars answered 26/12, 2020 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.