CSS3 盒子模型的詳細語法知識點及案例代碼
CSS3 盒子模型完整指南
一、盒子模型基礎
每個 HTML 元素都被視為一個矩形盒子,由以下部分組成:
- 內容區 (Content)
- 內邊距 (Padding)
- 邊框 (Border)
- 外邊距 (Margin)
二、語法知識點詳解
1. 盒子的寬和高
selector {width: 200px; /* 內容區寬度 */height: 150px; /* 內容區高度 */min-width: 100px; /* 最小寬度 */max-height: 300px; /* 最大高度 */
}
2. 盒子的邊框 (border)
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; /* 取消右邊框 */
}
3. 外邊距 (margin)
selector {margin: 10px; /* 四邊相同 */margin: 10px 20px; /* 上下 | 左右 */margin: 5px 10px 15px; /* 上 | 左右 | 下 */margin: 5px 10px 15px 20px; /* 上 右 下 左 *//* 單邊設置 */margin-top: 20px;margin-left: auto; /* 水平居中常用 */
}
4. 內邊距 (padding)
selector {padding: 15px; /* 四邊相同 */padding: 10px 5%; /* 上下 | 左右 */padding: 0; /* 清除內邊距 *//* 單邊設置 */padding-bottom: 30px;
}
5. 背景 (background)
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;
}
6. 盒子尺寸計算 (box-sizing)
selector {box-sizing: content-box; /* 默認值:width/height只包含內容區 */box-sizing: border-box; /* width/height包含內容+padding+border */
}
7. 盒子陰影 (box-shadow)
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);
}
8. 圓角 (border-radius)
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;
}
三、案例代碼
案例1:基礎盒子模型
<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>
案例2:border-box 對比
<div class="box-content">Content Box</div>
<div class="box-border">Border Box</div><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>
案例3:陰影與圓角應用
<div class="card">Hover Me</div><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>
案例4:復雜邊框與背景
<div class="fancy-border">Special Box</div><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>
案例5:margin 合并現象
<div class="margin-box">Box 1</div>
<div class="margin-box">Box 2</div><style>
.margin-box {width: 200px;height: 50px;background: #3498db;margin: 20px 0;/* 實際垂直間距為20px(合并后),不是40px */
}
</style>
四、關鍵總結
-
尺寸計算:
content-box
:總寬度 = width + padding + borderborder-box
:總寬度 = width (包含padding和border)
-
邊距合并:
- 垂直相鄰塊級元素的margin會發生合并
- 解決方法:使用padding代替或創建BFC
-
陰影技巧:
- 多層陰影用逗號分隔:
box-shadow: 陰影1, 陰影2;
- 內陰影使用
inset
關鍵字
- 多層陰影用逗號分隔:
-
背景疊加:
- 使用多背景時,先定義的圖片層級最高
background-blend-mode
控制混合模式
-
開發建議:
- 全局設置
box-sizing: border-box
更易控制布局
* { box-sizing: border-box; }
- 全局設置
通過調整案例中的數值,可以直觀觀察不同屬性的效果差異。建議使用瀏覽器開發者工具實時調試盒子模型參數!
五、案例代碼
以下是一些開發中常用的 實際案例,涵蓋盒子模型的各個核心屬性,每個案例都附帶詳細注釋和應用場景說明:
案例 1:響應式卡片布局(綜合應用)
場景:商品卡片展示,包含內邊距、陰影、圓角和背景控制。
<div class="product-card"><img src="product.jpg" class="card-image"><div class="card-content"><h3 class="title">商品名稱</h3><p class="price">¥199.00</p></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
處理圖片超出容器的情況- 陰影和邊框組合提升視覺層次
案例 2:靈活按鈕組件(邊距與圓角)
場景:可復用按鈕組件,支持不同尺寸和狀態。
<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
創建不同形狀(圓形/膠囊形)- 陰影實現懸浮效果
案例 3:高級邊框效果(偽元素實現)
場景:實現漸變邊框和裝飾性角標。
<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
控制圖層疊加順序- 絕對定位實現裝飾性元素
案例 4:間距系統實用類(Margin/Padding)
場景:快速構建布局的間距工具類。
<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
確保優先級 - 數字后綴表示像素值(實際項目建議使用rem)
- 響應式前綴處理不同屏幕尺寸
案例 5:動態輸入框(Focus狀態增強)
場景:帶交互效果的輸入框,聚焦時改變邊框和陰影。
<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
去除默認聚焦樣式- 使用過渡動畫平滑狀態變化
- 陰影實現柔和的高光效果
案例 6:等高列布局(Padding補償法)
場景:實現兩側有邊距的等高列布局。
<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>
關鍵點:
- 使用負margin解決flex布局的間距問題
- 內外邊距組合控制元素間隔
- 陰影增加視覺分隔
開發技巧總結
-
調試工具:使用瀏覽器開發者工具的 盒模型檢查器(Elements → Computed)
-
重置默認樣式:
* { margin: 0; padding: 0; box-sizing: border-box; }
-
間距系統:建議使用
rem
單位 + CSS變量 定義間距尺度 -
邊框技巧:
- 使用
transparent
占位隱藏邊框 - 通過
border-image
實現漸變邊框
- 使用
-
性能優化:避免過度使用陰影和復雜背景(特別是移動端)
這些案例涵蓋了常見的布局需求和視覺效果,通過調整數值和組合屬性,可以快速構建出專業級的界面組件!