border-top

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

border-top 简写 CSS 属性用于设置元素顶部边框的所有属性。

试一试

border-top: solid;
border-top: dashed red;
border-top: 1rem solid;
border-top: thick double #32a1ce;
border-top: 4mm ridge rgb(211 220 50 / 0.6);
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    This is a box with a border around it.
  </div>
</section>
#example-element {
  background-color: #eeeeee;
  color: darkmagenta;
  padding: 0.75em;
  width: 80%;
  height: 100px;
}

与所有简写属性一样,border-top 总是会设置它能设置的所有属性的值,即使这些属性没有被明确指定。它会将未明确指定的属性设置为其默认值。请看以下代码:

css
border-top-style: dotted;
border-top: thick green;

它实际上与此代码相同:

css
border-top-style: dotted;
border-top: none thick green;

border-top 之前给定的 border-top-style 值将被忽略。由于 border-top-style 的默认值是 none,因此不指定 border-style 部分会导致没有边框。

构成属性

此属性是以下 CSS 属性的简写:

语法

css
border-top: 1px;
border-top: 2px dotted;
border-top: medium dashed green;

/* Global values */
border-top: inherit;
border-top: initial;
border-top: revert;
border-top: revert-layer;
border-top: unset;

简写属性的三个值可以按任意顺序指定,其中一个或两个可以省略。

<br-width>

请参阅 border-top-width

<br-style>

请参阅 border-top-style

<color>

请参阅 border-top-color

正式定义

初始值作为简写中的每个属性
应用于所有元素。也适用于 ::first-letter
继承性
计算值作为简写中的每个属性
动画类型作为简写中的每个属性

正式语法

border-top = 
<line-width> ||
<line-style> ||
<color>

<line-width> =
<length [0,∞]> |
thin |
medium |
thick

<line-style> =
none |
hidden |
dotted |
dashed |
solid |
double |
groove |
ridge |
inset |
outset

示例

应用顶部边框

HTML

html
<div>This box has a border on the top side.</div>

CSS

css
div {
  border-top: 4px dashed blue;
  background-color: gold;
  height: 100px;
  width: 100px;
  font-weight: bold;
  text-align: center;
}

结果

规范

规范
CSS Backgrounds and Borders Module Level 3
# 边框简写

浏览器兼容性

另见