css盒模型以及如何计算盒子的宽度

css盒模型以及如何计算盒子的宽度

盒模型

每个存在于可访问性树中的元素都会被浏览器绘制成一个盒子1

每个盒子都可以看成由4部分组成,它们分别是 — 元素外边距(margin)元素边框(border)元素内边距(padding)元素内容(content)

在这里插入图片描述

元素外边距(margin)

元素外边距(margin)用来控制元素与相邻元素之间间隔大小。

用法:

/*上方对外边距12像素*/
margin-top: 12px;
/*右方对外边距24像素*/
margin-right: 24px;
/*下方对外边距6像素*/
margin-bottom: 6px;
/*左方对外边距3像素*/
margin-left: 3px;
/*上方、右方、下方、左方皆对外边距12像素*/
margin: 12px;
/*上方、下方对外边距12像素 左方、右方对外边距24像素*/
margin: 12px 24px;
/*上方对外边距12像素 右方对外边距24像素 下方对外边距6像素 左方对外边距3像素*/
margin: 12px 24px 6px 3px;

按照left与right对应以及top与bottom对应,自动补充缺省值

/*上方对外边距12像素 右方对外边距24像素 下方对外边距6像素 左方对外边距24像素*/
margin: 12px 24px 6px;

水平方向相邻元素的margin值不会叠加

<!DOCTYPE html>
<html>
    <head>
        <meta title="charset" content="utf-8">
        <title>margin-horizontal-test</title>
        <style type="text/css">
            div {
                width: 100px;
                height: 100px;
                background-color: #f00;
            }            
            .margin-horizontal-test {
                display: inline-block;
                margin: 0 50px;
            }
        </style>
    </head>
    <body>
        <div class="margin-horizontal-test"></div>
        <div class="margin-horizontal-test"></div>
    </body>
</html>

效果图:
在这里插入图片描述

垂直方向相邻元素margin值会叠加,最终以两者之间最大值为准

<!DOCTYPE html>
<html>
    <head>
        <meta title="charset" content="utf-8">
        <title>margin-vertical-test</title>
        <style type="text/css">
            div {
                width: 100px;
                height: 100px;
                background-color: #f00;
            }            
            .margin-vertical-test {
                margin: 50px 0;
            }
        </style>
    </head>
    <body>
        <div class="margin-vertical-test"></div>
        <div class="margin-vertical-test"></div>
    </body>
</html>

效果图:
在这里插入图片描述

利用margin属性使块级元素水平居中

<!DOCTYPE html>
<html>
    <head>
        <meta title="charset" content="utf-8">
        <title>利用margin属性使块级元素水平居中</title>
        <style type="text/css">
            body {
                border: 1px solid black;
            }
            p {
                margin: 0 auto;
                width: 100px;
            }
        </style>
    </head>
    <body>
        <p>Fatman</p>
    </body>
</html>

效果图:
在这里插入图片描述

元素边框(border)

元素边框(border)位于元素外边距与元素内边距之间。

用法:

/*边框样式为实线*/
border-style: solid;
/*边框宽度为12像素*/
border-width: 12px;
/*边框颜色为黑色*/
border-color: black;
/*边框角度为36像素*/
border-radius: 36px;
/*边框宽度为12像素 样式为实线 颜色为黑色*/
border: 12px solid black;

border的属性也可以像margin一样根据上下左右设置,笔者就不再一一举例了。

利用边框颜色差设置3D按钮

<!DOCTYPE html>
<html>
    <head>
        <meta title="charset" content="utf-8">
        <title>border</title>
        <style type="text/css">
            button {
                width: 200px;
                height: 96px;
                background-color: #888888;
                border: 12px solid ;
                border-color: #cccccc #444444 #444444 #cccccc;
                color: white;
            }
        </style>
    </head>
    <body>
        <button>按钮</button>
    </body>
</html>

效果图:
在这里插入图片描述

元素内边距(padding)

元素内边距(padding)是用来定义元素内容距离元素边框之间间隔大小。

内边距和外边距的区别在于 — 外边距位于边框外部,而内边距位于边框内部。

用法:

/*上方对内边距12像素*/
padding-top: 12px;
/*右方对内边距24像素*/
padding-right: 24px;
/*下方对内边距6像素*/
padding-bottom: 6px;
/*左方对内边距3像素*/
padding-left: 3px;
/*上方、右方、下方、左方皆对内边距12像素*/
padding: 12px;
/*上方、下方对内边距12像素 左方、右方对内边距24像素*/
padding: 12px 24px;
/*上方对内边距12像素 右方对内边距24像素 下方对内边距6像素 左方对内边距3像素*/
padding: 12px 24px 6px 3px;

padding也可以按照left与right对应以及top与bottom对应,自动补充缺省值

/*上方对内边距12像素 右方对内边距24像素 下方对内边距6像素 左方对内边距24像素*/
padding: 12px 24px 6px;

使用padding增大元素的可点击范围

