SlideShare a Scribd company logo
7
Most read
8
Most read
11
Most read
CSS Box Model
What is the Box Model?
◌ Each XHTML element appearing on our page takes up a "box" or
"container" of space.
◌ Each box size is affected not only by content but also by padding,
borders, and margins.
◌ By knowing how to calculate the dimensions of each box, we can
accurately predict how elements will lay out on the screen.
◌ As we build a new page, we can arrange these boxes on the screen,
creating a balanced layout with white space around the content.
The importance of the box model concept cannot be overemphasized. It
would be difficult and frustrating to create a website without understanding
this concept.
The box model is a tool we use to understand how our
content will be displayed on a web page.
Box Components
Each element on the page has the following components:
Margin
Border
Content
Padding
The easiest way to understand these components is to use one of the most
versatile tools available to us as web designers: the <div> element.
Introducing the <div> Element
The <div> ("division") element groups other elements on the
screen.
◌ By setting the width and height attributes via CSS, we can reserve
a precise amount of space on our page for specific content.
◌ The actual content is nested and contained within the opening <div>
and closing </div> tags.
◌ When we apply CSS styling directly to the <div> element, all the
elements contained within that <div> will inherit that style.
◌ By using multiple <div> elements as building blocks, we can design
an entire web page layout.
Let's create a box on the screen and add a border around it:
Example <div> Element
<style type="text/css">
.box200 {
width: 200px;
height: 200px;
border: 1px solid black;
color: blue;
}
</style>
...
This is before the box.
<div class="box200">
This is a 200 by 200 pixel box with
a 1px border.
</div>
This is after the box.
...
With the border set, we can see the
exact space taken up on the page.
Notice that there is almost no space separating the text from the box border. Elements
such as paragraphs, headers, and lists automatically insert padding and margins, but
plain text does not do so.
Let's create some space between elements by adding both padding and
margin:
Adding Padding and Margin
<style type="text/css">
.box200 {
width: 200px;
height: 200px;
border: 1px solid black;
color: blue;
padding: 10px;
margin: 10px;
}
</style>
...
The 10 pixel padding adds buffer space, on all four sides, between the
content and border. The 10 pixel margin adds buffer space, on all four sides,
between the border and surrounding elements.
Let's examine this a bit closer up to see exactly what is happening.
Padding and Margin Illustrated
The dotted red line shows
the margin's outer boundary
and the dotted green line
shows the padding's inner
boundary.
242px high
242px wide
When we define the width
and height of a <div>
element, these dimensions
apply only to the actual
content, not to the padding,
border, or margin.
Calculating Total Dimensions
When we are planning our page, we have to calculate exactly how much
screen space a <div> or any other element will use. The formula is:
◌ Total element width = defined width + left padding + right padding + left
border + right border + left margin + right margin.
◌ Total element height = defined height + top padding + bottom padding + top
border + bottom border + top margin + bottom margin.
In our previous example:
◌ Total <div> width = 200px + 10px + 10px + 1px + 1px + 10px + 10px = 242px.
◌ Total <div> height = 200px + 10px + 10px + 1px + 1px + 10px + 10px = 242px.
Padding, borders, and margins do not have to be the same on all four sides,
as they are in this example. Let's see how to set these individually.
Setting Individual Margins
We can set the specific margin sizes by using these four properties:
Sets all four margins to be 10px.
margin-top:
margin-right:
margin-bottom:
margin-left:
In practice, few web designers use these properties, preferring a
shorthand method that condenses the declaration to a single line:
margin: 20px 10px 5px 15px;
Sets the top and bottom margins as 10px
and the left and right margins as 5px.
Sets the top margin as 20px, the right
margin as 10px, the bottom margin as
5px, and the left margin as 15px.
margin: 10px 5px;
margin: 10px;
Setting Individual Padding
We can set specific padding sizes by using these four properties:
Sets padding as 5px on all four sides.
padding-top:
padding-right:
padding-bottom:
padding-left:
Again, these are rarely used. Instead, the same shorthand method is
employed:
padding: 50px 10px 15px 5px;
Sets top and bottom padding as 25px and
left and right padding as 5px.
Sets top padding as 50px, right padding
as 10px, bottom padding as 15px, and
the left padding as 5px.
padding: 25px 5px;
padding: 5px;
Setting Individual Borders
Customizing borders is more involved. Since they are visible on the
page, we have to specify style, thickness, and color. Accordingly, there
are more properties available:
border-top-width:
border-right-width:
border-bottom-width:
border-left-width:
border: 5px solid blue;
border-top-style:
border-right-style:
border-bottom-style:
border-left-style:
border-top-color:
border-right-color:
border-bottom-color:
border-left-color:
To make things easier, we can use the shorthand method as before:
border-width: 10px;
border-style: solid dashed;
border-color: blue red orange gray;
If the three border properties will be identical on all four sides, we can use
a single-line border shorthand, as we did in an earlier lesson:
Here we have set custom padding, borders, and margins:
Example of Customized Settings
<style type="text/css">
.box200 {
width: 200px;
height: 200px;
color: blue;
padding: 40px 10px 0px 10px;
margin: 25px 5px;
border-width: 2px 5px;
border-style: solid dotted;
border-color: red blue green maroon;
}
</style>
A helpful way to remember the order of shorthand settings is that it
starts at noon and goes clockwise around: top, right, bottom, left.

