此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
CSS 背景和边框
CSS 背景和边框模块为元素提供了添加边框、圆角和阴影的属性。
你可以为元素添加各种类型的边框样式,涵盖从光栅图像到 CSS 渐变在内的任意类型图片作为边框素材。边框可以设置为方形或圆角,且每个角均可独立指定不同的圆角半径。无论元素是否有可见边框,均支持对其进行圆角化处理。
box-shadow 包括内阴影和外阴影、单个或多个阴影,以及实心或允许渐变为透明的阴影。外部 box-shadow 会投射出一种阴影,就好像元素的 border-box 是不透明的。内部 box-shadow 会投射出一种阴影,就好像填充边缘之外的所有内容都是不透明的。阴影可以是实心的,也可以包括过渡到透明的阴影颜色的扩散距离。
此模块中的属性还允许你定义<table> 中的单元格应具有共享边框还是单独边框。
In this article
背景、边框和盒子阴影的实际应用
此边框、背景和阴影示例由线性渐变和径向渐变组成的居中背景图像组成。一系列框阴影使边框看起来像是“弹出”的。左侧元素设置了边框图像。右侧元素具有圆角虚线边框。
<article> <div></div> <div></div></article>article { display: flex; gap: 10px;}div { color: #58ade3; height: 320px; width: 240px; padding: 20px; margin: 10px; border: dotted 15px; /* defaults to `currentcolor` */ border-radius: 100px 0; background-image: radial-gradient( circle, transparent 60%, currentcolor 60% 70%, transparent 70% ), linear-gradient(45deg, currentcolor, white), linear-gradient(transparent, transparent); /* the third transparent background image was added to provide space for the background color to show through */ background-color: currentcolor; background-position: center; background-size: 60px 60px, 120px 120px; background-clip: content-box, content-box, padding-box; box-shadow: inset 5px 5px 5px rgb(0 0 0 / 0.4), inset -5px -5px 5px rgb(0 0 0 / 0.4), 5px 5px 5px rgb(0 0 0 / 0.4), -5px -5px 5px rgb(0 0 0 / 0.4);}div:first-of-type { border-radius: 0; border-image-source: repeating-conic-gradient( from 3deg at 25% 25%, currentColor 0 3deg, transparent 3deg 6deg ); border-image-slice: 30;}背景图像使用background-image 定义。图像使用background-position 居中。为多个背景图像使用background-clip 属性的不同值,使背景图像保持在内容框内。背景颜色被裁剪到填充框,防止背景通过border-image 和dottedborder 的透明部分显示出来。右侧元素中的圆角使用border-radius 属性创建。使用单个box-shadow 声明设置所有阴影,包括内阴影和外阴影。
要查看此示例的代码,请在GitHub 上查看源代码。
参考
>属性
background-attachmentbackground-clipbackground-colorbackground-imagebackground-originbackground-positionbackground-repeatbackground-sizebackground简写background-position-xbackground-position-ybackground-position-inlinebackground-position-blockborder-bottom-colorborder-bottom-styleborder-bottom-widthborder-left-colorborder-left-styleborder-left-widthborder-left简写border-right-colorborder-right-styleborder-right-widthborder-right简写border-top-colorborder-top-styleborder-top-widthborder-top简写border-color简写border-style简写border-width简写border简写border-collapseborder-bottom-left-radiusborder-bottom-right-radiusborder-top-left-radiusborder-top-right-radiusborder-image-outsetborder-image-repeatborder-image-sliceborder-image-sourceborder-image-widthborder-image简写box-shadow
数据类型
<line-style>枚举类型
指南
- 学习 CSS:背景与边框
解释如何使用 CSS 背景图像实现装饰性图像。
- 多个背景的应用
解释如何在元素上设置一个或多个背景。
- 调整背景图片的大小
描述如何更改背景图像的大小和重复行为。
- 学习 CSS:盒模型
解释边框以及其他模型属性如何影响 CSS 盒模型。
- 使用 CSS 渐变
解释如何创建 CSS 渐变背景图像。
相关概念
box-sizing属性text-shadow属性<url>CSS 类型<color>数据类型<image>数据类型<position>数据类型currentcolor关键字
规范
| Specification |
|---|
| CSS Backgrounds and Borders Module Level 3> |
| CSS Backgrounds Module Level 4> |
参见
- 可以让你直观创建边框、圆角和阴影效果的交互工具:
- 使用 CSS 为 HTML 元素添加颜色,包括边框。
drop-shadow()过滤函数将下拉阴影效果应用于输入图像。该函数由filter和backdrop-filter属性使用。