Is it bad to nave no <header>?
I have this basic structure but I am thinking about wrapping a header arround everything between the body start and the content because the header has also this nice shema.org markup attached. Good Idea?
Just because sometimes the header is not there but the navbars actually contain the brandname and are sort of headers themseves.
if I do this how should I markup whats the <header> right now? A section inside a header is not valid right? Just a normal <div> I guess.
<body>
<div class="nav1"><h1>brandname</h1><nav>...</nav></div>
<header><h1>brandname</h1><p>slogan</p></header> <!-- sometimes this is not there -->
<div class="nav2"><h1>brandname</h1><nav>...</nav></div>
<div>content ...
To this
<body>
<header itemtype="http://schema.org/WPHeader" ...>
<div class="nav1"><h1>brandname</h1><nav>...</nav></div>
<div><h1>brandname</h1><p>slogan</p></div> <!-- sometimes this is not there -->
<div class="nav2"><h1>brandname</h1><nav>...</nav></div>
</header>
<div>content ...
Multiple headers not valid either or? I am thinking of it this way as using the <header> without any CSS styles just for semantic markup. I could think of styling barriers created with this so not 100% satisfied with this.
Or is this valid.
<body>
<header class="nav1"><h1>brandname</h1><nav>...</nav></header>
<header><h1>brandname</h1><p>slogan</p></header> <!-- sometimes this is not there -->
<header class="nav2"><h1>brandname</h1><nav>...</nav></header>
<div>content ...
// just read html5: using header or footer tag twice? this and looks like I stick with the 2nd solution since I not really like the idea of having no header at all
Dont be afraid to answer ;)
Decide for every sectioning root element (e.g., body) and every sectioning content element (e.g., article) if it contains content which is appropriate for header. The spec defines that it’s for "introductory content", and gives the following examples:
introductory or navigational aids
heading (an h1–h6 element) (not required)
table of contents, a search form, or any relevant logos
If there is such content, use a header element. If the content in that sectioning root/content element is scattered, use several header elements.
Keep in mind that the header always applies for "its nearest ancestor sectioning content or sectioning root element":
<body>
<header> <!-- this header is for the whole page --> </header>
<article>
<header> <!-- this header is for the whole article element --> </header>
<div>
<header> <!-- this header is also for the whole article element (and not only for the div element!) --></header>
</div>
<section>
<header> <!-- this header is for this article’s section element only --> </header>
</section>
</article>
<figure>
<header> <!-- this header is for the figure element only --> </header>
</figure>
<strong>
<header> <!-- but this header is also for the whole page (and not for the strong element only!) --> </header>
</strong>
</body>
Related
I received designs for one project and client wants footer placed in sidebar which is main navigation. Is it correct to have structure like this? I mean from design part it looks good, but from code it looks weird.
<nav class="Navbar">
<div class="Navbar-content">
<!-- some stuff -->
</div>
<footer class="Footer">
<!-- footers content -->
</footer>
</nav
In w3 there is no exact rule which would forbid nesting footer in navbar.
https://www.w3.org/html/wiki/Elements/footer
Also the w3 validator doesn't recognize it as any kind of error, so I guess it's okay?
You can have a footer element in a nav element. But it would represent the footer for that navigation, not the footer for the whole page.
For a footer that applies to the whole page, you have to place the footer element so that its nearest section parent is the body element. This means it can’t be a descendant of section/article/nav/aside nor of a sectioning root element other than body (blockquote, figure etc.). See an example.
If you want to show the navigation and the footer in a sidebar, you can use a div element for the sidebar:
<body>
<h1>…</h1>
<div class="sidebar">
<nav><!-- site-wide navigation --></nav>
<footer><!-- site-wide footer --></footer>
</div>
</body>
Is using multiple header tags common practice when it comes to separating a main-header and a sub-header, or should you divide one header into two sections using divs?
For example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header class="header--main-header">
</header>
<header class="header--sub-header">
</header>
</body>
</html>
As opposed to:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<div class="header--main-header">
<!-- Main-Header Content -->
</div>
<div class="header--sub-header">
<!-- Sub-Header Content -->
</div>
</header>
</body>
</html>
Would either of these methods be the "best-practice" way to achieve a website header and sub-header, or is there another approach that I'm missing?
You can use as many headers as you like. We tend to use a main header at the top of our HTML containing our h1, navigation, utilities etc. But headers can also be used for the head of a section.
For instance:
<header>
<h1>The most important heading on this page</h1>
<p>With some supplementary information</p>
</header>
<article>
<header>
<h2>Title of this article</h2>
<p>By Richard Clark</p>
</header>
<p>...Lorem Ipsum dolor set amet...</p>
</article>
It is the head tag that should only be used once per page.
The header tag element represents a container for introductory content or a set of navigational links.
A header tag element typically contains:
one or more heading elements (h1 - h6)
logo or icon
authorship information
You can have several elements in one document.
Convention in HTML is that generally there is one header tag. Sub-headers can be seperated by any element you wish, div for example like you have.
You can check out some of the example usage of header here and here.
Usually the header tag, followed by the nav or div tag itself. It is best-practice to use the nav tag.
I have gone through a discussion regarding header and footer. As HTML5 provides header, footer, content elements, I believe it should be used once per page. I state them in following snippet.
<!-- My understanding -->
<header>
<!-- code goes here -->
</header>
<content>
<!-- code goes here -->
<!-- code goes here -->
</content>
<footer>
<!-- code goes here -->
</footer>
Few people have header, footer elements approach like below.
<!-- People understanding -->
<header>
<!-- code goes here -->
</header>
<content>
<!-- code goes here -->
<!-- code goes here -->
</content>
<footer>
<header>
<!-- They also use <header> in footer. -->
</header>
<!-- code goes here -->
</footer>
Can header be used in footer element? In other words, What would you suggest to build a HTML structure?
No this isn't valid.
The header element represents a header for the content, however the header element must not be a descendant of the footer element.
The footer section of the HTML5 specification states that the footer element is:
Flow content, but with no header, footer, or main element descendants.
If you paste the below code into W3's HTML Validator you'll get the following error:
Line 8, Column 20: The element header must not appear as a descendant of the footer element.
<!doctype html>
<html>
<head>
<title>Title</title>
</head>
<body>
<footer>
<header>
</header>
</footer>
</body>
</html>
Ignoring the invalid content element, your first example is valid and you should stick with that instead.
I suggest that you should stick to your first line idea:
// My understanding
<header>
//code goes here
</header>
<content>
//code goes here
//code goes here
</content>
<footer>
//code goes here
</footer>
For me, it is more easy way to understand how to make a HTML file on this format.
The second example would technically work fine, as all of the <header>, <content> and <footer> functionally work similarly to <div>s, but semantically it'd be very bad practice. I'd go with your initial thoughts, which would certainly be the spirit in which those elements are intended.
According to MDN:
The HTML Element represents a group of introductory or
navigational aids. It may contain some heading elements but also other
elements like a logo, wrapped section's header, a search form, and so
on.
so I don't suggest you to use footer tag inside header tag.
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.
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.