Writing Clean Beautiful HTML Code

While writing HTML code, it’s not always necessary to put the tags in any specific order or format because the code runs anyway. But it is important to organize your code in a way it looks clean, beautiful and readable.

Dirty Code

Clean Code

There are two types of HTML tags, viz. block and inline tags. Block tags take up most of the space in a document. So even if you put them next to each other they will take up the whole space.

OUTPUT:

But using inline tags the texts can be written next to each other.

OUTPUT:
Inline tag example

Some tags contain other tags like, <html> contains <head>, <body>, <style>, <script> etc. while others like <p> also contain texts along with some inline tags. So in order to show the relationship among tags we need to indent them accordingly.

Some coders prefer not to indent <head> and <body> tag because it’s very obvious that they are contained inside <html>. That’s why <html> is called a parent tag. And the tags inside it are called its children. So <body> and <head> are child tags of <html> and both are called siblings because they have the same parent.

You can use the spacebar 2 or 4 times to indent but usually, in most code editors, the indentation is inbuilt. So you do not need to worry about it.

We indent only block tags, not the inline tags. But some block tags like <title> or <li> are also not indented because they are already very short.

OUTPUT:

Writing a clean code also shows that you care and love what you do.

CategoriesTags

Related Posts