Can you put a <center> tag before <body> tag in HTML?

Viewed 68

Recently in class I have been tasked with making a web page. I made the page - I was expecting a high grade (it's nothing special but we're starting out in HTML, didn't even touch CSS yet), but to my surprise I failed the task.

Apparently, you're not supposed to put a <center> tag before a <body> tag. My .html file looked something like this:

<!DOCTYPE html>
<html>
    <head>
        <title>filler</title>
        <meta charset="UTF-8"/>
    </head>
    
<center>
    <body>
    <p>filler</p>
    </body>
</html>

Is it actually not allowed to put a tag before a tag? If so, what would be a better way to align it? (maybe body style?)

3 Answers

As other said in comment, dont use center anymore. Here how to center content inside body with place-items:

 html, body { height: 100%; }

body { display: grid; place-items: center; }
<!DOCTYPE html>
<html>
    <head>
        <title>filler</title>
        <meta charset="UTF-8"/>
    </head>
    
    <body>
    <p>filler</p>
    </body>
</html>

Next step is to learn about display: grid and display: flex :)

you can, but you shouldn't, also <center> is obsolete... use body { text-align:center; }

As mentioned in other answers, center is obsolete. But even if it wasn't, you should have put it inside the body element, and not the other way around:

<body>
    <center>
        ...
    </center>
</body>

The body element represents the content of the document, where "content" is - broadly speaking - everything the user can actually see. This means that if the user can see it, it should probably be inside the body element. Since the point of the center element is to alter what the user sees, it belongs inside body.

However, there is a good reason center is obsolete: enhancing the separation between the presentational structure of a web page and its design. We use HTML to model what a web page consists of, and CSS to express how it should look. If you find yourself writing HTML with the intention of changing the look of your page - you are probably doing something wrong.

So to summarize: the center element is an HTML element that affects design, this is why it was made obsolete. And even if it wasn't obsolete, it should have been inside body.

Related