--- title: border slug: Web/CSS/border tags: - CSS - CSS Borders - CSS Property - Layout - Reference translation_of: Web/CSS/border ---
{{CSSRef("CSS Borders")}}

border shorthand CSS 屬性可用來設定元素的邊框。它可以設定以下的值 {{Cssxref("border-width")}}、{{Cssxref("border-style")}} 以及 {{Cssxref("border-color")}}。

{{EmbedInteractiveExample("pages/css/border.html")}}

As with all shorthand properties, any omitted sub-values will be set to their initial value. Importantly, border cannot be used to specify a custom value for {{cssxref("border-image")}}, but instead sets it to its initial value, i.e., none.

The border shorthand is especially useful when you want all four borders to be the same. To make them different from each other, however, you can use the longhand {{Cssxref("border-width")}}, {{Cssxref("border-style")}}, and {{Cssxref("border-color")}} properties, which accept different values for each side. Alternatively, you can target one border at a time with the physical (e.g., {{Cssxref("border-top")}} ) and logical (e.g., {{Cssxref("border-block-start")}}) border properties.

Borders vs. outlines

Borders and outlines are very similar. However, outlines differ from borders in the following ways:

Syntax

/* style */
border: solid;

/* width | style */
border: 2px dotted;

/* style | color */
border: outset #f33;

/* width | style | color */
border: medium dashed green;

/* Global values */
border: inherit;
border: initial;
border: unset;

The border property may be specified using one, two, or three of the values listed below. The order of the values does not matter.

Note: The border will be invisible if its style is not defined. This is because the style defaults to none.

Values

<line-width>
Sets the thickness of the border. Defaults to medium if absent. See {{Cssxref("border-width")}}.
<line-style>
Sets the style of the border. Defaults to none if absent. See {{Cssxref("border-style")}}.
{{cssxref("<color>")}}
Sets the color of the border. Defaults to currentcolor if absent. See {{Cssxref("border-color")}}.

Formal syntax

{{csssyntax}}

Example

HTML

<div>I have a border, an outline, AND a box shadow! Amazing, isn't it?</div>

CSS

div {
  border: 0.5rem outset pink;
  outline: 0.5rem solid khaki;
  box-shadow: 0 0 0 2rem skyblue;
  border-radius: 12px;
  font: bold 1rem sans-serif;
  margin: 2rem;
  padding: 1rem;
  outline-offset: 0.5rem;
}

Result

{{EmbedLiveSample('Example')}}

Specifications

Specification Status Comment
{{SpecName('CSS3 Backgrounds', '#the-border-shorthands', 'border')}} {{Spec2('CSS3 Backgrounds')}} Removes specific support for transparent, as it is now a valid {{cssxref("<color>")}}; this has no practical impact.
Though it cannot be set to a custom value using the shorthand, border now resets {{cssxref("border-image")}} to its initial value (none).
{{SpecName('CSS2.1', 'box.html#border-shorthand-properties', 'border')}} {{Spec2('CSS2.1')}} Accepts the inherit keyword. Also accepts transparent as a valid color.
{{SpecName('CSS1', '#border', 'border')}} {{Spec2('CSS1')}} Initial definition.

{{cssinfo}}

Browser compatibility

{{Compat("css.properties.border")}}