More Related Content

PDF
GDI Seattle Intro to HTML and CSS - Class 3
PDF
Web Layout
PPTX
Chapter 14: Box Model
PPTX
Lecture 6.pptx..........................
PPTX
Cascading style sheets (CSS-Web Technology)
PPT
5.1 css box model
KEY
Css Essential
PPTX
Lecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
GDI Seattle Intro to HTML and CSS - Class 3
Web Layout
Chapter 14: Box Model
Lecture 6.pptx..........................
Cascading style sheets (CSS-Web Technology)
5.1 css box model
Css Essential
Lecture 10 CSS part 2.pptxvvvvvvvvvvvvvv

Similar to 11--CSS-Box-Model.pdf for second college (20)

PPTX
Css box-model
PPTX
CSS tutorial chapter 2
PPTX
Web Development - Lecture 6
PDF
CSS Foundations, pt 2
PDF
Dimensions of elements.pdf
PDF
Layout with CSS
PDF
Quarter 3_Lesson_One_CSSheets_Paddings.pdf
PPSX
CSS Box Model Presentation
PPTX
Pres
PDF
Box Model and Page Layouts
PPTX
Week 3
 
PPTX
Web Engineering - Basic CSS Properties
PDF
Lab#7 CSS Box Model
PPTX
Css margin and padding property
PDF
Css box model
PPT
Box Model
PPTX
CSS Cascade Style Sheet
PPT
Advance Css 1194323118268797 5
PPT
Advance Css
PDF
Margin vs Padding.pdf
Css box-model
CSS tutorial chapter 2
Web Development - Lecture 6
CSS Foundations, pt 2
Dimensions of elements.pdf
Layout with CSS
Quarter 3_Lesson_One_CSSheets_Paddings.pdf
CSS Box Model Presentation
Pres
Box Model and Page Layouts
Week 3
 
Web Engineering - Basic CSS Properties
Lab#7 CSS Box Model
Css margin and padding property
Css box model
Box Model
CSS Cascade Style Sheet
Advance Css 1194323118268797 5
Advance Css
Margin vs Padding.pdf
Ad

More from shwan it (12)

