--- title: border-width slug: Web/CSS/border-width translation_of: Web/CSS/border-width ---
border-width
属性可以设置盒子模型的边框宽度。
{{EmbedInteractiveExample("pages/css/border-width.html")}}
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
它是{{cssxref("border-top-width")}}, {{cssxref("border-right-width")}}, {{cssxref("border-bottom-width")}}, 和 {{cssxref("border-left-width")}}的简写;
/* 用法一:明确指定宽度值 */ /* 当给定一个宽度时,该宽度作用于选定元素的所有边框 */ border-width: 5px; /* 当给定两个宽度时,该宽度分别依次作用于选定元素的横边与纵边 */ border-width: 2px 1.5em; /* 当给定三个宽度时,该宽度分别依次作用于选定元素的上横边、纵边、下横边 */ border-width: 1px 2em 1.5cm; /* 当给定四个宽度时,该宽度分别依次作用于选定元素的上横边、右纵边、下横边、左纵边 (即按顺时针依次作用) */ border-width: 1px 2em 0 4rem; /* 用法二:使用全局关键字 */ /* 可以使用的全局关键字有:inherit(继承),initial(初始值),unset(不设置) */ border-width: inherit; /* 用法三:使用作用于 border-width 的关键字 */ border-width: thin; border-width: medium; border-width: thick;
关键字 | 实例 | 说明 |
thin |
|
细边线 |
medium |
|
中等边线 |
thick |
|
宽边线 |
提示:规范并没有规定关键字的实际值故在不同浏览器效果是不一样的,但显然thin≤medium≤thick,并且值在单个文档中是恒定的
HTML
<p id="sval"> one value: 6px wide border on all 4 sides</p> <p id="bival"> two different values: 2px wide top and bottom border, 10px wide right and left border</p> <p id="treval"> three different values: 0.3em top, 9px bottom, and zero width right and left</p> <p id="fourval"> four different values: "thin" top, "medium" right, "thick" bottom, and 1em right</p>
#sval { border: ridge #ccc; border-width: 6px; } #bival { border: solid red; border-width: 2px 10px; } #treval { border: dotted orange; border-width: 0.3em 0 9px; } #fourval { border: solid lightgreen; border-width: thin medium thick 1em; } p { width: auto; margin: 0.25em; padding: 0.25em; }
{{ EmbedLiveSample('A_mix_of_values_and_lengths', 300, 180) }}
Specification | Status | Comment |
---|---|---|
{{SpecName('CSS3 Backgrounds', '#the-border-width', 'border-width')}} | {{Spec2('CSS3 Backgrounds')}} | No direct change, the {{cssxref("<length>")}} CSS data type extension has an effect on this property. |
{{SpecName('CSS2.1', 'box.html#border-width-properties', 'border-width')}} | {{Spec2('CSS2.1')}} | Added the constraint that values' meaning must be constant inside a document. |
{{SpecName('CSS1', '#border-width', 'border-width')}} | {{Spec2('CSS1')}} | Initial definition |
{{Compat("css.properties.border-width")}}