This has been bothering me for a while, and I\'m wondering if there\'s any consensus on how to do this properly. When I\'m using an HTML list, how do I semantically include
As Felipe Alsacreations has already said, the first option is fine.
If you want to ensure that nothing below the list is interpreted as belonging to the heading, that's exactly what the HTML5 sectioning content elements are for. So, for instance you could do
<h2>About Fruits</h2>
<section>
<h3>Fruits I Like:</h3>
<ul>
<li>Apples</li>
<li>Bananas</li>
<li>Oranges</li>
</ul>
</section>
<!-- anything here is part of the "About Fruits" section but does not
belong to the "Fruits I Like" section. -->
Your first option is the good one. It's the least problematic one and you've already found the correct reasons why you couldn't use the other options.
By the way, your heading IS explicitly associated with the <ul>
: it's right before the list! ;)
edit: Steve Faulkner, one of the editors of W3C HTML5 and 5.1 has sketched out a definition of an lt element. That's an unofficial draft that he'll discuss for HTML 5.2, nothing more yet.
According to w3.org (note that this link is in the long-expired draft HTML 3.0 spec):
An unordered list typically is a bulleted list of items. HTML 3.0 gives you the ability to customise the bullets, to do without bullets and to wrap list items horizontally or vertically for multicolumn lists.
The opening list tag must be
<UL>
. It is followed by an optional list header (<LH>
caption</LH>
) and then by the first list item (<LI>
). For example:<UL> <LH>Table Fruit</LH> <LI>apples <LI>oranges <LI>bananas </UL>
which could be rendered as:
Table Fruit
- apples
- oranges
- bananas
Note: Some legacy documents may include headers or plain text before the first LI element. Implementors of HTML 3.0 user agents are advised to cater for this possibility in order to handle badly formed legacy documents.
a <div> is a logical division in your content, semantically this would be my first choice if I wanted to group the heading with the list:
<div class="mydiv">
<h3>The heading</h3>
<ul>
<li>item</li>
<li>item</li>
<li>item</li>
</ul>
</div>
then you can use the following css to style everything together as one unit
.mydiv{}
.mydiv h3{}
.mydiv ul{}
.mydiv ul li{}
etc...
You could also use the <figure>
element to link a heading to your list like this:
<figure>
<figcaption>My favorite fruits</figcaption>
<ul>
<li>Banana</li>
<li>Orange</li>
<li>Chocolate</li>
</ul>
</figure>
Source: https://www.w3.org/TR/2017/WD-html53-20171214/single-page.html#the-li-element (Example 162)
Try defining a new class, ulheader, in css. p.ulheader ~ ul selects all that immediately follows My Header
p.ulheader ~ ul {
margin-top:0;
{
p.ulheader {
margin-bottom;0;
}