PPTX
created appliciatons with Ai programming
PPTX
Ai created application by used Ai program
PDF
CH 1 for computer since write for code .pdf
PDF
Lec2- for c++ code Video Display Devices.pdf
PDF
02_Configure_a_Network_Operating_System.pdf
PDF
8--CSS-Classes-and-IDs.pdf for second to college
PDF
web design for html to second yea for college
PPTX
Blue Futuristic Illustrative Artificial Intelligence Project Presentation.pptx
PPTX
Lecture 02 - Logic Design(Number Systems).pptx
PDF
CS Lectures 1maher its sciences computer
PDF
Konsbruck.pdf
PDF
html5-tag-cheat-sheet-2019.pdf
created appliciatons with Ai programming
Ai created application by used Ai program
CH 1 for computer since write for code .pdf
Lec2- for c++ code Video Display Devices.pdf
02_Configure_a_Network_Operating_System.pdf
8--CSS-Classes-and-IDs.pdf for second to college
web design for html to second yea for college
Blue Futuristic Illustrative Artificial Intelligence Project Presentation.pptx
Lecture 02 - Logic Design(Number Systems).pptx
CS Lectures 1maher its sciences computer
Konsbruck.pdf
html5-tag-cheat-sheet-2019.pdf
Ad

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Cost to Outsource Software Development in 2025
PPTX
assetexplorer- product-overview - presentation
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
System and Network Administration Chapter 2
PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Cost to Outsource Software Development in 2025
assetexplorer- product-overview - presentation
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
System and Network Administration Chapter 2
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Upgrade and Innovation Strategies for SAP ERP Customers
Softaken Excel to vCard Converter Software.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Wondershare Filmora 15 Crack With Activation Key [2025

11--CSS-Box-Model.pdf for second college

  • 2. What is the Box Model? â—Œ Each XHTML element appearing on our page takes up a "box" or "container" of space. â—Œ Each box size is affected not only by content but also by padding, borders, and margins. â—Œ By knowing how to calculate the dimensions of each box, we can accurately predict how elements will lay out on the screen. â—Œ As we build a new page, we can arrange these boxes on the screen, creating a balanced layout with white space around the content. The importance of the box model concept cannot be overemphasized. It would be difficult and frustrating to create a website without understanding this concept. The box model is a tool we use to understand how our content will be displayed on a web page.
  • 3. Box Components Each element on the page has the following components: Margin Border Content Padding The easiest way to understand these components is to use one of the most versatile tools available to us as web designers: the <div> element.
  • 4. Introducing the <div> Element The <div> ("division") element groups other elements on the screen. â—Œ By setting the width and height attributes via CSS, we can reserve a precise amount of space on our page for specific content. â—Œ The actual content is nested and contained within the opening <div> and closing </div> tags. â—Œ When we apply CSS styling directly to the <div> element, all the elements contained within that <div> will inherit that style. â—Œ By using multiple <div> elements as building blocks, we can design an entire web page layout.
  • 5. Let's create a box on the screen and add a border around it: Example <div> Element <style type="text/css"> .box200 { width: 200px; height: 200px; border: 1px solid black; color: blue; } </style> ... This is before the box. <div class="box200"> This is a 200 by 200 pixel box with a 1px border. </div> This is after the box. ... With the border set, we can see the exact space taken up on the page. Notice that there is almost no space separating the text from the box border. Elements such as paragraphs, headers, and lists automatically insert padding and margins, but plain text does not do so.
  • 6. Let's create some space between elements by adding both padding and margin: Adding Padding and Margin <style type="text/css"> .box200 { width: 200px; height: 200px; border: 1px solid black; color: blue; padding: 10px; margin: 10px; } </style> ... The 10 pixel padding adds buffer space, on all four sides, between the content and border. The 10 pixel margin adds buffer space, on all four sides, between the border and surrounding elements. Let's examine this a bit closer up to see exactly what is happening.
  • 7. Padding and Margin Illustrated The dotted red line shows the margin's outer boundary and the dotted green line shows the padding's inner boundary. 242px high 242px wide When we define the width and height of a <div> element, these dimensions apply only to the actual content, not to the padding, border, or margin.
  • 8. Calculating Total Dimensions When we are planning our page, we have to calculate exactly how much screen space a <div> or any other element will use. The formula is: â—Œ Total element width = defined width + left padding + right padding + left border + right border + left margin + right margin. â—Œ Total element height = defined height + top padding + bottom padding + top border + bottom border + top margin + bottom margin. In our previous example: â—Œ Total <div> width = 200px + 10px + 10px + 1px + 1px + 10px + 10px = 242px. â—Œ Total <div> height = 200px + 10px + 10px + 1px + 1px + 10px + 10px = 242px. Padding, borders, and margins do not have to be the same on all four sides, as they are in this example. Let's see how to set these individually.
  • 9. Setting Individual Margins We can set the specific margin sizes by using these four properties: Sets all four margins to be 10px. margin-top: margin-right: margin-bottom: margin-left: In practice, few web designers use these properties, preferring a shorthand method that condenses the declaration to a single line: margin: 20px 10px 5px 15px; Sets the top and bottom margins as 10px and the left and right margins as 5px. Sets the top margin as 20px, the right margin as 10px, the bottom margin as 5px, and the left margin as 15px. margin: 10px 5px; margin: 10px;
  • 10. Setting Individual Padding We can set specific padding sizes by using these four properties: Sets padding as 5px on all four sides. padding-top: padding-right: padding-bottom: padding-left: Again, these are rarely used. Instead, the same shorthand method is employed: padding: 50px 10px 15px 5px; Sets top and bottom padding as 25px and left and right padding as 5px. Sets top padding as 50px, right padding as 10px, bottom padding as 15px, and the left padding as 5px. padding: 25px 5px; padding: 5px;
  • 11. Setting Individual Borders Customizing borders is more involved. Since they are visible on the page, we have to specify style, thickness, and color. Accordingly, there are more properties available: border-top-width: border-right-width: border-bottom-width: border-left-width: border: 5px solid blue; border-top-style: border-right-style: border-bottom-style: border-left-style: border-top-color: border-right-color: border-bottom-color: border-left-color: To make things easier, we can use the shorthand method as before: border-width: 10px; border-style: solid dashed; border-color: blue red orange gray; If the three border properties will be identical on all four sides, we can use a single-line border shorthand, as we did in an earlier lesson:
  • 12. Here we have set custom padding, borders, and margins: Example of Customized Settings <style type="text/css"> .box200 { width: 200px; height: 200px; color: blue; padding: 40px 10px 0px 10px; margin: 25px 5px; border-width: 2px 5px; border-style: solid dotted; border-color: red blue green maroon; } </style> A helpful way to remember the order of shorthand settings is that it starts at noon and goes clockwise around: top, right, bottom, left.