Which is more semantic for a sidebar of widgets? - html

In the following example html5 page structure, which of the following is more semantic in respect to the sidebar of widgets (elements which appear in the sidebar are elements which appear on multiple pages and do not necessarily, directly, nor particularly relate to this page of content):
<body>
<header id="site-header">
...
</header>
<section id="page-body">
<main>
<header></header>
<article></article>
<footer></footer>
</main>
<aside class="sidebar" id="sidebar-a">
<section id="search-widget">
...search field, etc...
</section>
<section id="recent-articles-widget">
...articles list...
</section>
</aside>
<aside class="sidebar" id="sidebar-b">
<section id="cloud-tag-widget">
...search field, etc...
</section>
<section id="recent-articles-widget">
...articles list...
</section>
</aside>
</section>
<footer id="site-footer"> ... </footer>
</body>
or...
<body>
<header id="site-header">
...
</header>
<section id="page-body">
<main>
<header></header>
<article></article>
<footer></footer>
</main>
<section class="sidebar" id="sidebar-a">
<aside id="search-widget">
...search field, etc...
</aside>
<aside id="recent-articles-widget">
...articles list...
</aside>
</section>
<section class="sidebar" id="sidebar-b">
<aside id="cloud-tag-widget">
...tag list...
</aside>
<aside id="popular-articles-widget">
...articles list...
</aside>
</section>
</section>
<footer id="site-footer"> ... </footer>
</body>
AKA - Is it more semantic or in any way more appropriate to put multiple asides within a section, or include multiple sections within an aside to create a sidebar of widgets? Should they instead be simple divs within a div? Or divs within a section? Divs within an aside? Why?
Which is easier for screen readers or search engines? Why?

The aside is "tangentially related to the content around" it.
Currently, it doesn’t seem to be defined what "around" means exactly. Assuming that it’s (at least) the content of the parent sectioning content element, then this would mean for your examples:
<body> <section> <aside></aside> </section> </body>
The aside is related to the content of the section.
<body> <aside> <section></section> </aside> </body>
The aside is related to the content of the body (i.e., of the whole document).
So in your case, you’ll probably want to have aside as descendant of body (and of no other sectioning content element).
The next thing to decide would be: one aside with several sub-sections vs. several aside. I’d go with a separate aside for each "sidebar block", unless you can logically group (*) these blocks.
* I.e., if there is a natural heading that could be used (it doesn’t matter if you actually use it) to group several sidebar blocks, use one aside with section childs for these.

The <aside> tag defines some content aside from the content it is placed in.
The aside content should be related to the surrounding content.

Related

Is correct wrapping <header> and <nav> tags

Is correct (semantically) wrapping <header> and <nav> tags to share in the background
example
<div id="background">
<header> <!-- ... --> </header>
<nav> <!-- ... --> </nav>
</div>
It is absolutely correct.
You could have the header and nav nested, or not nested. Both are fine. For example you could use any of these:
<div class="wrapper">
<header>
<nav></nav>
</header>
</div>
<div class="wrapper">
<header></header>
<nav></nav>
</div>
<section>
<header></header>
<nav></nav>
</section>
<article>
<header></header>
<nav></nav>
</article>
here's a useful thing to read: http://www.w3.org/wiki/HTML_structural_elements
in short: yes, its valid html5 structure
"the <div> still has a perfectly valid use. You should use it when there is no other more suitable element available for grouping an area of content, which will often be when you are purely using an element to group content together for styling/visual purposes. A common example is using a to wrap all of the content on the page, and then using CSS to centre all the content in the browser window, or apply a specific background image to the whole content."

Main content in HTML5

