I have an HTML document where I would like to semantically group text to the head of a UL as a "header." The initial attempt looks like this:
<ul id="databases">
Databases:
<li>Microsoft SQL Server - 10+ years</li>
<li>Sybase SQL Server - 5 years</li>
<li>Oracle - 5 years</li>
</ul>
The W3C validator points out that there's no text allowed inside a UL, but outside a LI. I could put the text inside an LI, then use the pseudo-class :first-child to find the "header" in my CSS, but this is clearly not the semantically correct way.
How do I handle this properly?
Yes, it's wrong. It's not valid HTML. Github has h4s within uls in their footer.
Use the <ul> tag together with the <li> tag to create unordered lists. Tip: Use CSS to style lists. Tip: For ordered lists, use the <ol> tag.
<h1> headings should be used for main headings, followed by <h2> headings, then the less important <h3> , and so on. Note: Use HTML headings for headings only. Don't use headings to make text BIG or bold.
<ul>: The Unordered List element. The <ul> HTML element represents an unordered list of items, typically rendered as a bulleted list.
It should not be set in the first li because this would assume a sibling relationship to the preceding li elements whereas the header is more important in the hierarchy. Imagine screen-readers etc
<h2>Databases:</h2>
<ul id="databases">
<li>Microsoft SQL Server - 10+ years</li>
<li>Sybase SQL Server - 5 years</li>
<li>Oracle - 5 years</li>
</ul>
Swap out the h2 for a h(n) depending on the hierarchy in relation to the other headers on the page. To target the header in css just give it a class if there are other headers that will share the same style e.g.
<h2 class="subHeader">Languages:</h2>
<ul id="languages">
<li>English</li>
<li>Chinese</li>
<li>French</li>
</ul>
Otherwise give it an id
You could try the "Definition List" tag for your listing purposes. You get a much cleaner code.
<dl>
<dt>Databases</dt>
<dd>Microsoft SQL Server - 10+ years</dd>
<dd>Sybase SQL Server - 5 years</dd>
<dd>Oracle - 5 years</dd>
<dt>Second heading</dt>
<dd>Item 1</dd>
<dd>Item 2</dd>
<dd>Item 3</dd>
</dl>
More information on Definition List here http://www.w3schools.com/tags/tag_dl.asp
Just for reference, HTML 3.0 had <lh>
:
<ul>
<lh>This is a list header!
<li>Item A
<li>Item B
<li>Item C
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With