94

This is a question I have been struggling with for a while. What is the proper way to mark up name/value pairs?

I'm fond of the <dl> element, but it presents a problem: There is no way to separate one pair from another - they have no unique container. Visually, the code lacks definition. Semantically, though, I think this is the correct markup.

<dl>
    <dt>Name</dt>
    <dd>Value</dd>
    <dt>Name</dt>
    <dd>Value</dd>
</dl>

In the above code, it is difficult to properly offset the pairs visually, both in code and rendered. If I wanted to, for instance, but a border around each pair, that would be a problem.

We may point to tables. It could be argued that name-value pairs are tabular data. That seems incorrect to me, but I see the argument. However, the HTML does not differentiate the name from the value, except in position, or with the addition of class names.

<table>
    <tr>
        <td>Name</td>
        <td>Value</td>
    </tr>
    <tr>
        <td>Name</td>
        <td>Value</td>
    </tr>
</table>

This makes much more sense from a visual standpoint, both in code and in CSS. Styling the aforementioned border is trivial. However, as mentioned above, the semantics are fuzzy at best.

Thoughts, comments, questions?

Edit/Update Perhaps this was something I should have explicitly mentioned in relation to structure, but a definition list also has the problem of not semantically grouping the pairs. The ordering and implicit border between a dd and a dt is easily understood, but they still feel slightly off to me.

Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
  • Re: your edit/update - if you wanted to ensure absolutely that there's no ambiguity about the semantic relationships between nodes you could always get really verbose and do
    Name
    Value 1
    Value 2
    but some would call that overkill. ;)
    – lucideer Jul 19 '10 at 16:49
  • You should also ask about the CSS. E.g. `
    ` would be not so bad, but it is hard to style to display the pairs inline (like in table)
    – takeshin Jul 19 '10 at 19:11
  • There have been many good answers, especially on the code side, but I'm still slightly hesitant about nearly all of them. This is only because every solution is a give-and-take. I will be marking one of the current answers as best for this thread soon, but know that it's a tough decision. – Ryan Kinal Jul 20 '10 at 19:11
  • @Danny Lin's was the one! Semantics and style with ease thanks to the improved spec! https://stackoverflow.com/a/50313363/792888 – EvilDr Oct 28 '19 at 14:27

14 Answers14

68

Thanks for this interesting question. There are few more things to consider here.

What is a pair? Two elements together. So we need a tag for this. Let's say it is pair tag.

 <pair></pair>

The pair contains the key, and the corresponding value:

 <pair><key>keyname</key><value>value</value></pair>

Then, we need to list the pairs:

<pairlist>
     <pair><key>keyname</key><value>value</value></pair>
     <pair><key>keyname</key><value>value</value></pair>
</pairlist>

The next thing to consider, is the display of the pairs. The usual layout is the tabular one:

key value
key value

and the optional separator, which is usually colon:

key : value
key : value

The colons can be easily added via CSS, but this certainly won't work in IE.

Case described above is the ideal one. But there is no valid HTML markup to fit in this easily.


To sum up:

dl is semantically closest, for simple cases of key and value, but is hard to apply visual styles (eg. to display the pairs inline or to add red border to just hovered pair). The case which fits most for dl is glossary. But this is not the case we discuss.

The only alternative I can see in this case is to use table, like this:

<table summary="This are the key and value pairs">
    <caption>Some notes about semantics</caption>
    <thead class="aural if not needed">
        <tr><th scope="col">keys</th><th scope="col">values</th></tr>
    </thead>
    <tbody class="group1">  
        <tr><th scope="row">key1</th><td>value1</td></tr>
        <tr><th scope="row">key2</th><td>value2</td></tr>
    </tbody>
    <tbody class="group2">
        <tr><th scope="row">key3</th><td>value3</td></tr>
        <tr><th scope="row">key4</th><td>value4</td></tr>
    </tbody>
</table>

One more:

<ul>
  <li><strong>key</strong> value</li>
  <li><strong>key</strong> value</li>
</ul>

or:

<ul>
  <li><b>key</b> value</li>
  <li><b>key</b> value</li>
</ul>

or, when the keys may be linked:

<ul>
  <li><a href="/key1">key1</a> value</li>
  <li><a href="/key2">key1</a> value</li>
</ul>

The key and value pairs are usually stored in database, and those usually store tabular data, so the table would fit best IMHO.

What do you think?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
takeshin
  • 49,108
  • 32
  • 120
  • 164
  • 4
    Very thorough answer. I like how you showed your line of thought. +1 – Ryan Kinal Jul 20 '10 at 19:09
  • and what about something like this: `
    • key
      value
    ` whit that we can have arbitrary keys and values (a value could be another list, an image, etc.)
    – Enrique Apr 20 '11 at 19:24
  • also, for the table solution maybe you could use the `` tag. Anyway, I think the table solution is not good, what happens if we can show the key and the value in different lines? – Enrique Apr 20 '11 at 22:47
  • The verbosity of the table solution suggests against it's use, but an unordered list is pretty good. – Kzqai Feb 20 '13 at 16:38
  • 2
    I believe (well, almost *know*) `` should be replaced by `` according to the HTML5 semantics. – Andreas Rejbrand Jul 31 '13 at 20:54
  • 1
    This question is tops in Google. Could you please consider updating your answer to reflect @Danny Lin's input regarding the use of `div`s within `dl`? This solves the problem of semantics and styling, thus avoiding the noise of `` and ``: https://stackoverflow.com/a/50313363/792888 – EvilDr Oct 28 '19 at 15:09