Let's say, I always want to use the main element in a project for different page types. One of these page types is just a product page, with a header and some sections. Which variant would be semantically correct:
A
<body>
<header>
...
</header>
<main>
<header>
<h1>...</h1>
<img />
</header>
<section>
<h1>...</h1>
<p>...</p>
</section>
<section>
<h1>...</h1>
<p>...</p>
</section>
</main>
<footer>
...
</footer>
</body>
B
<body>
<header>
...
</header>
<main>
<article>
<header>
<h1>...</h1>
<img />
</header>
<section>
<h1>...</h1>
<p>...</p>
</section>
<section>
<h1>...</h1>
<p>...</p>
</section>
</article>
</main>
<footer>
...
</footer>
</body>
I would say A can't be correct. Accordingly to the working draft:
The header element represents introductory content for its nearest ancestor sectioning content or sectioning root element.
The main element is not sectioning content and has no effect on the document outline.
Thus the body > main > header in A would represents the introductory content for body which is the sectioning root element.
For those who might ask, why you don't use B without main. This has technical reasons either I always use main or not. And there are other page types with multiple articles in the main content.
Example A is not correct. I can think of no content where example A would make sense.
You gave the correct explanation why this is the case:
The header belongs to its "nearest ancestor sectioning content or sectioning root element".
In example A, both header elements would belong to body (= nearest sectioning root element), but only the first header should belong to it, while the second header should belong to the product.
As main is not a sectioning content element, you’d need to use one here. article seems to be the correct choice (as all its content is about a product), so example B looks fine.

Proper usage of HTML5 elements & structure

