In HTML5, can the
and
tags appear outside of the <body> tag?

后端 未结 8 1475
梦谈多话
梦谈多话 2020-12-01 07:12

I\'m currently using the above tags in this way (classic tag order):


  ...
  
    
...
相关标签:
8条回答
  • 2020-12-01 07:47

    I see what you are trying to do, you are trying to use the <body> tag as the container for the main content of the page. Instead, use the <main> tag, as specified in the HTML5 spec. I use this layout:

        <!DOCTYPE html>
        <html>
            <head> *Metadata* </head>
            <body>
                <header>
                    *<h1> and other important stuff </h1>*
                    <nav> *Usually a formatted <Ul>* </nav>
                </header>
                <main> *All my content* </main>
                <footer> *Copyright, links, social media etc* </footer>
            </body>
        </html>
    

    I'm not 100% sure but I think that anything outside the <body> tag is considered metadata and will not be rendered by the browser. I don't think that the DOM can access it either.

    To conclude, use the <main> tag for your content and keep formatting your HTML the correct way as you have in your first code snippet. You used the <section> tag but I think that comes with some weird formatting issues when you try to apply CSS.

    0 讨论(0)
  • 2020-12-01 07:47

    This is the general structure of an html document.

    <html>
        <head>
            Title, meta-data, scripts, etc go here... Don't confuse with header
        </head>
        <body>
            You body stuff comes here...
            <footer>
                Your footer stuff goes here...
            </footer>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-12-01 07:50

    According to the HTML standard, the content model of the HTML element is:

    A head element followed by a body element.

    You can either define the BODY element in the source code:

    <html>
        <body>
            ... web-page ...
        </body>
    </html>
    

    or you can omit the BODY element:

    <html>
        ... web-page ...
    </html>
    

    However, it is invalid to place the BODY element inside the web-page content (in-between other elements or text content), like so:

    <html>
        ... content ...
        <body>
            ... content ...
        </body>
        ... content ...
    </html>
    
    0 讨论(0)
  • 2020-12-01 08:01

    Well, the <head> tag has nothing to do with the <header> tag. In the head comes all the metadata and stuff, while the header is just a layout component.
    And layout comes into body. So I disagree with you.

    0 讨论(0)
  • 2020-12-01 08:01

    If you really want it to look more semantic like having the <body> in the middle you can use the <main> element. With all the recent advances the <body>element is not as semantic as it once was but you just have to think of it as a wrapper in which the view port sees.

    <html>
        <head>
        </head>
        <body>
            <header>
            </header>
            <main>
                <section></section>
                <article></article>
            </main>
            <footer>
            </footer>
        <body>
    </html>
    
    0 讨论(0)
  • 2020-12-01 08:01

    Even though the <head> and <body> tags aren't required, the elements are still there - it's just that the browser can work out where the tags would have been from the rest of the document.

    The other elements you're using still have to be inside the <body>

    0 讨论(0)
提交回复
热议问题