Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*{ box-sizing: border-box }

*{
  box-sizing: border-box;
}

Is this a good idea? Any drawbacks?

I find this very useful when I want an element to have 100% and some inner padding. Because I don't have to add another element inside for the padding :/

like image 411
thelolcat Avatar asked Nov 12 '13 21:11

thelolcat


People also ask

What is border-box sizing?

border-box tells the browser to account for any border and padding in the values you specify for an element's width and height. If you set an element's width to 100 pixels, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width.

Should I always use box sizing border-box?

CSS border-box is the most popular choice for setting box-sizing . It guarantees that the content box shrinks to make space for the padding and borders. Therefore, if you set your element width to 200 pixels, border-box makes sure that the content, padding, and borders fit in this number.

What is box sizing border-box in CSS?

With the CSS box-sizing Property The box-sizing property allows us to include the padding and border in an element's total width and height. If you set box-sizing: border-box; on an element, padding and border are included in the width and height: Both divs are the same size now!


2 Answers

I have started to use this almost always.

The Pros,

You do not need to calculate out the CSS box-model anymore.

You can easily add large padding to an object without have to re-fix your height/width

Faster coding of your css (look up SASS if you have not)

The cons,

IE7 and below have no support, who cares right? Some sadly do.

IE8 and up have only partial support.

This is how I go about this if I don't want everything to have it,

div, li, p {
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    -o-box-sizing: border-box;
}

I add the elements that I know will utilize this property and to prevent every object from having that property.

like image 88
Josh Powell Avatar answered Sep 18 '22 15:09

Josh Powell


The biggest drawback is that it doesn't work in a lot of browsers, most specifically < IE8. Check it out the usage here.

When I use this, I usually make sure I have all of the following in my stylesheet

.my-element {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  box-sizing: border-box;
}
like image 39
vernak2539 Avatar answered Sep 19 '22 15:09

vernak2539