A quick explanation of the Box Model in CSS, why content box makes life difficult and how to use border box to fix that.
https://docs.google.com/presentation/d/1jaYGtlyxLqmGj87loFaJe0LJP6WE7Vq2uIHebz3AZ9A/edit?usp=sharing - To view with full speaker notes
5. A box has a content area
with a width and height
It also has an outer edge ⤴
← width: →
min-width:
max-width:
↑
height:
min-height:
max-height:
↓
6. A box has padding
Padding has an outer edge ⤴ ↑
padding-top
↓
↑
padding-bottom
↓
← →
padding-right
← →
padding-left
Content edge
7. A box has a border
The border has an outer edge ⤵
Content edge
Padding edge
8. A box has margin
Margin has an outer edge ⤵
↑
margin-top
↓
↑
margin-bottom
↓
← →
margin-right
← →
margin-left
Content edge
Padding edge
9. Chase dog then run away, lick arm hair, claw drapes, so hide head under
blanket so no one can see. Stare at the wall, play with food and get
confused by dust, bathe private parts with tongue then lick owner's face
put butt in owner's face. Lick butt and make a weird face.Caticus
cuteicus loves cheeseburgers. Hide at bottom of staircase to trip human,
attack dog, run away and pretend to be victim. See owner, run in terror
ignore the squirrels, you'll never catch them anyway so peer out window,
chatter at birds, lure them to mouth, chew on cable so present belly,
scratch hand when stroked or claw drapes. Stare at ceiling light, leave fur
on owner’s clothes. Curl into a furry donut. Lick the plastic bag, steal the
warm chair right after you get up, plan steps for world domination, paw at
your fat belly. Computer screen cat is love.
10. Chase dog then run away, lick arm hair, claw drapes, so hide head under
blanket so no one can see. Stare at the wall, play with food and get
confused by dust, bathe private parts with tongue then lick owner's face
put butt in owner's face. Lick butt and make a weird face.Caticus
cuteicus loves cheeseburgers. Hide at bottom of staircase to trip human,
attack dog, run away and pretend to be victim. See owner, run in terror
ignore the squirrels, you'll never catch them anyway so peer out window,
chatter at birds, lure them to mouth, chew on cable so present belly,
scratch hand when stroked or claw drapes. Stare at ceiling light, leave fur
on owner’s clothes. Curl into a furry donut. Lick the plastic bag, steal the
warm chair right after you get up, plan steps for world domination, paw at
your fat belly. Computer screen cat is love.
.container {
width: 600px;
padding: 20px;
border: 5px solid #333;
background-color: #fff;
}
11. Chase dog then run away, lick arm hair, claw drapes, so hide head under
blanket so no one can see. Stare at the wall, play with food and get
confused by dust, bathe private parts with tongue then lick owner's face
put butt in owner's face. Lick butt and make a weird face.Caticus
cuteicus loves cheeseburgers. Hide at bottom of staircase to trip human,
attack dog, run away and pretend to be victim. See owner, run in terror
ignore the squirrels, you'll never catch them anyway so peer out window,
chatter at birds, lure them to mouth, chew on cable so present belly,
scratch hand when stroked or claw drapes. Stare at ceiling light, leave fur
on owner’s clothes. Curl into a furry donut. Lick the plastic bag, steal the
warm chair right after you get up, plan steps for world domination, paw at
your fat belly. Computer screen cat is love.
20 600 20
640
12. Chase dog then run away, lick arm hair, claw drapes, so hide head under blanket so no one can see. Stare at the wall,
play with food and get confused by dust, bathe private parts with tongue then lick owner's face put butt in owner's
face. Lick butt and make a weird face.Caticus cuteicus loves cheeseburgers. Hide at bottom of staircase to trip
human, attack dog, run away and pretend to be victim. See owner, run in terror ignore the squirrels, you'll never catch
them anyway so peer out window, chatter at birds, lure them to mouth, chew on cable so present belly, scratch hand
when stroked or claw drapes. Stare at ceiling light, leave fur on owner’s clothes. Curl into a furry donut. Lick the plastic
bag, steal the warm chair right after you get up, plan steps for world domination, paw at your fat belly. Computer
screen cat is love.
Play riveting piece on synthesizer keyboard lick the other cats so paw at beetle and eat it before it gets away white cat
sleeps on a black shirt or unwrap toilet paper for poop on grasses yet jump off balcony, onto stranger's head. Intently
stare at the same spot sleep on dog bed, force dog to sleep on floor roll on the floor purring your whiskers off,
16. Chase dog then run away, lick arm hair, claw drapes, so hide head under
blanket so no one can see. Stare at the wall, play with food and get
confused by dust, bathe private parts with tongue then lick owner's face
put butt in owner's face. Lick butt and make a weird face.Caticus
cuteicus loves cheeseburgers. Hide at bottom of staircase to trip human,
attack dog, run away and pretend to be victim. See owner, run in terror
ignore the squirrels, you'll never catch them anyway so peer out window,
chatter at birds, lure them to mouth, chew on cable so present belly,
scratch hand when stroked or claw drapes. Stare at ceiling light, leave fur
on owner’s clothes. Curl into a furry donut. Lick the plastic bag, steal the
warm chair right after you get up, plan steps for world domination, paw at
your fat belly. Computer screen cat is love.
computed 20
5
20
5
600
.container {
box-sizing: border-box;
width: 600px;
padding: 20px;
border: 5px solid #333;
}
Every element in web design is represented on the page by a rectangular box. The box model describes how we can affect those boxes, and how we can position them on the page.
And yet, you’d be surprised by the number of developers who don’t have a good grasp of how to describe it.I’ve seen it bring seasoned developers out in a cold sweat and yet is one of the first things that we try and teach to a student learning CSS.But is is by no means difficult to understand, so let’s go over it now.
*
So we said that every element in web design is represented on the page by a rectangular box.
The box model models how we can affect those boxes,
how we can style them and space them out on the page,
and how they push each other about the page.
So, how do we describe a box?
Firstly, a box has content. This is, unsurprisingly calledl the content area.
The content area is the area containing the actual content of the element, be it text, an image or another element.
It is located inside of the content edge, its dimensions are width and height,Its properties are width, min-width, max-width,height, min-height and max-height control the content size.
A box can also have padding
The padding is located inside the padding edge, and it creates the padding area,
The space between the padding and the content edge can be controlled using the
padding-top,padding-right, padding-bottom, padding-left and the shorthand padding CSS properties.
When the content area has a background, color, or image set on it, this will extend into the padding.
The border area extends the padding area to the area containing the borders.
It is the area inside the border edge.
This area depends on the size of the border that is defined by the border-width property or the shorthand border.
The margin area extends the border area with an empty area ususally used to separate the element from its neighbors.
It is the area inside the margin edge,
The size of the margin area is controlled using the margin-top, margin-right, margin-bottom, margin-left and the shorthand margin CSS properties.
Ok, so now that we have that down we can start to build up a picture of what our element will look like on the page and how it will fit around the other elements.
Maybe something like this
We’ve got our content, our padding, a border and a margin, that you can’t see because it is transparent.
This is where we’re going to start running into problems.
You see W3C, the people who created CSS, in their infinite wisdom made it work a little counter intuitively.
Let’s illustrate the problem
I have a column on my page that is 600px wide. I want to fit the box that I made earlier into it so I set it’s width to 600px*Ok, so lets put the html in there.Wait, what, that wasn’t really what I was looking for. So what happened here?*
Padding, by default is additive to the width of the box. In other words, the width we’ve set is just on the content*the padding will add to that width and we’ll end up with a box that is 340px wide and now it is exploding out of the space we want it to sit inside.
Dammit.*
I’m sure those of you who have started your foray into responsive design have run into this problem, I want the element to fill the width of the page, so i set its width to 100%, and I want it to have 20px padding, so I give it that too With padding it is now 100% + 40px? Huh? Oh gods, scrollbars. The horror.
What is happening here? Well, the default sizing behaviour for CSS is what is called ‘Content box’.
That means that when you set its width, it sets the width of the content.
Which imho is a little mad.
If I made this box out of lego and asked you to tell me how wide it was, would you tell me three bricks or 5?!
Well content-box would tell me that this was three wide.
Who measures this way?
So, I could be forgiven for thinking that I’d be able to fit three of them on a backing plate that is 9 wide?
Right?
They won’t
So, what can we do?
Thankfully there is a sane option, whereas the default behaviour is content box, we can override this, and set it to
*
border box.
This means that everything inside the border is contained within the specified width.
Meaning that we can now do this:
Width 600px;
Padding 20px;
Border
We could even do width: 100% and it would fill its container
So, how do we use border box? In a sensible way? Putting the attribute on every element would be tiresome.
I would recommend the method created by our hero and all around good guy Paul Irish:
So, what is happening here? Well we’ve set border box on the html element and told everything to inherit from its parent, using the universal selector. This means that it will take border box too, unless you have specified otherwise elsewhere in your css.
Before and after aren’t covered by the universal selector, so we need to explicitly tell them to inherit too.
This gives us the box model that we always wanted.
You might get up in arms about the universal * selector.Apparently you’ve heard it’s slow.
Firstly, it’s not. It is as fast as h1 as a selector.
It can be slow when you specifically use it like .foo > *, so don’t do that.
Aside from that, you are not allowed to care about the performance of * unless you concatenate all your javascript, have it at the bottom, minify your css and js, gzip all your assets, and losslessly compress all your images. If you aren’t getting 90+ Page Speed scores, it’s way too early to be thinking about selector optimization.
So, how do we use this?
Well place it at the top of your CSS, or in your reset css file or wherever you are putting your helpers and all of the elements on the page will use border-box instead of content-box.
Thanks Paul
Thaul