Simplifying CSS Box Model
Simplifying CSS Box Model
SIMPLIFYING
CSS
g. Margin Area………………………….…………..….….…..…16
h. Margin Collapsing…….……………………………….……..20
m. Solutions .……………………………………………….……28
How is a web page rendered?
CSS Box Model is used to describe the size of these rectangular boxes
which represent each web page element.
Page | 1
What is CSS Box Model?
Page | 2
Culture section further has a heading on the left side and a block of 3
cards on the right. All these components also have a box wrapped
around them.
Above image shows the rectangular box for the side heading and below
image shows the rectangular box of the “culture” cards block.
Page | 3
Here above image shows the rectangular box of each “culture”
card. In this way, every component of a web page can be broken
down into boxes.
• All these rectangular boxes are defined with CSS Box Model.
• They define the space occupied by an element in a page
layout and can be arranged to make complex page layouts
also.
Page | 4
Structure of CSS Box Model
Page | 5
Content Area
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia cum,
alias facilis aperiam, tenetur ad omnis accusantium fugiat saepe culpa?
Voluptate molestias, cum nulla soluta nisi maxime ullam maiores
blanditiis!
</article>
article {
border: 2px solid purple;
padding:1em;
}
Page | 6
For this <article> tag since no specific width & height were mentioned in
CSS, browser will take up as much space is required to show the content
inside <article> tag.
article {
border: 2px solid purple;
padding:1em;
width:150px;
height:150px;
}
Page | 7
On adding width & height to <article>, we
notice that since the content was too large
to fit in the box, it has overflowed out of the
border of <article>.
This is known an extrinsic sizing when
we ourselves mention the width and
height of an element with CSS
properties.
• Use min-content value for width : this keyword will assign the
minimum width required to fit in the content.
• Use overflow property to add scrollbars or hide the overflowing
content.
• Use text-overflow property add ellipsis to show that some content is
overflowed.
Page | 8
Padding Area
• Padding area is the space between the content area and the
border.
• It provides a breathing space to content and hence makes it easier
to read and the design cleaner.
• It is defined by padding property. Padding doesn’t take negative
values.
article{
padding-top: 20px;
padding-right: 35px;
padding-bottom: 50px;
padding-left: 45px;
}
Page | 9
Border Area
• Border area is the space between padding area and margin area.
• It is where the border of an element is displayed using border
property.
CSS provides properties to style color, width and choose a type of border
for each side.
article{
border-top-width: 8px;
border-top-color: green;
border-top-style: dotted;
}
Shorthand
border-top property is shorthand for styling width, color and adding
type of border with just one property.
article {
border-top: 8px dotted green;
}
Page | 10
Styling Bottom Border
Bottom border can be styled using its shorthand and longhand versions.
Longhand
article{
border-bottom-width: 5px;
border-bottom-color: blue;
border-bottom-style: dashed;
}
Shorthand
border-bottom property is shorthand for styling width, color and adding
type of border with just one property.
article {
border-bottom: 5px dashed blue;
}
article{
border-left-width: 5px;
border-left-color: pink;
border-bottom-style: solid;
}
Page | 11
Shorthand
border-left property is shorthand for styling width, color and adding type
of border with just one property.
article {
border-left: 5px solid pink;
}
Shorthand
border-right property is shorthand for styling width, color and adding
type of border with just one property.
article {
border-right: 5px double orange;
}
Page | 12
Different Border Styles
Page | 13
border-style : outset border-style : ridge
Page | 14
Styling Border for all sides
To apply similar styles to all border sides – we have border property.
Longhand
article{
border-width: 10px;
border-color: #5865F2;
border-style: double;
}
Shorthand
border property is shorthand for styling width, color and adding type of
border for all 4 sides with just one property.
article {
border: 10px double #5865F2;
}
Page | 15
Margin Area
Page | 16
For this example, I will wrap a <section> tag around the <article> tag →
<section class="container">
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Mollitia cum, alias facilis aperiam
</article>
</section>
.container {
border: 2px solid blue;
}
article{
width: 150px;
padding: 0.5em;
background-color: #FFCBA4;
}
Page | 17
Adding Left Margin
article{
margin-left: 30px;
}
article{
margin-left: -30px;
}
article{
margin-right: 45px;
}
article{
margin-right: -45px;
}
Page | 18
Adding Top Margin
article{
margin-top: 40px;
}
article{
margin-top: -40px;
}
article{
margin-bottom: 25px;
}
article{
margin-bottom: -25px;
}
Page | 19
Margin Collapsing
Page | 20
Case 2 : Between Parent and its children
I have a <section> which holds 2 paragraphs – first para has a margin-top
of 20px and margin-bottom of 30px. Second paragraph has a margin-
bottom of 30px.
<section> is given margin-top and margin-bottom of 20px.
Two more paragraphs are used → one above the <section> tag and one
below it.
Page | 21
Box Model for Inline Elements
Suppose, we have a <span> tag and apply all the box properties to it →
article{
border: 10px solid #B8860B;
padding: 20px;
width: 300px;
height: 80px; }
article{
border: 10px solid #B8860B;
padding: 20px;
width: 300px;
height: 150px;
box-sizing: border-box;
}
.middle{
outline: 20px solid #C8A2C8;
box-shadow: 0 0 15px 20px #ff0000;
}
Page | 25
Test Your Knowledge
a. Margin b. Padding
c. Outline d. Border
a. No b. Yes
c. May be d. Not sure
a. Padding b. Margin
c. Border d. None of the above
a. content-box b. inherit
c. padding-box d. border-box
a. border-right-style b. border-left-width
c. border-left-style d. border-right-width
Page | 26
6) Outline of an element appears in which part of Box Model?
a. overflow b. text-overflow
c. wrap d. word-spacing
a. Padding b. Border
c. Content d. Margin
a. Yes b. No
c. Sometimes d. May be
Page | 27
Solutions
1) c. Outline
2) a. No
3) a. Padding
4) d. border-box
5) c. border-left-style
6) b. Margin area
7) a. overflow
8) d. Margin
9) b. No
Page | 28
Author’s Note
I hope this ebook was helpful to you and my explanation could help you
understand CSS Box Model in simpler terms.
Read my blogs on →
c. Medium : https://medium.com/@ritikaagrawal08
Page | 29