国际盒子尺寸认知日
International box-sizing Awareness Day (2014)

原始链接: https://css-tricks.com/international-box-sizing-awareness-day/

今天被戏称为“国际盒模型意识日”,以庆祝CSS属性`box-sizing: border-box`。这个属性从根本上改变了元素宽度的计算方式,使CSS布局更具可预测性和可管理性。 默认情况下,CSS使用`content-box`,其中内边距和边框*增加*到元素的指定宽度。`border-box`则将内边距和边框*包含*在定义的宽度内,这意味着25%的宽度真正等于其容器的25%——简化了计算并防止意外溢出。 推荐代码`*, *:before, *:after { box-sizing: border-box; }`将其应用于所有元素,包括伪元素,并包含厂商前缀以获得更广泛的浏览器兼容性(尽管Autoprefixer可以自动处理此问题)。当使用响应式布局和列时,此更改特别有用,可确保在不同浏览器和屏幕尺寸上保持一致的尺寸。这是一个小小的改变,对CSS开发流程有着巨大的影响。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 国际 box-sizing 意识日 (css-tricks.com) 13 分,由 hisamafahri 1小时前发布 | 隐藏 | 过去 | 收藏 | 讨论 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文

It’s February 1st today, which I’ve decided to declare International box-sizing Awareness Day. In honor of, you guessed it, the most humble and undersung, yet awesome and useful CSS property: box-sizing.

The date corresponds to Paul Irish’s post where he introduced the concept of using it on every single element on the page. We’ve talked about it around here a few times as well.

Here it is, in all it’s glory:

*, *:before, *:after {
  -webkit-box-sizing: border-box; 
  -moz-box-sizing: border-box; 
  box-sizing: border-box;
}

The default value for box-sizing is content-box, which is what we are overriding here. There is also a padding-box value but… kinda useless if you ask me. We’ll get to what this means shortly.

Notice we’re using the * selector to select all elements, as well as making pseudo elements use the same model, which otherwise wouldn’t be selected by the * selector alone.

Here’s the browser support situation. “-” = “this version and down”. “+” = “this version and up”.

*, *:before, *:after {
  /* Chrome 9-, Safari 5-, iOS 4.2-, Android 3-, Blackberry 7- */
  -webkit-box-sizing: border-box; 

  /* Firefox (desktop or Android) 28- */
  -moz-box-sizing: border-box;

  /* Firefox 29+, IE 8+, Chrome 10+, Safari 5.1+, Opera 9.5+, iOS 5+, Opera Mini Anything, Blackberry 10+, Android 4+ */
  box-sizing: border-box;
}

In the fairly near future we won’t need any prefixes at all for it, but I like to just leave that kind of thing to Autoprefixer.

Why all the HOO-RAH?!

It makes working with boxes so super duper much nicer.

When you set the width of an element, that’s the width that it is. If you set the width to 25%, it will take up 1/4 of the horizontal space available in its parent element. That’s it.

That’s not always the case. With the default box-sizing, as soon as an element has either padding or border applied, the actual rendered width is wider than the width you set.

Actual width = width + border-left + border-right + padding-left + padding-right

The math is bad enough, but when combined with percentages (or any mixed units, really) the result is impossible to do in your head and, more importantly, ends up being some useless number that you can’t do anything with.

You might think of it this way: with box-sizing: border-box the padding and border press their way inside the box rather than expand the box. The result is a box the exact width you set it to be and can count on.

Columns is a particularly useful case, but this comes in useful all the time and becomes one of those things that just makes CSS development better.


Remember to read Paul’s original post which covers some more ground like performance (don’t worry about it), jQuery (don’t worry about it), and third-party content (easy to fix).

Happy International box-sizing Awareness Day! Maybe next year we can get organized about it and all wear groovy square sunglasses or something.

联系我们 contact @ memedata.com