Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I semantically group a header with a UL in HTML?

Tags:

html

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?

like image 203
Yes - that Jake. Avatar asked Mar 10 '10 15:03

Yes - that Jake.


People also ask

Can a UL contain a heading element?

Yes, it's wrong. It's not valid HTML. Github has h4s within uls in their footer.

How do I make a heading in HTML UL?

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.

How do you style a header tag in HTML?

<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.

What is the following HTML tag used for UL?

<ul>: The Unordered List element. The <ul> HTML element represents an unordered list of items, typically rendered as a bulleted list.


3 Answers

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

like image 180
Nick Allen Avatar answered Oct 10 '22 00:10

Nick Allen


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

like image 13
Nima Foladi Avatar answered Oct 10 '22 00:10

Nima Foladi


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>
like image 27
Pup Avatar answered Oct 09 '22 23:10

Pup