43

Following the specification (and further details) provided by Alexandr Antonov: use dl, dt, dd, and optionally div.

A combination of dl, dt, and dd is semantically fine for key-value pairs:

<dl>
    <dt>Key1</dt>
    <dd>Value1</dd>
    <dt>Key2</dt>
    <dd>Value2</dd>
</dl>

For easier styling or parsing, divs can be used as children of dl to group the key-value pairs (and makes dt and dd be grandchildren of dl):

dl { display: table; }
dl > div { display: table-row; }
dl > div > dt, dl > div > dd { display: table-cell; border: 1px solid black; padding: 0.25em; }
dl > div > dt { font-weight: bold; }
<dl>
  <div>
    <dt>Key1</dt>
    <dd>Value1</dd>
  </div>
  <div>
    <dt>Key2</dt>
    <dd>Value2</dd>
  </div>
</dl>
Danny Lin
  • 2,050
  • 1
  • 20
  • 34
  • 4
    Excellent answer, and IMHO should be accepted answer, especially given the example included in the spec that fits this question perfectly. CSS Grid would also be a rather amazing way to format this simply and effectively. – EvilDr Oct 28 '19 at 14:25
13

XHTML 2 introduces the ability to group terms and definitions using the di element

<!-- Do not us this code! -->
<dl>
    <di>
        <dt>Name</dt>
        <dd>John</dd>
    </di>
    <di>
        <dt>Age</dt>
        <dd>25</dd>
    </di>
</dl>

X/HTML 5 vs XHTML 2 > Enhancement To Definitions Lists

Unfortunately though, XHTML 2 is dead and HTML5 doesn't have di element

So, you can combine ul > li with dl > dt + dd :

<ul>    
    <li>
        <dl>
            <dt>Name</dt>
            <dd>John</dd>
        </dl>
    </li>
    <li>
        <dl>
            <dt>Age</dt>
            <dd>25</dd>
        </dl>
    </li>
</ul>
Manticore
  • 1,284
  • 16
  • 38
Yukulélé
  • 15,644
  • 10
  • 70
  • 94
6

I think a definition list is probably a bad idea. Semantically, they are used for definitions. Other key-value lists will often differ from definition titles and descriptions.

A table is one way to go, but what about an unordered list?

<ul>
    <li class="key-value-pair">
        <span class="key">foo</span>
        <span class="value">bar</span>
    </li>
</ul>
gpmcadam
  • 6,346
  • 2
  • 33
  • 37
  • 2
    Definition lists may be slightly poorly named in this regard, but they are not intended purely for definitions (they would be of fairly limited use case if they were) - the W3C spec. that defines them actually has an example of using them for marking up a dialogue. – lucideer Jul 19 '10 at 13:04
  • Thinking about it from an object-oriented standpoint, I suppose what I'm looking for is "an unordered list of name-value pairs", but I'm not sure this is the best answer (for now). +1 – Ryan Kinal Jul 19 '10 at 14:39
  • 1
    This is kind of microformat approach, but in default browser rendering or screen reader, you will get `foo bar`, like `foo bar` without any tags at all. One of the spans needs to be more than a span. Maybe ``? Then the second one is not needed. – takeshin Jul 19 '10 at 19:49
  • 3
    In the current [HTML5 spec](http://www.w3.org/TR/2011/WD-html-markup-20110405/dl.html#dl), dl has been rebranded as a description list which makes its use here seem more correct. – Eric Bock Apr 14 '11 at 13:39
6

dl {
  display: grid;
  grid-template-columns: auto auto;
}

dd {
  margin: 0
}
<dl>
  <dt>key</dt>
  <dd>value</dd>
  <dt>key</dt>
  <dd>value</dd>
</dl>

I used <dl> <dt> <dd> and styled them with grid

3

This is not my preferred solution, but it is a clever abuse of the semantic element:

Use a new <dl> per <dt>/<dd> pair:

<div class="terms">
    <dl><dt>Name 1</dt><dd>Value 1</dd></dl>
    <dl><dt>Name 2</dt><dd>Value 2</dd></dl>
</div>

An example with css floats and red border on hover:

dt:after { content:":"; }
dt, dd { float: left; }
dd { margin-left: 5px }
dl { float: left; margin-left: 20px; border: 1px dashed transparent; }
dl:hover { border-color: red; }
<div class="terms">
    <dl>
        <dt>Name 1</dt>
        <dd>Value 1</dd>
    </dl><!-- etc -->
    <dl><dt>Name 2</dt><dd>Value 2</dd></dl>
    <dl><dt>Name 3</dt><dd>Value 3</dd></dl>
    <dl><dt>Name 4</dt><dd>Value 4</dd></dl>
    <dl><dt>Name 5</dt><dd>Value 5</dd></dl>
    <dl><dt>Name 6</dt><dd>Value 6</dd></dl>
</div>
Iiridayn
  • 1,747
  • 21
  • 43
  • Interesting. This would would work if it's not needed to be a list of related items... though I suppose one COULD encapsulate each `
    ` in an `
  • `
  • – Armstrongest Aug 02 '16 at 17:20