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?
HTML Elements are Nested
Notice how the elements are in a nested structure. Proper nesting is required for the HTML to be valid.
What's wrong with the HTML below?
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.
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.
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.
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.
Your styles can override these defaults.
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.
Element Flow: Inline vs Block
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
Last updated
Was this helpful?