Why padding is included in height sometimes?
Why padding is included in height sometimes?
That depends on what box-sizing
attribute you have used:
border-box
means that the height and width of the box, defined/calculated in CSS, will also include the padding(s) and border width(s) applied to itcontent-box
is the default behavior, where padding(s) and border width(s) are added onto the defined/calculated height and width of the box.
By setting box-sizing: border-box
as seen in your left example, you have defined the height of the element at 20px. This means that the actual content box will only be 8px tall, because the browser will subtract the border (1px top, 1px bottom) and padding (5px top, 5px bottom) form the defined height, leaving only 8px left, which is a tad bit too short to contain height of the entire line (therefore the word appears to be cut off).
What is the difference between border-box and content-box in CSS?
While box-sizing: border-box; uses the box-model that people have come to associate with Internet Explorer, where the dimensions of the padding and border are included in the element’s dimensions.
https://jsfiddle.net/ud25tGNwMddCP7JI/25gtsr1d/17/ 参考这个demo
box-sizing
The box-sizing
CSS property sets how the total width and height of an element is calculated.
By default in the CSS box model, the width
and height
you assign to an element is applied only to the element's content box. If the element has any border or padding, this is then added to the width
and height
to arrive at the size of the box that's rendered on the screen. This means that when you set width
and height
, you have to adjust the value you give to allow for any border or padding that may be added. For example, if you have four boxes with width: 25%;
, if any has left or right padding or a left or right border, they will not by default fit on one line within the constraints of the parent container.
The box-sizing
property can be used to adjust this behavior:
content-box
gives you the default CSS box-sizing behavior. If you set an element's width to 100 pixels, then the element's content box will be 100 pixels wide, and the width of any border or padding will be added to the final rendered width, making the element wider than 100px.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. This typically makes it much easier to size elements.
Note: It is often useful to set box-sizing
to border-box
to layout elements. This makes dealing with the sizes of elements much easier, and generally eliminates a number of pitfalls you can stumble on while laying out your content. On the other hand, when using position: relative
or position: absolute
, use of box-sizing: content-box
allows the positioning values to be relative to the content, and independent of changes to border and padding sizes, which is sometimes desirable.
Syntax
The box-sizing
property is specified as a single keyword chosen from the list of values below.
Values
content-box
- This is the initial and default value as specified by the CSS standard. The
width
andheight
properties include the content, but does not include the padding, border, or margin. For example,.box {width: 350px; border: 10px solid black;}
renders a box that is 370px wide. - Here, the dimensions of the element are calculated as: width = width of the content, and height = height of the content. (Borders and padding are not included in the calculation.)
border-box
- The
width
andheight
properties include the content, padding, and border, but do not include the margin. Note that padding and border will be inside of the box. For example,.box {width: 350px; border: 10px solid black;}
renders a box that is 350px wide, with the area for content being 330px wide. The content box can't be negative and is floored to 0, making it impossible to useborder-box
to make the element disappear. - Here the dimensions of the element are calculated as: width = border + padding + width of the content, and height = border + padding + height of the content.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-01-12 Loop through an array in JavaScript
2019-01-12 Why does typeof array with objects return “Object” and not “Array”?
2017-01-12 原型模式Prototype
2017-01-12 How to Hide Zip Files Inside a Picture Without any Extra Software in Windows
2016-01-12 Walkthrough: Creating and Running Unit Tests for Managed Code
2016-01-12 Create a unit test project
2016-01-12 How to: Run Tests from Microsoft Visual Studio