×

Css Box Model

Introduction

The Box Model property of CSS is used for design and layout of an html page. In CSS, all the elements has a rectangular box around it, you have to understand that to align the elements or to create layouts.

CSS Box Model is an area around the item which has various assets such as padding, margin and border. It acts as a set of tools to customize the design of various elements.

Width and Height of the element:

The width and height of an html element may vary according to the different browsers, thus you need to study the box model to understand how to assign values to the width and the height.

Note that when you set width and height of an element using CSS, it just sets the width and height of the content area. To personalize the element completely, padding, borders and margins must be used.

CSS Box Model

The CSS Box Model consists of following properties:

  • Border
  • Margin
  • Padding
  • Content

Note: When the padding or border values are not declared, they are either zero (css reset) or the default value of browser.

  • Border box:
    This is the region around the content, between the margin and padding of the box. When the standard box model is used, size of border is assigned to width and height of the box.
    There are a large number of properties of border for border styling. Also there are four borders and you can manipulate the color, style and width of each border.
    To set the color, style and width of all four border at once you can use border property.

Following are the properties to set each side of the border individually:

  • border-top
  • border-right
  • border-bottom
  • border-left

Following are the properties used to set color, style and width of all sides of the border:

  • border-color
  • border-style
  • border-width

It is also possible to set the width, style or color of single side using the following longhand properties:

  • border- <side> -width
  • border- <side> -style
  • border- <side> -color

Where the <side> may be top, right, bottom or left.

Example:

<!DOCTYPE html>
<head>
<title>CSS Box Model</title>
<style>
 .container{
font-size: 25px;
font-family: arial;
width: 500px;
height: 500px;
}
.box{
margin: 30px 20px;
height: 60px;
/*to set the width, style and color of all sides of the border*/
border: 12px solid skyblue;
border-right-width: 30px;  /*to set the width of right side*/
border-bottom-color: hotpink; /*to set the color of bottom side*/
border-top: 4px dotted skyblue;
}
</style>
</head>
<body>
<div class = "container">
<div class="box">
<center>CSS Box Model : Border</center>
</div>
</div>
</body>
</html>

Output:



CSS Box Model

Here, different properties of border box are used to display the border.

  • Margin box:
    It represents the outermost layer outside of the border. It is a transparent area around the box which lies between the border and the boundary of the window.
    The values of the margin can be positive and negative. When you set the margin as negative, one side of the box overlaps other elements on the page.
    To manipulate the margin of an element, you can use margin property. It changes the margin of all sides at once. And to change the margin of each side individually, you can use following longhand properties:

    • margin-top
    • margin-right
    • margin-bottom
    • margin-left

Example:

<!DOCTYPE html>
<head>
<title>CSS Box Model</title>
<style>
.container {
font-size: 25px;
font-family: arial;
width: 500px;
height: 300px;
border: 5px solid;
}
.box {
height: 60px;
border: 15px solid skyblue;
margin-top: 60px;
margin-right: -30px; /*negative value overlaps the outside border element*/
margin-bottom: 20px;
margin-left: 40px;
}
</style>
</head>
<body>
<div class = "container">
<div class="box">
<center>CSS Box Model : Margin</center>
</div>
</div>
</body>
</html>

Output:

CSS Box Model

Here, all the longhand properties of margin are used with different values. As we have given a negative value to the margin-right property, it overlaps the other element to its right.

  • Padding box:
    Padding is the space between the content area and the border box. To decide the proportions of the padding box, its height and width needs to be specified.
    Padding does not accept negative values like margins, so you should assign either zero or positive value to it. The main aim of using padding is to keep the content away from border with a particular distance provided.
    To manipulate the padding of an element, you can use padding property which changes the padding of all sides at once. And to change the margin of each side individually, you can use the following long hand properties:

    • padding-top
    • padding-right
    • padding-bottom
    • padding-left

Example:

<!DOCTYPE html>
<head>
<title>CSS Box Model</title>
<style>
.container {
font-size: 25px;
font-family: arial; 
width: 500px;
height: 300px;
margin-top: 40px; 
margin-left: 40px;
}
.box {  
height: 60px;
border: 15px solid skyblue;
padding-top: 30px;
padding-right: 20px;
padding-bottom: 80px;
padding-left: 0;
}
</style>
</head>
<body>
<div class = "container">
<div class="box">
        CSS Box Model : Padding
</div>
</div>
</body>
</html>

Output:

CSS Box Model

In the above output, all the longhand properties of padding are used with different values. The left-padding is set to 0, so there is no space between the left side of the border and the content.

  • Content box:
    Content box is the area which displays the content of the page. It may be an image, text, or other media. The size of the content can be manipulated using properties like width and height.

Example:

<!DOCTYPE html>
<head>
<title>CSS Box Model</title>
<style>
.container {
font-size: 25px;
font-family: arial; 
margin: 20px 20px20px20px;
width: 400px;
height: 300px;
border: 5px brown dotted;
}
.box {
height: 100px;
border: 15px solid skyblue;
margin: 60px 30px 30px 60px;
padding-top: 0;
padding-left: 20px;
}
</style>
</head>
<body>
<div class = "container">
<div class="box">
This is the content box of CSS Box Model</div>
</div>
</body>
</html>

Output:

CSS Box Model

Above output displays all the properties of Box Model i.e margin, padding, border and content.

To calculate the size of the box on a web page we use:

Size Properties
Width height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom
Height width + padding-left + padding-right + border-left + border-right + margin-left + margin-right

In this way, we have learnt the CSS Box Model and its various properties to manipulate the design and layout of an html page.


Related Topics

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Overflow

CSS Overflow: The Overflow property in CSS is used to manage the content if it overflows from its container of the block level. We have seen that all the elements...

3 minutes read.

Difference between HTML and CSS

HTML HTML stands for HyperText Markup Language and is used to create web pages and websites and web applications, which means we can create static web pages. Html is a scripting...

3 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Ellipsis

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. Here, we will discuss the following: What is CSS Ellipsis? Syntax for CSS ellipsis How to...

3 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

CSS Links

CSS Link is used to create styled link. There are various ways to implement it. Styling Links Link can be styled with CSS properties like color, font-family, background etc. Example: <!DOCTYPE html>   <html>   <head>   <style>   a {       color: red;   }   </style>   </head>   <body>   <p><b><a href="https://www.tutorialandexample.com"    target="_blank">This is a link</a></b></p>   </body>   </html> Try Now There are four...

2 minutes read.

CSS Text-align

Text-align The text-align property of CSS sets the table-cell box’s horizontal alignment or any block element. The text-align property is the same as the CSS property vertical-align but towards any horizontal...

1 minute read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.

CSS Page-break-inside Attribute

Page-break-inside Attribute As its name implies, the page-break-before property is applied to include page break inside any element, at the time of printing any document. It adds the page break before...

3 minutes read.