Mastering the Box Model in CSS: Block vs Inline
Tags: webdev, beginners, tutorial, css
Published: 2023-08-24 10:45:16

## Introduction
As a web developer, understanding the CSS box model is fundamental to crafting a polished user experience. The box model comprises the content, padding, border and margin of an element, and it determines how elements are rendered on the page. Mastering the nuances of the box model unlocks new possibilities for layout and design. In this article, we will explore the difference between block and inline elements and how they interact with the box model. Block elements break the normal flow of a page, stacking on top of each other, while inline elements flow with surrounding content. Knowing when to use each element type gives you granular control over spacing, positioning, and the overall flow of your web pages. Read on to gain intimate understanding of block vs inline and how the box model brings it all together.

_**Pre-requisites:**_
For one to get a better understanding of the topics in this article, you should have prior knowledge of HTML and CSS programming basics.

Let's have a jump ahead:

{%- # TOC start (generated with https://github.com/derlin/bitdowntoc) -%}

- [What is the CSS Box Model?](#what-is-the-css-box-model)
- [Block Level Boxes vs Inline Boxes](#block-level-boxes-vs-inline-boxes)
- [Using Display: Block](#using-display-block)
  * [Using Margin and Padding](#using-margin-and-padding)
  * [Controlling Width](#controlling-width)
  * [Floating Block Elements](#floating-block-elements)
- [Using Display: Inline](#using-display-inline)
- [Using Display: Inline-Block](#using-display-inlineblock)
  * [Usage](#usage)
  * [Differences from Floats](#differences-from-floats)
  * [Compatibility](#compatibility)
- [The Box Model Component: Margin, Border, Padding and Content](#the-box-model-component-margin-border-padding-and-content)
  * [The Box Model and Box Sizing](#the-box-model-and-box-sizing)
- [Best Practices for Using the Box Model](#best-practices-for-using-the-box-model)
- [Application of acquired knowldge](#application-of-acquired-knowldge)
- [Conclusion](#conclusion)

{%- # TOC end -%}


## What is the CSS Box Model?
The CSS box model refers to the rectangular boxes that are generated for elements in CSS. Each box has a content area (where the content is displayed) as well as optional border, and margin areas.
The content area is the innermost part of the box that contains the actual content. Padding is the space around the content. When you set padding, it will push the border and margin outward.
The border area is the area around the padding (if any) and content. Borders sit on top of the padding and content area and separate the margin area from the padding area.
The margin area is the outermost layer that surrounds the border. It separates the element from surrounding elements. Margins are transparent, so they allow you to see the background color and background images of the element behind them.
 The box model allows us to add spacing and borders around elements in web pages. A basic understanding of the box model is essential for any web developer to style pages effectively.
By default, the width and height you assign to an element only apply to the content are. If you add padding, borders, and margins, it will increase the total size of the box. This is known as box sizing. There are two main box sizing models:

1. Content-box: The width and height only apply to the content area. Padding, border, and margin are added outside the content area.
2. Border-box: The width and height apply to the content, padding, and border. Margin is added outside. This is the box model used by most browsers today.

To change the box sizing model, use the box sizing property. For example:
``.element{
box-sizing: border-box;
}
``
This will set ``.element`` to use the border-box model.
## Block Level Boxes vs Inline Boxes
Block elements occupy the full width of their parent element, stacking on top of each other. Inline elements only take up the space necessary for their content, sitting on the same line. Understanding how these box models work is key to mastering CSS layouts.
Block level boxes have some important characteristics:

1. They force line breaks before and after the element.
2. They occupy the full width of their parent element.
3. Margins and padding apply to the element but do not collapse with margins and padding of inline boxes.
4. The width and height properties are respected.

Some common block level elements are:

``<p>,<div>, <li>, <nav>, <ul>, <ol>, <nav>, <h1> - <h6>, <article>, <footer>, <section>``

Inline boxes, on the other hand have different behaviors:

1. They do not force line breaks.
2. They only take up the space necessary for their content.
3. Margins and padding apply but collapse with adjacent inline boxes.
4. The width and height properties are ignored.

Some common inline elements are:
``<a>, <i>, <span>, <button>, <input>, <textarea>, <select>,<time>, <img>``

Understanding block vs inline box models is essential for any web developer. Mastering their differences will give you full control over how elements are laid out on the page.
## Using Display: Block
``display: block`` is a powerful tool in CSS for controlling layout, When an element is set to ``display: block``, it takes up the full width of its parent container. Each block element starts on a new line, and will stack vertically with other block elements.
### Using Margin and Padding
You  can add spacing around block elements using the margin and padding properties. The margin adds spacing outside the element while the padding adds spacing inside. For example:
``css
.block{
display: block;
width: 200px;
margin: 20px;
padding: 10px;
}
``
This will give the ``.block`` element 20px of margin on all sides, and 10px of padding on all side, creating a total of 40px of space between .block elements.
### Controlling Width
Block elements will expand to fill the width of their parent container by default. You can give a block element a fixed width using the width property. For example:
``css
.block{
display: block;
width: 600px;
{
``
This will make ``.block`` 600px wide. If you don’t give a block element a width, it will expand to fill its parent container.
### Floating Block Elements
You can float block elements left or right using the ``float`` property. A floated element will move to the left or right, allowing inline content and block elements to wrap around it. For example:
``css
.block{
display: block;
float: left;
width: 200px;
}
``
This will float ``.block`` left, allowing text and other elements to display to the right of it.
In summary, the ``display: block`` property gives you a lot of flexibility and control over layout in CSS. Using it in combination with other properties like ``width``, ``margin``, ``padding`` and ``float`` will allow you to create complex layouts and designs. With some practice, you will be mastering the box model in no time! 
## Using Display: Inline
When working with CSS, understanding how to properly use the display property is essential. The display property specifies the type of box used for an element. It has many possible values, but two of the most common are block and inline.
Block level elements occupy the full width of their parent element. They always start on a new line and stack on top of each other.
Inline elements only occupy the space bounded by the tags defining the element. They do no start on a new line and only take up as much width as necessary.
To change an element to a block or inline, you use the display property:
``css
.div{ 
display: block;
}
.span{
display: inline;
}
``
Setting an element to ``display: inline`` will make it act like an inline element, while ``display: block`` will make it act like a block element.
Some elements, like ``<a>``, are inline by default but are commonly used as block elements. To do this, you just set ``display: block; ``as shown below:
``css
a{
display: block;
}
``
Now the ``<a>`` element will occupy the full width of its parent and start on its own line.
Using the display property strategically in your CSS allows you to manipulate the default behaviors of elements and build complex layouts. Understanding block vs inline is a foundational concept to mastering CSS.
## Using Display: Inline-Block
``display: inline-block ``allows block elements to sit next to each other, like inline elements do, while still retaining all the features of block elements. This is useful when you want to have block elements side by side without adding floats.
### Usage
To use inline-block, simply set the display property to ``inline-block``:
``css
.element {
display: inline-block;
}
``
This will allow the element to behave like an inline element (sitting next to other elements), while still retaining features of a block element (setting ``width``, ``height``, ``margin``, ``padding``, etc).
### Differences from Floats
Using ``inline-block`` is different from using floats in a few key ways:

- Inline-block elements will not wrap to the next line like floats will. They will stay on the same line until there is no more space left.
- Margins and paddings are respected between inline-block elements. With floats, margins and paddings are collapsed.
- Inline-block elements respect vertical-align. You can set vertical-align: top, middle, bottom to align inline-block elements relative to the line box.
- The width of an inline-block element will shrink-wrap to its content. With floats, the width is determined by the width of the float container.

### Compatibility
Inline-block is supported in all major browsers. In cases of non-supported browsers, you will need to apply ``display: inline`` to the element and the add ``display: block`` to a child element.
Using inline-block is an easy way to layout blocks in a row without having to use floats. It gives you more flexibility and control over styling and alignment compared to floats. By understanding the differences between the two, you can determine which method is used for your particular use case.

## The Box Model Component: Margin, Border, Padding and Content
The CSS box model consists of four components that determine the space an element takes up on the page:

- Margin: The transparent area surrounding the border. It separates the element from other elements and creates negative space.
- Border: The line surrounding the padding and content. It separates the element from other elements. 
- Padding: The transparent space between the border and the content. It separates the content from the border.
- Content: The actual content of the element, such as text, images, or videos.
Below is a diagrammatic illustration of the Box model components:

![CSS browser box model by angelocodes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eko7ltyx9ca61rwt5oa9.png)

In the illustration above, the light blue colored section is the Content section. For the above case, it had the dimension indicated above
To understand how the box model works, think of a gift box. The actual present inside is the content. The tissue paper surrounding it is the padding. The decorative wrapping paper is the border. And the empty space between the gift and other gifts is the margin.
The box model allows us to add spacing and borders around elements in a consistent way. Its important to note that the total width of an element is the width of the content plus the padding, border, and margin. This is known as box sizing and can cause issues if you are not aware of it.
### The Box Model and Box Sizing
By default, browsers add the padding, border, and margin to the width you specify. For example, if you set an element to have: 
``width: 200px
padding: 20px
border: 5px
margin: 15px``
The actual rendered width of the element will be 260px (200px + 20px padding + 10px border + 15px margin on each side).
To fix this issue, you can set the box-sizing property to border-box. This will make the width you set equal to the content width plus the padding and border, and will not add the margin to the final rendered width.
Using the box model and box sizing property will allow you to create well-spaced and responsive layouts.
Understanding how components like margin, border, padding, and content interact with each other is key to mastering CSS.
## Best Practices for Using the Box Model
When working with the CSS box model, following best practices will help you build web pages efficiently and consistently. Below are the best practices that have helped me as well as many other CSS users to build magnificent layouts:

- Use classes instead of IDs when possible. Classes allow you to reuse styles on multiple elements, keeping your code DRY (Don’t Repeat Yourself). Only use IDs when you want to identify elements uniquely.
- Keep selectors short and specific. Overly long selectors can be hard to read and navigate. Use direct child selectors (>) instead of descendant selectors(space) when possible. For example, use ``.parent > .child`` instead of ``.parent .child``. 
- Use padding instead of margins for spacing content. Padding adds space within element’s border, while margins add  space around an element’s border. So when you want to push content away from the element’s border, its best to use padding.
- Define width for fluid layouts. When using % units for widths, also define a max-width to prevent elements from becoming too wide on every large screens. For example:

``.container {
width: 80%;
max-width: 1200px;
}``

- Use comments to organize rules. Add comments to logically group similar rules together. For example:

``css
/ Typography /
h1, h2, h3 {
font-family: sans-serif;
}
/ Forms /
input, textarea {
border: 1px solid #ddd;
padding: .5rem;
}``

- Use variables for reused values. If you use the ssame color, font, or other value repeatedly, define it as a variable. This makes it easy to change in the future. For example:

``css
      :root {
 --main-color: #04a4cc;
 }
 header {
 background-color: var(--main-color);
 }
 .button {
 background-color: var(--main-color);
 }``

Following the above practices will make your CSS code easier to navigate, reuse and maintain. 
## Application of acquired knowldge
In application of the knowledge acquired in in this article, we are going to design a review section for a product. 
Below are the HTML and CSS codes for the review section;
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="reviews.css">
    <title>Reviews</title>
</head>
<body>
    <div class="review-card-container">
        <div class="review-card">
            <h3>Great Product</h3>
            <p class="rating">⭐⭐⭐⭐⭐</p>
            <p>This product exceeded my expectations. Its incredibly versatile and easy to use</p>
            <p>Ajika Angelo</p>
        </div>

        <div class="review-card">
            <h3>Highly recommended</h3>
            <p class="rating">⭐⭐⭐⭐⭐</p>
            <p>I have tried out very similar products but this one stands out. Worth every penny</p>
            <p>Orieda Pius</p>
        </div>

        <div class="review-card">
            <h3>Good Value</h3>
            <p class="rating">⭐⭐⭐⭐</p>
            <p>This product offers great features at a reasonable price. Minor improvements</p>
            <p>Nathan Onwang</p>
        </div>
    </div>   
</body>
</html>
```
The inline elements used here are ``<h3>``, ``<p>`` and ``<p class=”rating”>``. The ``review-card`` elements are also block elements. 
 


The CSS code:
```/*css*/
.review-card-container{
    text-align: center;

}

.review-card{
    display: inline-block;
    width: 300px;
    margin: 0 10px;
    vertical-align: top;
    border: 1px solid #ccc;
    background-color: #f9f9f9;
    box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
    padding: 20px;
    margin-bottom: 20px;
    }
    
.review-card h3 {
    margin-top: 0;
    font-size: 1.2em;
}

.review-card p {
    margin: 10px 0;
}

.rating {
    color: #ff9900;
    font-size: 1.5em;
}
```
From the above code snippet, ``padding``  property is used to create space inside the ``.review-card`` element, pushing the content away from the edges of the element.
The ``margin`` property is used to create space around each ``.review-card`` element, providing spacing between the card themselves.
The ``border`` property adds a border around each ``.review-card`` element, visually separating it from other elements.
The ``.review-card`` elements are set to ``display: inline-block``. This allows them to align horizontally in a row similar to inline elements, while still maintaining their characteristics.
Below is the output:

![Responsive product card using CSS box model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptxeqnd7evdsaftf6mtb.png)

The above output is responsive due the combination of CSS properties like flexible width and the default behavior of block-level and inline-block elements.
Responsive illustration:

![Responsive product card using CSS box model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4xjjckf6p1lcs1ygg8ly.png)
## Conclusion
As you have learnt, the box model in CSS defines how elements are displayed on the page and how they interact with other elements. Understanding block and inline elements and how to manipulate them is core to mastering CSS layouts. With practice, you will find yourself intuitively reaching for block or inline elements to achieve your desired layout results. Pay close attention to ``margin``, ``border``, and ``padding`` and how they affect the size and position of elements. Use the technique discussed to create professional, polished web designs and layouts. You now have a solid foundation in the box model --- Keep practicing and continue honing your CSS skills. The possibilities for creativity are endless.
For more advanced knowledge in creation and design of CSS layouts, I do recommend you to embark on the journey of Using CSS Flexbox and CSS Grid.