简体   繁体   中英

Why do HTML designers indent their HTML code?

For instance, why do people write:

<html>
<body>
   <p>blahblahblah</p>
   <p>blahblah</p>
</body>
</html>

Instead of

<html>
<body>
<p>blahblahblah</p>
<p>blahblah</p>
</body>
</html>

I just finished going through an HTML course, but never learned why this is. Is it just to make the code easier for people to read?

It's a convention because it makes it easier to visualize the structure of the document, to locate without using a parsing tool that you've closed your tags, and to help visually segregate page components.

In summary, because it's easier to write, read and maintain in the long term, to adopt some kind of visual organization strategy.

Its all about being able to understand your code the next time you open your code. It helps in making sure your code is free or errors and makes debugging easy.

Indentation has no purpose beyond readability of code, which is of course subjective. Readability in particular relates to visualizing the nesting of elements. This may help to avoid problems like accidental omission of end tags.

There are varying practices, regarding the amount of indentation and logic in using it (eg, some people would also indent the body element, since it is a child of the html element).

Quite often, indentation is there just because some authoring software produces it automatically, and authors see no reason to override it.

Indentation is normally ignored by browsers, since any sequence of whitespace characters (spaces, tabs, line breaks) is equivalent to a single space in most contexts in HTML. Sometimes it is not, and then indentation causes problems.

And at the end of the day you can always use Dave Raggett's "Tidy" to clean up your pages for you. http://www.w3.org/People/Raggett/tidy/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM