HTML: Best tag for 'labels' outside of forms

Html

Html Problem Overview


What HTML tags would you use in for this kind of information:


Name: John
Age: 40
City: Frankfurt
Country: Germany
Status: Active

I like to use the <label> tag within p tags here, which sounds like good semantics. However, this information is NOT inside a form. And the W3C says:

label: The label element represents a caption for a form control.

So what should I use then? I can clearly use <strong>, but this has no 'strong importance' and does not feel correct.

UPDATE

After noticing the DEFINITION LIST suggestion, I'd like to ask if this would still be best if I have several pieces of this information on the same page? It would mean I define "name" with a different "definition" several times. Eg:


Name: John
Age: 40
City: Frankfurt
Country: Germany
Status: Active

Name: Maria
Age: 30
City: Bonn
Country: Germany
Status: Inactive


Is <dl> still the best way to go?

Html Solutions


Solution 1 - Html

A definition list could be suitable here:

<dl>
	<dt>Name</dt>
	<dd>John</dd>
	<dt>Age</dt>
	<dd>40</dd>
	<dt>City</dt>
	<dd>Frankfurt</dd>
	<dt>Country</dt>
	<dd>Germany</dd>
	<dt>Status</dt>
	<dd>Active</dd>
</dl>
 

> http://www.w3.org/TR/html401/struct/lists.html#h-10.3 - Definition lists, created using the DL element, generally consist of a series of term/definition pairs (although definition lists may have other applications).

However, just because you want bold formatting doesn't mean you are restricted to <strong> or <b>. You can use the non-semantic <span> and simply apply the formatting with CSS.

In addition, you can use CSS to add the colons, rather than putting them in your markup:

dt:after { content:":"; }

Perhaps to help clear up some confusion about it's proper usage, it looks like HTML5 will be referring to this tag as a description list.

> http://www.w3.org/TR/html5/grouping-content.html#the-dl-element - The dl element represents an association list consisting of zero or more name-value groups (a description list).

Solution 2 - Html

This is the right place for a DL structure:

<dl>
   <dt>Name</dt>
   <dd>John</dd>

   <dt>Age</dt>
   <dd>40</dd>

   <dt>City</dt>
   <dd>Frankfurt</dd>

   <dt>Country</dt>
   <dd>Germany</dd>

   <dt>Status</dt>
   <dd>Active</dd>
</dl>

Note that you'll need to add styling in order to make it look like that, but this is the right logical structure.

Solution 3 - Html

Maybe you can use a definition list (dl, dt, dd), but that doesn't sound correct either.

As W3C says that it represents a caption for a form control, the text next to the label isn't a control, and so the for attribute becomes pointless.

I would just go with strong, also because it's the best representation without CSS.


Edit:

Since this about people, you can also use the vCard equivalent for HTML: hCard. Example:

<div class="vcard">
    <div class="fn">
        <strong class="type">Name</strong>:
        <span class="value">John</span>
    </div>
    <div class="note">
        <strong class="type">Age</strong>:
        <span class="value">40</span>
    </div>
    <div class="adr">
        <div class="locality">
            <strong class="type">City</strong>:
            <span class="value">Frankfurt</span>
        </div>
        <div class="country-name">
            <strong class="type">Country</strong>:
            <span class="value">Germany</span>
        </div>
    </div>
    <div class="note">
        <strong class="type">Status</strong>:
        <span class="value">Active</span>
    </div>
</div>

You can then use as many of these in an <ul> or <ol>. Some browsers will recognize these properties and turn them into links for use with an application. For example the adr properties could link to a mapping tool.

This is just a standard, it's not meant to be semantically correct. As far as I know you can use whatever markup (HTML tags) you like within these hCards. See link for the details.

Solution 4 - Html

I agree with the other answers that a <dl> list is the most semantic option here. In the past I was never sure if this was a correct use of <dl> because all the official descriptions of the <dl> element I saw described it as a "definition list" to be used for actually defining terms as in a glossary (see my comments on @Cristian's answer). However, after looking at the latest editor's draft of the HTML 5 spec, I noticed that it has been renamed to "description list" rather than "definition list", and the example given is actually very similar to the example in the question here.

<dfn>, on the other hand, seems to be intended only for actually defining terms. Here are some relevant quotes from the above link:

> The dt element itself, when used in a dl element, does not indicate > that its contents are a term being defined, but this can be indicated > using the dfn element.

...

> The dfn element represents the defining instance of a term. The paragraph, description list group, or section that is the nearest ancestor of the dfn element must also contain the definition(s) for the term given by the dfn element.

So in conclusion I would second the recommendation for the use of the <dl> element, even though its default styling is different from the OP's example.

Here's a reusable CSS class for a <dl> in the style of the example:

.dl-inline dt, .dl-inline dd {display:inline; margin:0;}
.dl-inline dt:after {content:': '}
.dl-inline dd + dt:before {content:''; display:block;}

<dl class="dl-inline">
	<dt>Name</dt>
	<dd>John</dd>
	<dt>Age</dt>
	<dd>40</dd>
	<!-- ... -->
</dl>

Or the SASS version:

.dl-inline {
	dt,dd {display:inline; margin:0;}
	dt:after {content:': '}
	dd + dt:before {content:''; display:block;}
}

Solution 5 - Html

I would argue for using the <dfn> tag.

It's short, semantic and can be easily styled.

> The <dfn> element marks the term being defined; the definition > of the term should be given by the surrounding <p>, > <section> or definition list group (usually a <dt>, > <dd> pair).

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dfn

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjoh-3View Question on Stackoverflow
Solution 1 - HtmlWesley MurchView Answer on Stackoverflow
Solution 2 - HtmlJohn GreenView Answer on Stackoverflow
Solution 3 - HtmlMidasView Answer on Stackoverflow
Solution 4 - HtmlMatt BrowneView Answer on Stackoverflow
Solution 5 - HtmlCristianView Answer on Stackoverflow