每个 HTML 元素都被视为一个矩形盒子,由以下部分组成:
selector {
width: 200px; /* 内容区宽度 */
height: 150px; /* 内容区高度 */
min-width: 100px; /* 最小宽度 */
max-height: 300px; /* 最大高度 */
}
selector {
/* 完整写法 */
border-width: 2px; /* 边框宽度 */
border-style: solid; /* 样式:solid/dashed/dotted/double等 */
border-color: #ff0000; /* 边框颜色 */
/* 简写形式 */
border: 2px solid red; /* 顺序:width style color */
/* 单边设置 */
border-top: 3px dashed blue;
border-right: none; /* 取消右边框 */
}
selector {
margin: 10px; /* 四边相同 */
margin: 10px 20px; /* 上下 | 左右 */
margin: 5px 10px 15px; /* 上 | 左右 | 下 */
margin: 5px 10px 15px 20px; /* 上 右 下 左 */
/* 单边设置 */
margin-top: 20px;
margin-left: auto; /* 水平居中常用 */
}
selector {
padding: 15px; /* 四边相同 */
padding: 10px 5%; /* 上下 | 左右 */
padding: 0; /* 清除内边距 */
/* 单边设置 */
padding-bottom: 30px;
}
selector {
background-color: #f0f0f0; /* 背景色 */
background-image: url("image.jpg"); /* 背景图片 */
background-repeat: no-repeat; /* 重复方式 */
background-position: center; /* 定位 */
background-size: cover; /* 尺寸控制 */
/* 简写形式 */
background: #fff url("bg.png") no-repeat center/cover;
}
selector {
box-sizing: content-box; /* 默认值:width/height只包含内容区 */
box-sizing: border-box; /* width/height包含内容+padding+border */
}
selector {
box-shadow: h-shadow v-shadow blur spread color inset;
/* 参数说明 */
/* h-shadow: 水平阴影位置(必需) */
/* v-shadow: 垂直阴影位置(必需) */
/* blur: 模糊距离 */
/* spread: 阴影尺寸 */
/* color: 颜色 */
/* inset: 内部阴影 */
/* 示例 */
box-shadow: 5px 5px 15px 2px rgba(0,0,0,0.3);
}
selector {
border-radius: 10px; /* 四角相同 */
border-radius: 10px 20px; /* 左上右下 | 右上左下 */
border-radius: 5px 10px 15px 20px; /* 左上 右上 右下 左下 */
/* 椭圆半径 */
border-radius: 50% 30% 20% 40%;
/* 单边设置 */
border-top-left-radius: 8px;
}
<div class="basic-box">Hello Box Model!div>
<style>
.basic-box {
width: 300px;
height: 200px;
padding: 20px;
border: 3px solid #3498db;
margin: 30px auto; /* 水平居中 */
background-color: #f8f9fa;
box-sizing: content-box; /* 默认 */
}
/* 总宽度 = width + padding*2 + border*2 = 300 + 40 + 6 = 346px */
style>
<div class="box-content">Content Boxdiv>
<div class="box-border">Border Boxdiv>
<style>
.box-content {
width: 200px;
padding: 20px;
border: 5px solid red;
background: #ffe6e6;
margin: 10px;
}
.box-border {
width: 200px;
padding: 20px;
border: 5px solid blue;
background: #e6f3ff;
margin: 10px;
box-sizing: border-box; /* 总宽度保持200px */
}
style>
<div class="card">Hover Mediv>
<style>
.card {
width: 250px;
height: 150px;
background: white;
margin: 20px;
padding: 20px;
border-radius: 15px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
transition: all 0.3s;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
border-radius: 25px 5px;
}
style>
<div class="fancy-border">Special Boxdiv>
<style>
.fancy-border {
width: 200px;
height: 100px;
padding: 20px;
margin: 30px auto;
background:
linear-gradient(45deg, #ff6b6b, #4ecdc4),
url("pattern.png");
background-blend-mode: overlay;
border: 3px double #2ecc71;
border-radius: 15px 0 15px 0;
box-shadow:
inset 0 0 10px #2ecc71,
5px 5px 15px rgba(0,0,0,0.3);
}
style>
<div class="margin-box">Box 1div>
<div class="margin-box">Box 2div>
<style>
.margin-box {
width: 200px;
height: 50px;
background: #3498db;
margin: 20px 0;
/* 实际垂直间距为20px(合并后),不是40px */
}
style>
尺寸计算:
content-box
:总宽度 = width + padding + borderborder-box
:总宽度 = width (包含padding和border)边距合并:
阴影技巧:
box-shadow: 阴影1, 阴影2;
inset
关键字背景叠加:
background-blend-mode
控制混合模式开发建议:
box-sizing: border-box
更易控制布局* { box-sizing: border-box; }
通过调整案例中的数值,可以直观观察不同属性的效果差异。建议使用浏览器开发者工具实时调试盒子模型参数!
以下是一些开发中常用的 实际案例,涵盖盒子模型的各个核心属性,每个案例都附带详细注释和应用场景说明:
场景:商品卡片展示,包含内边距、阴影、圆角和背景控制。
<div class="product-card">
<img src="product.jpg" class="card-image">
<div class="card-content">
<h3 class="title">商品名称h3>
<p class="price">¥199.00p>
div>
div>
<style>
.product-card {
width: 300px;
background: white;
border-radius: 12px; /* 圆角 */
box-shadow: 0 4px 12px rgba(0,0,0,0.1); /* 阴影 */
margin: 20px; /* 外边距 */
overflow: hidden; /* 隐藏图片溢出部分 */
box-sizing: border-box; /* 确保尺寸计算包含padding */
}
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
border-bottom: 2px solid #eee; /* 底部边框 */
}
.card-content {
padding: 20px; /* 内边距 */
}
.title {
margin: 0 0 10px 0; /* 下外边距 */
color: #333;
}
.price {
color: #e74c3c;
margin: 0; /* 清除默认外边距 */
}
style>
关键点:
box-sizing: border-box
确保布局稳定overflow: hidden
处理图片超出容器的情况场景:可复用按钮组件,支持不同尺寸和状态。
<button class="btn primary">主要按钮button>
<button class="btn secondary">次要按钮button>
<style>
.btn {
/* 基础样式 */
padding: 12px 24px; /* 内边距 */
border: none;
border-radius: 25px; /* 胶囊圆角 */
margin: 10px;
cursor: pointer;
transition: all 0.3s ease;
box-sizing: border-box;
font-size: 16px;
}
/* 不同变体 */
.primary {
background: #3498db;
color: white;
box-shadow: 0 4px 6px rgba(52,152,219,0.2);
}
.secondary {
background: #f1f1f1;
color: #333;
border: 1px solid #ddd; /* 边框替代背景色 */
}
/* 悬停状态 */
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 12px rgba(0,0,0,0.15);
}
/* 禁用状态 */
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
style>
关键点:
padding
控制按钮点击区域border-radius
创建不同形状(圆形/胶囊形)场景:实现渐变边框和装饰性角标。
<div class="fancy-box">
<div class="content">特殊边框效果div>
div>
<style>
.fancy-box {
position: relative;
width: 300px;
padding: 2px; /* 为伪元素留出空间 */
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
border-radius: 12px;
margin: 30px auto;
}
/* 通过伪元素实现内层背景 */
.fancy-box::after {
content: "";
position: absolute;
top: 2px;
left: 2px;
right: 2px;
bottom: 2px;
background: white;
border-radius: 10px; /* 比外层小2px */
z-index: 1;
}
.content {
position: relative;
padding: 20px;
z-index: 2; /* 确保内容在伪元素上方 */
}
/* 添加装饰角标 */
.fancy-box::before {
content: "HOT";
position: absolute;
top: -10px;
right: -10px;
background: #e74c3c;
color: white;
padding: 5px 15px;
border-radius: 20px;
z-index: 3;
font-size: 12px;
}
style>
关键点:
z-index
控制图层叠加顺序场景:快速构建布局的间距工具类。
<div class="mt-20 mb-40 pl-15">内容区块div>
<style>
/* Margin 工具类 */
.mt-10 { margin-top: 10px !important; }
.mt-20 { margin-top: 20px !important; }
.mb-40 { margin-bottom: 40px !important; }
/* Padding 工具类 */
.pl-15 { padding-left: 15px !important; }
.pr-30 { padding-right: 30px !important; }
/* 响应式示例 */
@media (max-width: 768px) {
.md-mt-0 { margin-top: 0 !important; }
}
style>
关键点:
!important
确保优先级场景:带交互效果的输入框,聚焦时改变边框和阴影。
<div class="input-group">
<input type="text" placeholder="请输入内容">
div>
<style>
.input-group {
margin: 15px 0;
}
input {
width: 100%;
padding: 12px 20px;
border: 2px solid #ddd;
border-radius: 8px;
box-sizing: border-box;
transition: all 0.3s ease;
}
input:focus {
outline: none;
border-color: #3498db;
box-shadow: 0 0 8px rgba(52,152,219,0.3);
}
style>
关键点:
outline: none
去除默认聚焦样式场景:实现两侧有边距的等高列布局。
<div class="column-container">
<div class="column">左侧内容div>
<div class="column">右侧内容div>
div>
<style>
.column-container {
margin: 0 -15px; /* 抵消列的边距 */
display: flex;
}
.column {
flex: 1;
background: #f8f9fa;
margin: 0 15px; /* 列间距 */
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
style>
关键点:
调试工具:使用浏览器开发者工具的 盒模型检查器(Elements → Computed)
重置默认样式:
* { margin: 0; padding: 0; box-sizing: border-box; }
间距系统:建议使用 rem
单位 + CSS变量 定义间距尺度
边框技巧:
transparent
占位隐藏边框border-image
实现渐变边框性能优化:避免过度使用阴影和复杂背景(特别是移动端)
这些案例涵盖了常见的布局需求和视觉效果,通过调整数值和组合属性,可以快速构建出专业级的界面组件!