此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

border-top

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

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

尝试一下

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">
    这是一个带有边框的盒子。
  </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;

/* 全局值 */
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

形式定义

初始值该简写所对应的每个属性:
适用元素所有元素. It also applies to ::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>这个盒子在上边有一个边框。</div>

CSS

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

结果

规范

Specification
CSS Backgrounds and Borders Module Level 3
# border-shorthands

浏览器兼容性

参见