Let's talk a little bit about block level elements. The heading 1 (h1), heading 2 (h2), paragraphs (p), list items (li), and divs (div) are all examples of natural block level elements. Block level elements have two defining traits: they expand the full width available, and they force elements that come after them to appear on the next line, meaning they stack on top of each other. So let's remove the box-sizing property from our declaration block as well as the width property to demonstrate how they take up the full width available if no width is specified:
h1 {
font-size: 40px;
line-height:1.4;
font-weight: bold;
color: #0072ae
background-color: black;
padding: 50px;
margin: 50px;
border: 10px solid blue;
}
Save this and refresh the site. You can see in the following screenshot that, as you make your browser window larger, it takes up the full width available, apart from the margin that we set of 50px on all sides:
Now let's go into the HTML file, add two more of these h1 tags into the HTML, and save it:
<section>
<h1>Old Chompy</h1>
<h1>Old Chompy</h1>
<h1>Old Chompy</h1>
Here's what that looks like:
Now you can see how these block level elements stack on top of each other: good ol' block level elements.
Inline elements, on the other hand, behave differently. They sit next to each other horizontally and they don't take up the full width available. They only take up as much width as they need. A few elements that are naturally inline elements are the anchor (<a>), <span>, <i>, <b>, <strong>, and <em> tags.
Alright, so let me go into the HTML and add three span tags to the page:
<section>
<h1>Old Chompy</h1>
<h1>Old Chompy</h1>
<h1>Old Chompy</h1>
<span>Inline</span>
<span>Inline</span>
<span>Inline</span>
What I'll also do is generally target those span elements in a rule set and give them a green background, just to kind of see that they're distinct:
span {
background-color: green;
}
Here's how that looks:
You can notice how the green inline elements sit next to each other horizontally instead of stacking vertically. Nothing special, but we can see how they do not take up the full width available, they only take as much as they need.
There are some things that inline elements do not do. They don't respond to width or margin-top or margin-bottom. So if an element is naturally inline and you give it a width and a margin-top or margin-bottom, as shown in the following code, it's going to do absolutely nothing:
span {
background-color: green;
width: 1000px;
margin-top: 1000px;
}
Nothing changes:
Inline elements just don't respect those properties, and those properties don't have an impact on them, so we'll remove those.
There's one last interesting thing you can do. There's a display property that allows you to change a natural block level element to inline and vice versa. So let's add a display property with the block value to our span selector and view that in the browser. So, I can just say display: block and also add some margin-top:
span {
background-color: green;
display: block;
margin-top: 10px;
}
We can see that these elements now stack on top of each other and now respect the margin-top and margin-bottom values:
Elements with the display property set to block would respect any width value I give it, but it also takes up the full width available. You can see that it extends all the way to the edge of our screen. We could've just as easily used the display: inline property on our h1 selector to change the nature of the display from block to inline. Lastly though, we can use display: none, which totally hides the element from the page and is often used for various reasons. So let's go to our h1 declaration and say display: none:
h1 {
font-size: 40px;
line-height:1.4;
font-weight: bold;
color: #0072ae;
background-color: black;
padding: 50px;
margin: 50px;
border: 10px solid blue;
display: none;
}
Now, if we look at our site, that h1 is invisible. It's no longer something that the browser is going to show us:
To sum up, all elements conform to a box model. The box model changes a little depending on how the box-sizing property is used, if used at all. Also, the box model changes based on whether the element is block or inline, the two most common display properties.