Search code examples
htmlalignment

Why does attribute align="center" work in my html5 file?


I just started with html and wrote a small basic file:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>
            A Page
        </title
    </head>
    <body>
        <h1 align="center">
            A Header
        </h1>
        <p>
            Some content
        </p>
        <audio src="some-audio.mp3" controls>
            If this shows, somthing is not right.
        </audio>
    </body>
</html>

By inserting <!DOCTYPE html> and <meta charset="UTF-8"> i wanted to make it a html5 file. To test this, I inserted the audio, which should not work with previous html versions.

But since the audio works and the file is html5, why does the <h1 align="center"> still work?

Everywhere I search it always says that the align attribute is not supported by html5.

I will leave the file online until the question is solved: http://patriclay.de/testhtml5.htm


Solution

  • The Doctype is used only to switch between standards and quirks modes.

    Browsers support obsolete elements and attributes for the sake of backwards compatibility.

    This is explicitly mentioned in the HTML specification which says:

    The following rules are also expected to apply, as presentational hints:

    p[align=center i], h1[align=center i], h2[align=center i], h3[align=center i],
    h4[align=center i], h5[align=center i], h6[align=center i] {
      text-align: center;
    }
    

    Do note, however, that the spec also says:

    The following attributes are obsolete (though the elements are still part of the language), and must not be used by authors: