DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Practical Example of Using CSS Layer
  • What Is Ant, Really?
  • Orgchart With CSS Flex and ZK
  • Circle Text With CSS and JavaScript

Trending

  • The Role of AI in Identity and Access Management for Organizations
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • Agile’s Quarter-Century Crisis
  • Enforcing Architecture With ArchUnit in Java
  1. DZone
  2. Coding
  3. Languages
  4. The CSS Box Model

The CSS Box Model

The box model is one of the most important parts of CSS. Let's look at how it works.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Jan. 26, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.5K Views

Join the DZone community and get the full member experience.

Join For Free

The CSS box model is a term thrown around in CSS with very little context, but is probably the most fundamental things you can know in CSS. Simply put, the box model determines, the size, margin, and padding of any object on the page. It also refers to the weird way CSS handles 'inline' and 'block' content.

The Box Model

In HTML, every element creates a box. Some of these elements, such as span and p are inline, meaning they are in line with text, rather than structural elements of the page.

Other elements, like div are large 'block' elements. Each element carries a different type, so getting familiar with these is useful when learning both HTML and CSS.

Block elements have a fixed width and height which sometime span the entire page, while inline elements are within in lines of text, meaning they have content floating beside them. Another type of block is an inline block, which is literally a block of fixed width and height, within an inline context, such as within text.

Regardless of whether an element is inline or a block, all elements have a number of core 'box' attributes. Those are shown in the diagam below:

CSS boxes

  • margin is the space outside (around) an HTML element.
  • border is the line around the element, enclosing both padding and the width/height.
  • padding is the space around the text and the edge of the element.
  • width and height only refer to the space within that, excluding the padding.

Box Model Properties

The CSS box model has 5 main properties, all of which can be defined separately. Below is a div with all the CSS box model properties applied to it:

CSS
 
div {
    width: 100px;
    height: 100px;
    padding: 10px;
    border: 2px solid black;
    margin: 5px;
}


For padding and margin, we can also refer to each side separately on the same line. In CSS, when we are referring to each side, the order is top, right, bottom, left. Take a look at the example below:

CSS
 
div {
    /*  top side padding: 10px
        right side padding: 20px
        bottom side padding: 5px
        left side padding: 10px
    */
    padding: 10px 20px 5px 10px;
}


We can also directly call out these by using the properties padding-top, padding-right, padding-bottom and padding-left. The exact same properties exist for margin, i.e:

CSS
 
div {
    margin-left: 20px;
}


Quick Case Study

Let's think a little bit more about how box models work. We create a new div, and give it a width of 40px, a padding of 20px, and a border of 2px, as shown below. We also add 4px of margin.

CSS
 
div {
    width: 40px;
    padding: 20px;
    border: 2px solid black;
    margin: 4px;
}


How Big Is the Box?

Since the width is 40px, the padding is 20px, and the border is 2px, the total width rendered on the page is actually 84px!

To explain a bit more, the width as shown in the diagram, is the width excluding the padding. Since we said the padding is 20px, CSS adds 20px to all sides of the box. That means 20px on the left, and 20px on the right, which is 40px in total. When we add that to our width, we get 80px.

Finally, we have 2px of border the whole way around the div, which is 2px on the left hand side, and 2px on the right hand side. The result is 40px + 40px + 4px, or 84px.

Display

CSS also has another property called display, which among other things, can allow you to hide an item. Display affects the box model, by defining if an object is block or inline. For the purposes of the box model, let's think about a few key properties:

  • none - the item is hidden.
  • inline - the item is inline, i.e. inline with text, it cannot have a width or height added to it.
  • block - the item is a block, i.e. it takes up the entire width and starts on a new line.
  • inline-block - the item is inline with text, but it can have a width and height added to it in CSS.
  • contents - the item is displayed as if its container does not exist, and is added to the container above.

An Example of the CSS Box Model Display Property

Let's take a look at a quick example. The code is shown below for a span that has been set to display as a block. Typically, a span is an inline element, so this line of CSS will change its behaviour on the page. Notice that by default, the element is inline, and the width and height are not applied

CSS
 
span {
    display: block;
    width: 100px;
    height: 30px;
    padding: 10px;
}


Box Sizing

The way CSS manages padding, width, and border separately has always been a point of contention in the CSS community. As such, a property has been created to remedy this, known as box-sizing. Box sizing lets us override this default behaviour.

Let's think about our 40px width box which ended up being 84px wide. We can set box-sizing to:

  • border-box: the width includes border and padding. Our total width will now be 40px, even with the padding and border.
  • content-box: the default behaviour, the width excludes border and padding. Our total width will now be 84px.

Now we have way more control and can set our widths with certainty that they will display as we expect them to on the page.

Borders

Borders are another way we can affect the box model. Borders can be defined as surrounding the entire element, or on a specific side, using border-top, border-right, border-bottom or border-left. Here is an example:

CSS
 
div {
    border: 1px solid red;
    border-top: 2px solid black;
}


A border property can be split out into separate lines too. 1px solid red can be written as:

CSS
 
div {
    border-width: 1px;
    border-color: red;
    border-style: solid;
}


Similarly, we can apply these to a single side, i.e. border-top-width, border-top-color or border-top-style for the top side. We can do this for any side.

The color accepts any color, and you can learn more about colors in the color section. the border-style property accepts the following values: None, Hidden, Dotted, Dashed, Solid, Double, Groove, Ridge, Inset, Outset

Border Radius

Finally, border-radius lets us add rounded edges to our divs. Note, this does not affect the box model, so the size of the element remains the same, but it does affect its aesthetics. It accepts any unit — but I am using pixels as an example below. The larger the unit the bigger the rounding. Here is an example in code of how it looks:

CSS
 
div {
    border-radius: 20px;
}


Conclusion

That's everything you need to know to understand the box model. If you're interested in testing your knowledge, I've also made a quiz which you can check out here. Thanks for reading.

CSS Element Property (programming) Blocks

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Practical Example of Using CSS Layer
  • What Is Ant, Really?
  • Orgchart With CSS Flex and ZK
  • Circle Text With CSS and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!