# Important HTML Rules

Before we move on to adding our own elements within the body element, let's review a few important rules and concepts.

### Opening and  Closing Tags are Required

Unless the element is a self-closing tag, the element must have both a start and ending tag.

What's wrong with the HTML below?

```markup
<body>
    <h1>This is the title
        <p>Lorem ipsum dolor sit amet, consectetur 
        adipiscing elit, sed do eiusmod tempor incididunt 
        ut labore et dolore magna aliqua. 
        Ut enim ad minim veniam</p>
</body>
```

### HTML Elements are Nested

Notice how the elements are in a nested structure. Proper nesting is required for the HTML to be valid.

```markup
<ol>
  <li>Item 1</li>
  <li>
    Item 2
    <ul>
      <li>Subitem 1</li>
      <li>Subitem 2</li>
    </ul>
  </li>
  <li>Item 3</li>
</ol>
```

What's wrong with the HTML below?

```markup
<body>
    <div>
        <p>This is a paragraph</div></p>
</body>
```

### Browsers are Forgiving

While there are ways to enforce HTML code to follow the rules, browsers have always attempted to gracefully handle errors in HTML. The HTML that currently exists is full of such errors, so it is not possible to change this behavior.&#x20;

It's up to you to carefully review your HTML to make sure the syntax is correct, otherwise you'll get unexpected results.

### White-space in HTML is ignored

You can add as much white-space as you want within the text of an element, and it will be ignored.

```markup
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
        sed do eiusmod tempor incididunt ut labore et dolore 
        magna aliqua. Ut enim ad minim veniam, quis nostrud 
        exercitation ullamco laboris nisi ut aliquip ex ea 
        
        a
        a
        as  b
        s fsd
         g sdfa g
        
        
        
        commodo consequat. Duis aute irure dolor in reprehenderit 
        in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
         Excepteur sint occaecat cupidatat non proident, 
         sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
```

![](https://1336010533-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWvM--ORajEf7cHwnl6%2F-MYIK7mpPh_S2utNS4RE%2F-MYInuzplXQAP_KeUDsT%2Fimage.png?alt=media\&token=ddcec2c3-fe56-4aec-9e81-fbdbce4736bd)

### Comments are Ignored

Anything that starts with **\<!-- and ends with -->** will be completely ignored by the browser. This is useful for documenting your code and making notes to yourself, or for commenting out HTML that you are debugging.

```markup
<body>
    <!-- This will be ignored -->
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
        sed do eiusmod tempor</p>
    <!-- <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
        sed do eiusmod tempor</p> -->  
</body>
```

### User Agent Default Styles

User-agent styles refer to the default stylesheets a browser apply to web pages. Each browser defines their own default styles, but they do tend to be pretty standard.

Here is the default stylesheet that will be applied by the Chrome browser.

{% embed url="https://chromium.googlesource.com/chromium/blink/+/master/Source/core/css/html.css" %}

Your styles can override these defaults.&#x20;

If you look at the user-agent stylesheet above, you will notice that quite a few elements have the CSS display property set to "**display: block**". This is because the default for all elements is for them to be "inline", but the expectation for how certain elements flow on the page is for them to have a block layout.&#x20;

### Element Flow: Inline vs Block&#x20;

Every element on a web page is a rectangular box.  By default, elements flow from top to bottom, but the CSS display property is used to determine how much horizontal and vertical space an element takes up.

* **Block elements**: take up the entire line, unless they are empty.
* **Inline elements**: take up width of their content only.
* **Inline block elements**: like inline, except can specify width and height.

headings, paragraphs, lists are examples of block elements. images, links, spans are examples of inline elements

![](https://1336010533-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWvM--ORajEf7cHwnl6%2F-MYIK7mpPh_S2utNS4RE%2F-MYLdUUwDV9GTvPISb16%2Fimage.png?alt=media\&token=3414f5f5-7e67-4b26-8bc7-01b6efb5f748)
