CSS3 盒模型是网页布局的基础,理解盒模型对于创建精确的布局至关重要。CSS3 提供了一些增强功能,如
box-sizing和resize属性,让我们能够更好地控制元素的尺寸和调整行为。
传统盒模型由四个部分组成:内容、内边距、边框和外边距。
.element {
width: 300px;
height: 200px;
padding: 20px;
border: 5px solid #000;
margin: 10px;
}
在传统盒模型中,元素的实际宽度计算方式:
实际宽度 = width + padding-left + padding-right + border-left + border-right
实际高度 = height + padding-top + padding-bottom + border-top + border-bottom
/* 传统盒模型 */
traditional-box {
box-sizing: content-box;
width: 300px;
padding: 20px;
border: 5px solid #000;
/* 实际宽度 = 300 + 20 + 20 + 5 + 5 = 350px */
}
box-sizing 属性用于更改盒模型的计算方式。
box-sizing: content-box | border-box;
content-box:传统盒模型(默认)border-box:现代盒模型/* 传统盒模型 */
.content-box {
box-sizing: content-box;
width: 300px;
padding: 20px;
border: 5px solid #000;
/* 实际宽度 = 300 + 20 + 20 + 5 + 5 = 350px */
}
/* 现代盒模型 */
.border-box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid #000;
/* 实际宽度 = 300px */
/* 内容宽度 = 300 - 20 - 20 - 5 - 5 = 250px */
}
/* 全局设置 */
* {
box-sizing: border-box;
}
/* 响应式网格 */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
.grid-item {
padding: 20px;
border: 1px solid #e9ecef;
border-radius: 8px;
}
/* 表单布局 */
.form {
max-width: 500px;
margin: 0 auto;
}
.form-group {
margin-bottom: 20px;
}
.form-control {
box-sizing: border-box;
width: 100%;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 16px;
}
.form-control:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}
/* 卡片布局 */
.card {
box-sizing: border-box;
width: 100%;
padding: 20px;
border: 1px solid #e9ecef;
border-radius: 12px;
background: white;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card-image {
box-sizing: border-box;
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 8px;
margin-bottom: 15px;
}
/* 导航栏 */
.nav {
box-sizing: border-box;
width: 100%;
padding: 15px 30px;
background: white;
border-bottom: 1px solid #e9ecef;
display: flex;
align-items: center;
justify-content: space-between;
}
.nav-links {
box-sizing: border-box;
display: flex;
gap: 20px;
}
.nav-link {
padding: 8px 16px;
border-radius: 4px;
transition: background 0.3s ease;
}
.nav-link:hover {
background: #f8f9fa;
}
/* 多列布局 */
.columns {
box-sizing: border-box;
display: flex;
gap: 20px;
}
.column {
box-sizing: border-box;
flex: 1;
padding: 20px;
border: 1px solid #e9ecef;
border-radius: 8px;
}
.column-1 {
flex: 2;
}
.column-2 {
flex: 1;
}
resize 属性用于控制元素是否可以调整大小。
resize: none | both | horizontal | vertical;
none:不可调整大小both:可以水平和垂直调整horizontal:只能水平调整vertical:只能垂直调整/* 不可调整大小 */
.no-resize {
resize: none;
}
/* 可水平和垂直调整 */
.both {
resize: both;
overflow: auto;
min-width: 200px;
min-height: 100px;
}
/* 只能水平调整 */
.horizontal {
resize: horizontal;
overflow: auto;
min-width: 200px;
}
/* 只能垂直调整 */
.vertical {
resize: vertical;
overflow: auto;
min-height: 100px;
}
/* 可调整大小的文本区域 */
.textarea {
box-sizing: border-box;
resize: vertical;
width: 100%;
min-height: 100px;
max-height: 300px;
padding: 12px 16px;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 16px;
font-family: inherit;
overflow: auto;
}
/* 代码编辑器 */
.code-editor {
box-sizing: border-box;
resize: both;
width: 100%;
min-width: 400px;
min-height: 200px;
padding: 20px;
background: #1e1e1e;
color: #d4d4d4;
font-family: 'Courier New', monospace;
font-size: 14px;
line-height: 1.6;
overflow: auto;
border-radius: 8px;
}
/* 可调整大小的图像容器 */
.image-container {
box-sizing: border-box;
resize: both;
width: 400px;
height: 300px;
min-width: 200px;
min-height: 150px;
overflow: hidden;
border: 2px solid #e9ecef;
border-radius: 8px;
}
.image-container img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* 可调整大小的面板 */
.panel {
box-sizing: border-box;
resize: both;
width: 300px;
height: 400px;
min-width: 200px;
min-height: 200px;
padding: 20px;
background: white;
border: 1px solid #e9ecef;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
overflow: auto;
}
.panel-header {
font-size: 18px;
font-weight: bold;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid #e9ecef;
}
/* 可调整大小的侧边栏 */
.sidebar {
box-sizing: border-box;
resize: horizontal;
width: 250px;
min-width: 200px;
max-width: 400px;
height: 100%;
padding: 20px;
background: white;
border-right: 1px solid #e9ecef;
overflow: auto;
}
/* 全局设置 */
* {
box-sizing: border-box;
}
/* 伪元素 */
*::before,
*::after {
box-sizing: border-box;
}
/* 响应式调整 */
.responsive {
resize: both;
width: 100%;
max-width: 600px;
min-width: 300px;
height: auto;
min-height: 200px;
}
@media (max-width: 768px) {
.responsive {
resize: vertical;
}
}
/* 组合使用 */
.combined {
box-sizing: border-box;
resize: both;
width: 100%;
padding: 20px;
border: 1px solid #e9ecef;
border-radius: 8px;
overflow: auto;
}
/* 自定义调整手柄 */
.custom-resize {
resize: both;
position: relative;
}
.custom-resize::after {
content: '↘';
position: absolute;
bottom: 5px;
right: 5px;
font-size: 12px;
color: #6c757d;
pointer-events: none;
}
CSS3 盒模型在所有现代浏览器中都有良好的支持:
box-sizing:Chrome 10+, Firefox 29+, Safari 5.1+, Edge 12+, IE 8+resize:Chrome 4+, Firefox 5+, Safari 4+, Edge 79+, IE 不支持/* 推荐 - 全局使用 border-box */
* {
box-sizing: border-box;
}
/* 不推荐 - 不统一盒模型 */
.element-1 {
box-sizing: content-box;
}
.element-2 {
box-sizing: border-box;
}
/* 推荐 - 提供降级方案 */
.element {
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
/* 不推荐 - 不提供降级方案 */
.element {
box-sizing: border-box;
}
/* 推荐 - 合理使用 resize */
.textarea {
resize: vertical;
overflow: auto;
}
/* 不推荐 - 不合理使用 resize */
.div {
resize: both;
}
/* 推荐 - 保持一致性 */
.form-control {
box-sizing: border-box;
width: 100%;
}
.card {
box-sizing: border-box;
width: 100%;
}
/* 不推荐 - 不一致 */
.element-1 {
box-sizing: content-box;
width: 100%;
}
.element-2 {
box-sizing: border-box;
width: 100%;
}
CSS3 盒模型提供了强大的布局控制能力:
主要属性:
box-sizing:盒模型类型resize:调整大小控制盒模型类型:
content-box:传统盒模型border-box:现代盒模型调整大小选项:
none:不可调整both:水平和垂直horizontal:水平vertical:垂直实际应用:
最佳实践:
优势:
注意事项:
掌握 CSS3 盒模型,能够让我们创建更加精确和灵活的布局,大大提升网页的布局质量和开发效率。