The CSS Box Model

The box model is a basic CSS concept that explains how HTML elements are structured and displayed on a web page while also giving full details on how they interact with each other. Regarding the explanation of the structure of HTML elements, the CSS box model describes every HTML as a rectangular box made up of four components.

These four components include:

  1. CONTENT: This is simply the information that is contained within an HTML element. It could be in the form of text, images or any other media.

  2. BORDER: The border is a decorative line that surrounds the content. It creates some sort of boundary to separate elements from each other.

  3. PADDING: This is simply the space between the content and its border.

  4. MARGIN: This is instead the space between an element’s border and other surrounding elements.

Padding and Margin are such important CSS properties because they make an element’s content easier to read and more visually appealing thus improving readability and organization of the elements on a web page. While it’s possible to give the padding property a value, other properties can be used to control the amount of space between content and its border. They are the padding-top, padding-right, padding-bottom, and padding-left properties. The same goes for the margin property where we have the margin-top, margin-right, margin-bottom, and margin-left properties that can also be used to control the amount of space between an element's border and other elements on the page.

BOX-SIZING.

Box-sizing is a CSS property that deals with how the total width and height of an element are calculated concerning its padding and border values. When laying out elements in CSS, it’s common to make use of the box-sizing property as layout is important in dealing with the sizes of HTML elements.

There are a few values that could be attached to the box-sizing property but I’ll be focusing on just two of them. They are:

  1. CONTENT-BOX: This value is the default CSS box-sizing behaviour and this behaviour is such that whatever height and width assigned to an element is applied only to the content box. This implies that the width and height of the element are set independently of the border and padding. In simpler terms, I’m saying that the eventual size you get to view on the screen is the width and height plus the border and padding.

*{

box-sizing: content-box;

}

  1. BORDER-BOX: The border-box value is different simply because it factors the border and padding in the values specified for an element’s width and height. In simpler terms, I’m saying that if you specify the width and height of an element to be 50px, the border and padding will be factored into the specified value for width and height (in this case, 50px).

*{

box-sizing: border-box;

}

It’s common practice among developers to set the value of the box-sizing property to border-box because it enhances easier sizing and layouts, more predictable behaviour, consistent box model and also better control over space usage thus bringing about more intuitiveness with layouts.

Below is an example of how the CSS box model can be used to create a centred box with padding and margin:

.centered{

width: 400px;

height: 200px;

margin: 0 auto;

padding: 20px;

border: 1px solid black;

}