visit
Before we dive deeper, everyone needs to understand that every element in web design is a rectangular box. You have probably heard this multiple times before, but this is an important concept that every developer should be aware of.
Now, let’s explain the mysterious box model!<div class=”box”>Lorem ipsum dolor amet whatever woke cronut, farm-to-table church-key tousled edison bulb. </div>
.box {
background-color: hotpink;
color: #fff;
}
To refresh your memory, the difference between inline and block elements is the fact that block elements take up 100% of the container width, while inline elements only take up the amount of space that the content needs.
When using inline elements it is not possible to set a fixed width or height for that element, since the element doesn’t have any predetermined width and height (because the width and height are determined by the content). This can be overcome by converting the element to a block element.
Unlike inline elements, when using block-level elements you can easily set a fixed width or height for it. Since by default the block level elements take up 100% of the container width, we can easily override it by setting a fixed width.
You can also covert your element to inline-block. When using inline-block, the element has the behavior of the inline element (only take up the space of content), but you can manipulate it the same way as you can do with a block element.
Now when we have a block level element we can give it a width and height..box {
height: 200px;
width: 200px;
background-color: hotpink;
color: #fff;
}
.box {
height: 200px;
width: 200px;
background-color: hotpink;
color: #fff;
padding: 10px;
}
In the image we see how padding affects the overall look of the box. There is space of 10px between the content and edge of the box on all four sides. We can also add padding to every side individually, using padding-top, padding-bottom, padding-left, padding-right.
.box {
height: 200px;
width: 200px;
background-color: hotpink;
color: #fff;
padding: 10px;
border: solid 3px black;
}
<div class=”box”></div>
<div class=”box”></div>
.box {
height: 200px;
width: 200px;
background-color: hotpink;
color: #fff;
padding: 10px;
border: solid 3px black;
margin: 0
}
.box {
margin: 20px;
}
Now, this looks better. We added some space between the boxes. We can also add space on every side of the element individually using margin-top, margin-bottom, margin-left or margin-right.
Stay tuned, because in the next article we will talk about another very important property that helps you calculate the width of the box model, the box-sizing property.
Thanks for reading!//kolosek.com/css-box-model-for-beginners/