I need to convert a PSD to HTML5/CSS. I'm having problems finding a definitive answer on what elements to use. My layout consists of a header, content/sidebar and footer. The template structure is below.
I understand you can use multiple header / footer elements with HTML5, I've got the main header/footer tags. The main problem I'm having is with the structure of the content.
I've got a main content area, and then a sidebar, the sidebar content doesn't really relate to the content. Within the sidebar I need to have multiple content boxes.
The content area has various sections, mainly a featured image with text on, and then the rest is just the page text.
From what I understand, using the <section> tag to define the content / sidebar isn't semantic as it's not content related, it's page structure / layout. Does my markup below look correct for the structure I'm trying to achieve?
<header>
</header>
<div class="wrapper">
<div class="main">
<section id="featured-content">
<img />
<p>Featured text</p>
</section>
<section id="main">
<header><h2>Title</h2></header>
<p>The rest of content goes here</p>
</section>
</div>
<aside class="sidebar">
<section class="content-box">
<header>Box title</header>
<p>Box content</p>
</section>
<section class="content-box">
<header>Box title</header>
<p>Box content</p>
</section>
</aside>
</div>
<footer>
</footer>`
In general: If the header only contains a heading, you could omit it.
If the main content of the page could stand on its own (e.g. like a blog post or similar), then you could use article instead of section.
As the #featured-content is some kind of abstract/summary of the main content, it should be part of the article. It makes sense to include it in the header:
<article>
<header>
<h2>(article title)</h2>
<img src="" alt="(article teaser image)">
<p>(article teaser text)</p>
</header>
<p>(article content)</p>
</article>
(Only if the teaser is really complex (e.g. containing headings itself), it could get its own section.)
Using aside for the sidebar is correct, because its content is "tangentially related" to the whole page (!) and not only to the main content (in that case, it should be include in the article, too).
If all content blocks in that aside are somewhat related, using section inside of that aside for each block is correct (but, depending on the actual content, article could be appropriate, also). However, if the blocks inside of that aside have no relation (easy test: do you find a heading for the whole aside?), you could consider using a separate aside for each of it:
<div class="sidebar">
<aside>
<h2>…</h2>
</aside>
<aside>
<h2>…</h2>
</aside>
</div>
So the structure could look like:
<body>
<h1>…</h1> <!-- maybe with header -->
<article>
<h2>…</h2>
</article>
<aside>
<h2>…</h2> <!-- omit it or hide it visually -->
<section> <!-- or article -->
<h3>…</h3>
</section>
<section> <!-- or article -->
<h3>…</h3>
</section>
</aside> <!-- resp. use 1 aside for each block -->
<footer></footer>
</body>

Which of these is better semantically?

In order to make websites more accessible I have been encouraged to use HTML5 tags <header>, <footer>, etc... to only surround the actual content, but I have a feeling that I might be doing something wrong.
An example body:
<header>
<div class="center">
<h1>Title</h1>
<nav>
...
</nav>
</div>
</header>
<div>
<section>
...
</section>
</div>
<footer>
<div class="center">
...
</div>
</footer>
.center {
max-width: 70em;
margin: 0 auto;
}
header {
width: 100%
background-color: red;
}
footer {
width: 100%
background-color: green;
}
body > div {
width: 100%
background-color: blue;
}
Is it actually better like this?
<div id="head">
<header>
<h1>Title</h1>
<nav>
...
</nav>
</header>
</div>
<div>
<section>
...
</section>
</div>
<div id="foot">
<footer>
...
</footer>
</div>
As for what is better — DIV inside structural elements like HEADER/FOOTER or structural elements inside DIV, it does not matter since DIV is common container without any semantic sense at all.
What is really unsemantic/bad-practice in your first example is center class name. Class names should reflect purpose of block (content, products, etc.), not its presentation (center, red, etc.).
Basically, that div elements are not required semantically speaking (maybe you need them for styling?).
div is an element without semantic (as its counterpart for inline elements span) and you have to use them where there isn't anything better. Even if you give them some semantic with its id attribute, that semantic is only known by you and not for, for example, any web search motor (google) or any screen reader (for blind people, for example), because there aren't any definitive conventions about id or class values.
If you use header, footer etc, you are giving them semantics. Maybe you want to increase their semantic using some value for the role attribute.
By the way, that section surely it isn't needed. Look at what people from HTML5 Doctor say:
In HTML 5 you can specifically mark up all the “secondary” content on
a page such as navigation, branding, copyright notices, so it feels
odd that you can’t specifically mark up the most important part of
your page—the content.
But what would be the purpose of marking it up specifically, anyway?
If you need to style it, use a div. An assistive technlogy like a
screenreader can find the main content because it is the first thing
inside a page that isn’t a header, nav or footer.
With a <div role="main"> you have everything you need.
It'd be better like this:
<header>
<h1>Title</h1>
<nav>
...
</nav>
</header>
<section>
...
</section>
<footer>
...
</footer>
Or alternatively:
<div class="header">
<h1>Title</h1>
<div class="nav">
...
</div>
</div>
<div class="section">
...
</div>
<div class="footer">
...
</div>
Why are you being told to add those extra wrapper div elements?
Try
<section>
<header> head content </header>
main content
<footer> footer content </footer>
</section>
This get's rid of all those silly divs and now you have your header and footer linked to your section like they should be.

Multiple <header> and <footer> in a HTML5 document

Is it fine to use multiple <header> and in HTML 5, if yes then isn't semantically incorrect and won't it confuse mobile readers?
I saw many site uses like
<body class="home">
<header class="hd1">
<hgroup>
<h1>HTML5 Documnet</h1>
<h2>tagline</h2>
</hgroup>
</header><!-- .hd1 -->
<div class="main">
<section class="hs1">
<header>
<h1>This is a Page Sub Title</h1>
</header>
<p>Some content...</p>
<h2>Demonstrating EM and STRONG</h2>
<p><strong>This text will have more importance (SEO-wise and contextually)</strong></p>
<footer>
<p>Author: <cite>Louis Lazaris</cite></p>
</footer>
</section>
</div><!-- .main -->
<footer class="f1">
<p>copyright © year</p>
</footer><!-- .f1 -->
</body>
Yes, multiple <header> and <footer> elements are fine. They aren't used the same as <div id="header"> as most people use them for. Technically speaking, header and footer represent a header and footer of a section. A section being a piece of the page such as an article that contains header tags like <h1> and then content, then footer stuff like copyrights, citations, references, etc.
From the horses mouth:
A header element is intended to usually contain the section's heading
(an h1–h6 element or an hgroup element), but this is not required. The
header element can also be used to wrap a section's table of contents,
a search form, or any relevant logos.
And
The footer element represents a footer for its nearest ancestor
sectioning content or sectioning root element. A footer typically
contains information about its section such as who wrote it, links to
related documents, copyright data, and the like.
Directly from the spec at: http://dev.w3.org/html5/spec/Overview.html
Note that as I said these are not used to create sections like people did with <div id="header/footer"> it mentions this confusion in the spec:
The footer element is not sectioning content; it doesn't introduce a
new section.
So, again, "technically" speaking, that last footer you have there introduces a new section and isn't semantic. From the spec's point of view anyways.