Article image Box model and padding, border and margin

11. Box model and padding, border and margin

Page 57 | Listen in audio

Complete HTML, CSS and Javascript course

Chapter 11: Box Model and Padding, Border and Margin

The Box Model is one of the most fundamental parts of CSS, as it controls the design and layout of many aspects of a web page. Each element on a web page is considered a "box" and that box can have different properties, such as width, height, padding, borders, and margins.

Box Model

The Box Model is a representation of how each element is rendered on the page. The 'box' of an element includes the element's content, padding, border, and margin. Content is the text, image, or anything else inside the element. Padding is the space between the content and the border. The border is a line that surrounds the content and padding. The margin is the space between the border and neighboring elements.

To view the Box Model, you can use the browser's inspect tool. This will show the content width and height, padding, border, and margin of an element.

Padding

Padding is the space between the content of an element and its border. You can set the padding for all sides at once using the 'padding' property, or you can set each side individually using 'padding-top', 'padding-right', 'padding-bottom' and 'padding-left'.

For example, if you wanted to add 10px padding to the top of an element, you would use 'padding-top: 10px;'. If you wanted to add 10px padding to all sides, you would use 'padding: 10px;'. The fill is transparent, so the background of the element will be visible through it.

Border

A border is a line that surrounds the padding and content of an element. You can set the border width, style and color using the 'border-width', 'border-style' and 'border-color' properties respectively. You can also set all three properties at once using the 'border' property.

For example, if you wanted a 1px wide solid border around an element, you would use 'border: 1px solid;'. If you wanted this border to be red, you would use 'border: 1px solid red;'. You can also set the border properties for each side individually using 'border-top', 'border-right', 'border-bottom' and 'border-left'.

Margin

Margin is the space between the edge of an element and the elements around it. You can set the margin for all sides at once using the 'margin' property, or you can set each side individually using 'margin-top', 'margin-right', 'margin-bottom' and 'margin-left'.

For example, if you wanted to add 10px margin to the top of an element, you would use 'margin-top: 10px;'. If you wanted to add 10px margin to all sides, you would use 'margin: 10px;'. The margin is transparent, so anything behind the element will be visible through it.

Understanding the Box Model is crucial to being able to create complex and responsive layouts. It's one of the first things you should learn when starting out with CSS, and it will be a valuable tool in your arsenal as a front-end developer.

Now answer the exercise about the content:

In the context of CSS, what is the Box Model?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Colors and backgrounds in CSS

Next page of the Free Ebook:

58Colors and backgrounds in CSS

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text