此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
每个盒子都有层
- CSS 把每个元素都看成一个盒子。
- 内容是你的文字或图片。padding(内边距)是内容四周、盒子里面的空间。
- **border(边框)**是内边距外面的一圈线。margin(外边距)是盒子外面的空间,把别的盒子推开。
内边距和边框
padding: 20px;在四边各加 20 像素的内部空间。border: 2px solid black;根据宽度、样式和颜色绘制线条。任何顺序均可,但通常按 width, style, colour 的顺序书写。- 内边距让盒子显得宽松;边框让它更醒目。
English
<style>
.card {
background-color: #eef;
padding: 16px;
border: 2px solid #88a;
}
</style>
<div class="card">A comfy padded card.</div>
常见错误
- 盒模型由内到外是:内容、内边距、边框、外边距。
- 内边距在边框内侧增加空间。
现在轮到你
- 内边距在里面,边框围在它外面。
- 先给盒子加内边距,再加边框。
探索
The CSS box model
Every box is content + padding + border + margin around it.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Give the box some breathing room inside. Add padding: 20px; to the .box rule.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Draw a line around the box. Add border: 2px solid navy; to the .box rule — the usual order is width, style, colour.
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Style the tip box with both layers: padding: 12px; inside and border: 3px dashed orange; around it.