盒模型的问题_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:40:34
原创
1306人浏览过

我记得我之前在网上有查到,如果在CSS属性里面声明一个值可以使得外边距,内边距和框的数值是计算在盒的高度和宽度里面的,但我忘记这个属性和这个值是什么了?求高手告知。

还有我想问为什么CSS默认外边距,内边距和框的值要独立出来,不算在盒的高度和宽度里面?这样有什么好处吗?

吐槽大师
吐槽大师

吐槽大师(Roast Master) - 终极 AI 吐槽生成器,适用于 Instagram,Facebook,Twitter,Threads 和 Linkedin

吐槽大师 94
查看详情 吐槽大师

回复讨论(解决方案)

box-sizing 设为 content-box 时, height 和 width 只包含内容, 不含 padding 和 border
设为 border-box 时, height 和 width 包含 padding 和 border

你看看 W3C 官方的文档:

http://www.w3.org/TR/css3-ui/#box-sizing

content-box
This is the behavior of width and height as specified by CSS2.1. The specified width and height (and respective min/max properties) apply to the width and height respectively of the content box of the element. The padding and border of the element are laid out and drawn outside the specified width and height.
border-box
Length and percentages values for width and height (and respective min/max properties) on this element determine the border box of the element. That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified width and height properties. As the content width and height cannot be negative ([CSS21], section 10.2), this computation is floored at 0. Used values, as exposed for instance through getComputedStyle(), also refer to the border box.

还有我想问为什么CSS默认外边距,内边距和框的值要独立出来,不算在盒的高度和宽度里面?这样有什么好处吗?



如果我想要明确的设置内容的 height 和 width, 但 css 的 height 和 width 这两个属性却始终把 padding 和 border 的尺寸都包含在其中, 那我设置时不就是得先计算才能设置好吗
比如

$(element).css('width',    内容期望的宽度 + padding-left + padding-right + border-left + border-right);

这样岂不是相当麻烦吗, 而且如果 padding-left , padding-right , border-left,  border-right 之中的任意一个发生改变时, 都将影响到  内容的宽度

还有我想问为什么CSS默认外边距,内边距和框的值要独立出来,不算在盒的高度和宽度里面?这样有什么好处吗?



如果我想要明确的设置内容的 height 和 width, 但 css 的 height 和 width 这两个属性却始终把 padding 和 border 的尺寸都包含在其中, 那我设置时不就是得先计算才能设置好吗
比如

$(element).css('width',    内容期望的宽度 + padding-left + padding-right + border-left + border-right);

这样岂不是相当麻烦吗, 而且如果 padding-left , padding-right , border-left,  border-right 之中的任意一个发生改变时, 都将影响到  内容的宽度

非常感谢。
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号