<!DOCTYPE html>
<html>
    <head>
        <meta title="charset" content="utf-8">
        <title>使用padding增大元素的可点击范围</title>
        <style type="text/css">
            img {
                width: 48px;
                height: 48px;
                padding: 24px;
            }
        </style>
    </head>
    <body>
    	<!-- 注意你的本地可能没有这个文件 -->
        <img id="click-img" src="./assets/icon.jpg">

        <script type="text/javascript">
            let img = document.getElementById('click-img');
            //点击padding处也可以触发点击事件
            img.addEventListener('click',function(){
                alert('click img');   
            })
        </script>
    </body>
</html>

效果图:
在这里插入图片描述

元素内容(content)

当box-sizing属性值为content-box时:

元素内容宽度 === 元素设置宽度

盒子宽度 === 元素左右外边距和 + 元素左右边框宽度和 + 元素左右内边距和 + 元素内容宽度

比如:

div {
    width: 100px;
    height: 100px;
    padding: 20px;
    border: 10px solid blue;
    margin: 20px;
    background-color: red;
    box-sizing: content-box;
}      

20 + 10 + 20 + 100 + 20 + 10 + 20
= 200

整个盒子的宽度为200px,元素内容的宽度值等于width值。

示意图:
在这里插入图片描述

当box-sizing属性值为border-box时:

元素内容宽度 === 元素设置宽度 - 元素左右边框宽度和 - 元素左右内边距和

盒子宽度 === 元素左右外边距和 + 元素设置宽度

比如:

div {
    width: 100px;
    height: 100px;
    padding: 20px;
    border: 10px solid blue;
    margin: 20px;
    background-color: red;
    box-sizing: border-box;
}      

20 + 100 + 20
= 140

整个盒子的宽度为140px,元素内容的宽度值等于width值减去padding值和border值。

示意图:
在这里插入图片描述


  1. 元素设置display:none后不存在于可访问性树中,不被浏览器绘制。 ↩︎

<think>好的,我现在需要回答用户关于CSS盒子模型中content-box和border-box两种计算方式的区别与应用的问题。首先,我得回顾一下盒子模型的基本概念,确保自己理解正确。 根据用户提供的引用内容,box-sizing属性用于指定模型计算方式,有两个值:content-box和border-box。默认情况下是content-box,这时候元素的宽度和高度只包括内容区域,而padding和border会增加到元素的外部,导致实际占据的空间更大。比如设置width: 200px时,实际宽度可能变成200px加上左右padding和border的宽度,这样容易在布局时出现意外,特别是响应式设计中需要注意。 而border-box则不同,它让元素的宽度和高度包含内容、padding和border。这样设置width: 200px时,无论padding和border如何调整,元素的总宽度保持200px,内容区域会相应缩小。这种模式更符合直观的布局需求,特别是在处理固定尺寸的布局时,不需要反复计算实际占据的空间,开发起来更方便。 接下来,我需要组织这些信息,确保结构清晰。先解释两种模式的区别,再对比它们的应用场景,最后给出示例代码帮助用户理解。同时要正确引用用户提供的参考资料,注意在适当的位置添加引用标识。 需要检查是否有遗漏点,比如是否提到了默认值,是否明确说明两种模式如何影响布局计算。另外,用户可能想知道何时使用哪种模式,所以应用场景部分要具体,比如传统布局与弹性布局中的使用建议。 最后,生成相关问题时要围绕盒子模型,可能涉及兼容性、实际案例、与其他CSS属性的结合等。确保问题有针对性,帮助用户深入理解。</think>### CSS盒子模型中content-box与border-box的区别与应用 #### 一、核心区别 1. **content-box(默认值)** - 元素的`width/height`仅定义**内容区域**的尺寸。 - 实际总宽度 = `width` + `左右padding` + `左右border` + `左右margin`[^1][^3]。 - 例如:`width: 200px; padding: 10px; border: 2px;` → 实际宽度为$200 + 20 + 4 = 224\text{px}$。 2. **border-box** - 元素的`width/height`包含**内容+内边距+边框**三部分的总和。 - 实际内容区域宽度 = `width` - `左右padding` - `左右border`[^2]。 - 例如:`width: 200px; padding: 10px; border: 2px;` → 内容区域实际宽度为$200 - 20 - 4 = 176\text{px}$。 #### 二、应用场景 1. **content-box适用场景** - 需要精确控制内容区域尺寸(如文本排版); - 传统文档流布局中,需显式计算总尺寸时。 2. **border-box适用场景** - 响应式布局中简化尺寸计算(推荐全局设置`* { box-sizing: border-box; }`); - 需要固定元素总尺寸时(如栅格系统、UI组件开发); - 避免因`padding/border`导致布局溢出。 #### 三、代码示例 ```css /* 传统content-box模式 */ .box1 { box-sizing: content-box; width: 200px; padding: 20px; border: 5px solid red; } /* 现代border-box模式 */ .box2 { box-sizing: border-box; width: 200px; padding: 20px; border: 5px solid blue; } ``` - `.box1`实际宽度为$200 + 40 + 10 = 250\text{px}$ - `.box2`实际宽度固定为$200\text{px}$,内容区域为$200 - 40 - 10 = 150\text{px}$ #### 四、兼容性 - 所有现代浏览器均支持`box-sizing`属性,IE8+可通过`-ms-box-sizing`兼容[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值