CSS border-top Property

The border-top property sets the width, style, and color of an element's top border in a single declaration.

It is a shorthand for border-top-width, border-top-style, and border-top-color. You can specify all three values or omit values you do not need. Omitted components are reset to their initial values.

The property affects the physical top edge of an element, regardless of writing mode or text direction. For a border that follows the beginning of the block direction, use the logical border-block-start property.

border-top Syntax

The border-top shorthand accepts a border width, a border style, and a border color in any order.

selector {
  border-top: 3px solid #1769aa;
}

The general syntax is:

selector {
  border-top: <line-width> || <line-style> || <color>;
}

The double vertical bars indicate that the components may appear in any order, with each component used at most once.

Width, Style, and Color

.panel {
  border-top: 4px solid #1769aa;
}

Style and Color

.panel {
  border-top: dashed #1769aa;
}

The width defaults to medium.

Width and Style

.panel {
  border-top: 5px dotted;
}

The color defaults to currentcolor.

Style Only

.panel {
  border-top: solid;
}

Different Value Order

.panel {
  border-top: #1769aa double 6px;
}

Removing the Top Border

.panel {
  border-top: none;
}

CSS-Wide Keywords

.inherited {
  border-top: inherit;
}
.reset {
  border-top: initial;
}
.unset {
  border-top: unset;
}
.reverted {
  border-top: revert;
}
.layer-reverted {
  border-top: revert-layer;
}

CSS-wide keywords must be used alone rather than combined with width, style, or color values.

Property Values

The border-top property combines values from three longhand properties.

Component Accepted Values Description
<line-width> thin, medium, thick, or a non-negative length Sets the thickness of the top border.
<line-style> none, hidden, dotted, dashed, solid, double, groove, ridge, inset, outset Sets the line style of the top border.
<color> Any valid CSS color Sets the color of the top border.
inherit CSS-wide keyword Inherits all three computed components from the parent.
initial CSS-wide keyword Resets all three components to their initial values.
unset CSS-wide keyword Resets the non-inherited components to their initial values.
revert CSS-wide keyword Reverts the components according to cascade origin rules.
revert-layer CSS-wide keyword Reverts the components according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value Defined by the three longhand properties
Inherited No
Applies to Elements that support the border properties
Computed value Defined separately for each longhand
Animation type Defined separately for each longhand
Property type Shorthand
Physical edge Top
Longhand properties border-top-width, border-top-style, border-top-color

The initial component values are medium for width, none for style, and currentcolor for color.

How It Works

The browser expands the shorthand into three longhand declarations, then uses their values to draw the top border.

Step 1: Create an Element

<div class="panel">
  This panel has a top border.
</div>

Step 2: Add Basic Styling

.panel {
  padding: 20px;
  background-color: #f3f8fc;
}

Step 3: Add the Top Border

.panel {
  border-top: 4px solid #1769aa;
}

The browser draws a solid blue line along the element's top edge.

Step 4: Change the Style

.panel {
  border-top: 4px dashed #1769aa;
}

The top border changes from a continuous line to a dashed line.

Step 5: Increase the Width

.panel {
  border-top: 8px dashed #1769aa;
}

Step 6: Remove the Border

.panel {
  border-top: none;
}

The visible top border disappears, and its computed width becomes zero.

The shorthand changes only the top border's width, style, and color. The other three physical borders remain unaffected unless another declaration changes them.

Shorthand Components

The border-top shorthand represents three independent longhand properties.

Shorthand Declaration

.panel {
  border-top: 5px dashed #1769aa;
}

Equivalent Longhand Declarations

.panel {
  border-top-width: 5px;
  border-top-style: dashed;
  border-top-color: #1769aa;
}

Both examples produce the same top border.

Using a Different Value Order

.panel {
  border-top: dashed #1769aa 5px;
}

The order of width, style, and color does not affect the result.

Changing One Component

.panel {
  border-top: 5px solid #1769aa;
  border-top-style: dotted;
}

The later longhand changes only the style. The width and color remain unchanged.

Resetting Components With the Shorthand

.panel {
  border-top-width: 10px;
  border-top-color: #c04b37;
  border-top: solid;
}

The final declaration resets the width to medium and the color to currentcolor, while setting the style to solid.

Use the shorthand when defining a complete top border. Use individual longhands when you need to change only one component without resetting the others.

Top Border Width

The width component determines the thickness of the top border.

Thin Border

.panel {
  border-top: thin solid #1769aa;
}

Medium Border

.panel {
  border-top: medium solid #1769aa;
}

Thick Border

.panel {
  border-top: thick solid #1769aa;
}

The exact thickness of these keywords is determined by the browser.

Pixel Width

.panel {
  border-top: 6px solid #1769aa;
}

Relative Width

.panel {
  border-top: 0.3rem solid #1769aa;
}

Calculated Width

.panel {
  border-top: clamp(2px, 0.5vw, 8px) solid #1769aa;
}

Zero Width

.panel {
  border-top: 0 solid #1769aa;
}

The top border is not visible because its width is zero.

Border widths cannot be negative and do not accept percentage values.

Top Border Style

The style component determines the appearance of the top border line.

Style Appearance
noneNo visible border
hiddenNo visible border, with special collapsed table behavior
dottedSeries of dots
dashedSeries of short lines
solidContinuous line
doubleTwo parallel lines
grooveRecessed appearance
ridgeRaised ridge appearance
insetSunken appearance
outsetRaised appearance

Solid Top Border

.panel {
  border-top: 4px solid #1769aa;
}

Dashed Top Border

.panel {
  border-top: 4px dashed #1769aa;
}

Dotted Top Border

.panel {
  border-top: 4px dotted #1769aa;
}

Double Top Border

.panel {
  border-top: 8px double #1769aa;
}

Groove Top Border

.panel {
  border-top: 8px groove #1769aa;
}

Ridge Top Border

.panel {
  border-top: 8px ridge #1769aa;
}

The precise appearance of dotted, dashed, and three-dimensional styles can vary between browsers.

Top Border Color

The color component accepts any valid CSS color value, including named colors, hexadecimal colors, RGB, HSL, and currentcolor.

Hexadecimal Color

.panel {
  border-top: 4px solid #1769aa;
}

RGB Color

.panel {
  border-top: 4px solid rgb(23 105 170);
}

HSL Color

.panel {
  border-top: 4px solid hsl(207 76% 38%);
}

Transparent Color

.panel {
  border-top: 4px solid transparent;
}

The border remains part of the box model but is not visibly painted.

Using currentcolor

.panel {
  color: #1769aa;
  border-top: 4px solid currentcolor;
}

The border uses the element's current text color.

Color With Transparency

.panel {
  border-top: 4px solid rgb(23 105 170 / 50%);
}

Using a Custom Color Variable

.panel {
  --accent-color: #1769aa;
  border-top: 4px solid var(--accent-color);
}

When no color is specified, the shorthand resets the color component to currentcolor.

Omitted Values and Resets

Every border-top declaration sets all three longhand components. Components not explicitly included are reset to their initial values.

Omitting the Width

.panel {
  border-top: solid #1769aa;
}

The width becomes medium.

Omitting the Color

.panel {
  border-top: 4px solid;
}

The color becomes currentcolor.

Omitting the Style

.panel {
  border-top: 4px #1769aa;
}

The style becomes none, so no visible border is drawn and the computed width is zero.

Omitting Width and Color

.panel {
  border-top: dashed;
}

The width becomes medium and the color becomes currentcolor.

Resetting an Existing Color

.panel {
  border-top-color: #c04b37;
  border-top: 5px solid;
}

The later shorthand resets the color to currentcolor.

Resetting an Existing Width

.panel {
  border-top-width: 10px;
  border-top: dashed #1769aa;
}

The later shorthand resets the width to medium.

Resetting the Entire Top Border

.panel {
  border-top: 5px solid #1769aa;
  border-top: none;
}

The final declaration removes the visible top border and resets its width and color components.

If you need to preserve existing width or color values, change the appropriate longhand property instead of replacing the entire shorthand.

Shorthand Interactions

The border-top shorthand can interact with broader border shorthands and individual longhand declarations.

Combining With border

.panel {
  border: 2px solid #cccccc;
  border-top: 6px solid #1769aa;
}

All four sides initially receive a gray border. The later declaration changes only the top border.

Later border Shorthand

.panel {
  border-top: 6px solid #1769aa;
  border: 2px solid #cccccc;
}

The later border shorthand resets all four sides, including the top border.

Combining With border-width

.panel {
  border-top: 4px solid #1769aa;
  border-width: 8px;
}

The later border-width shorthand changes the width of all four sides, including the top.

Combining With border-style

.panel {
  border-top: 4px solid #1769aa;
  border-style: dashed;
}

The later shorthand changes all four border styles to dashed.

Combining With border-color

.panel {
  border-top: 4px solid #1769aa;
  border-color: #c04b37;
}

The later shorthand changes all four border colors to red.

Overriding One Component

.panel {
  border-top: 4px solid #1769aa;
  border-top-style: double;
}

The width and color remain unchanged, while the style becomes double.

At equal cascade priority, later shorthand and longhand declarations override the components they set.

Physical vs. Logical Borders

The border-top property always targets the physical top edge. Logical border properties follow the element's writing mode.

Physical Top Border

.panel {
  border-top: 4px solid #1769aa;
}

The border remains on the physical top edge in both horizontal and vertical writing modes.

Logical Block-Start Border

.panel {
  border-block-start: 4px solid #1769aa;
}

The border follows the block-start edge.

Horizontal Writing Mode

.panel {
  writing-mode: horizontal-tb;
  border-block-start: 4px solid #1769aa;
}

The block-start edge is the physical top edge.

Vertical Right-to-Left Writing Mode

.panel {
  writing-mode: vertical-rl;
  border-block-start: 4px solid #1769aa;
}

The block-start edge is the physical right edge.

Vertical Left-to-Right Writing Mode

.panel {
  writing-mode: vertical-lr;
  border-block-start: 4px solid #1769aa;
}

The block-start edge is the physical left edge.

Physical and Logical Conflict

.panel {
  writing-mode: horizontal-tb;
  border-top: 3px solid #1769aa;
  border-block-start: 6px dashed #c04b37;
}

Both declarations target the top edge in this writing mode. The later logical declaration wins when cascade priority is otherwise equal.

Use border-top for a fixed physical top edge and border-block-start when the border should follow the flow of content.

Box Sizing and Layout

A visible top border contributes to an element's box dimensions. The effect depends on its box sizing and available space.

Content-Box Sizing

.panel {
  box-sizing: content-box;
  height: 100px;
  padding: 20px;
  border-top: 5px solid #1769aa;
}

The top border adds 5 pixels to the element's outer height, in addition to its padding and any other borders.

Border-Box Sizing

.panel {
  box-sizing: border-box;
  height: 150px;
  padding: 20px;
  border-top: 5px solid #1769aa;
}

The specified height includes the top border and padding, subject to the minimum size needed for the box's contents and border.

Transparent Border

.panel {
  border-top: 5px solid transparent;
}

The transparent border still occupies space.

Border With No Style

.panel {
  border-top: 5px none #1769aa;
}

The computed top border width becomes zero.

Preventing Layout Shifts

.panel {
  border-top: 5px solid transparent;
}
.panel:hover {
  border-top-color: #1769aa;
}

The border space is reserved before hover, so changing its color does not change the border dimensions.

When a top border is used for interactive feedback, consider reserving its width or using an outline if changing layout dimensions would be undesirable.

Backgrounds and Border Radius

The top border can be combined with backgrounds and rounded corners to create decorative panels and interface components.

Colored Panel With Top Border

.panel {
  padding: 20px;
  background-color: #f3f8fc;
  border-top: 5px solid #1769aa;
}

Rounded Top Corners

.panel {
  border-top: 5px solid #1769aa;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}

The top border follows the rounded corners.

Rounded Panel

.panel {
  border: 2px solid #cccccc;
  border-top: 6px solid #1769aa;
  border-radius: 16px;
}

The top border uses a different width and color while following the rounded outer shape.

Background Clipping

.panel {
  border-top: 8px solid rgb(23 105 170 / 50%);
  background-color: #dceefa;
  background-clip: padding-box;
}

The background is clipped to the padding box rather than extending beneath the semi-transparent border.

Top Border With a Gradient Background

.panel {
  padding: 20px;
  border-top: 5px solid #1769aa;
  background: linear-gradient(#ffffff, #e7f3fc);
}

The border remains a separate painted edge above the gradient background.

For a multicolored or gradient top accent, a background gradient or pseudo-element may provide more control than a standard border color.

Cascade and Overrides

The CSS cascade determines which top border components apply when multiple declarations target the same element.

Later Declaration

.panel {
  border-top: 3px solid #1769aa;
  border-top: 6px dashed #c04b37;
}

The later declaration wins when cascade priority is otherwise equal.

Higher Specificity

.panel {
  border-top: 3px solid #1769aa;
}
.panel.featured {
  border-top: 6px solid #c04b37;
}

Longhand Override

.panel {
  border-top: 4px solid #1769aa;
  border-top-color: #c04b37;
}

Only the top border color changes.

Later Shorthand Override

.panel {
  border-top-color: #c04b37;
  border-top: 4px solid #1769aa;
}

The later shorthand sets the color back to blue.

CSS Layers

@layer base, components;
@layer base {
  .panel {
    border-top: 2px solid #cccccc;
  }
}
@layer components {
  .panel {
    border-top: 5px solid #1769aa;
  }
}

For normal declarations, the later layer takes precedence.

Resetting the Top Border

.panel {
  border-top: 5px solid #1769aa;
}
.panel.no-top-border {
  border-top: none;
}

When troubleshooting a border, inspect the cascade for both the shorthand and its three longhand properties.

CSS Custom Properties

Custom properties can store reusable border widths, styles, colors, or complete shorthand values.

Shared Top Border

:root {
  --panel-top-border: 4px solid #1769aa;
}
.panel {
  border-top: var(--panel-top-border);
}

Separate Components

:root {
  --border-width: 4px;
  --border-style: solid;
  --border-color: #1769aa;
}
.panel {
  border-top: var(--border-width) var(--border-style) var(--border-color);
}

Fallback Value

.panel {
  border-top: var(--top-border, 3px solid #1769aa);
}

Component Variants

.card {
  --card-top-color: #1769aa;
  border-top: 4px solid var(--card-top-color);
}
.card.warning {
  --card-top-color: #c04b37;
}
.card.success {
  --card-top-color: #27805c;
}

Calculated Width

:root {
  --base-border-width: 2px;
}
.panel {
  border-top: calc(var(--base-border-width) * 2) solid #1769aa;
}

Theme-Based Top Border

.theme-light {
  --accent-border: #1769aa;
}
.theme-dark {
  --accent-border: #80c7f2;
}
.panel {
  border-top: 5px solid var(--accent-border);
}

Custom properties make it easier to maintain consistent border accents across a design system.

Interactive States

Top borders can provide visual feedback for hover, focus, selection, and active interface states.

Hover State

.card {
  border-top: 4px solid #cccccc;
}
.card:hover {
  border-top-color: #1769aa;
}

Focus State

.control {
  border-top: 3px solid #888888;
}
.control:focus-visible {
  border-top-color: #1769aa;
  outline: 2px solid #1769aa;
  outline-offset: 2px;
}

Selected Card

.card {
  border-top: 4px solid #cccccc;
}
.card.is-selected {
  border-top: 6px solid #1769aa;
}

Active Navigation Item

.nav-link {
  border-top: 3px solid transparent;
}
.nav-link[aria-current="page"] {
  border-top-color: #1769aa;
}

Expanded Panel

details {
  border-top: 3px solid #cccccc;
}
details[open] {
  border-top-color: #1769aa;
}

For keyboard accessibility, do not rely on a top border alone as the only focus indicator. Preserve a clearly visible focus outline or equivalent indicator.

Transitions and Animation

The border-top shorthand contains components with different animation behaviors. Width and color can interpolate smoothly, while border style changes are discrete.

Color Transition

.panel {
  border-top: 4px solid #1769aa;
  transition: border-top-color 0.3s ease;
}
.panel:hover {
  border-top-color: #c04b37;
}

Width Transition

.panel {
  border-top: 2px solid #1769aa;
  transition: border-top-width 0.3s ease;
}
.panel:hover {
  border-top-width: 8px;
}

Changing the border width may affect layout or the content area.

Style Change

.panel {
  border-top: 4px solid #1769aa;
  transition: border-top-style 0.3s ease;
}
.panel:hover {
  border-top-style: dashed;
}

The style switches discretely rather than gradually changing from solid to dashed.

Animating Multiple Components

.panel {
  border-top: 3px solid #1769aa;
  transition: border-top-width 0.3s ease, border-top-color 0.3s ease;
}
.panel:hover {
  border-top: 6px solid #c04b37;
}

Keyframe Animation

@keyframes top-border-highlight {
  from {
    border-top-color: #1769aa;
  }
  to {
    border-top-color: #c04b37;
  }
}
.animated-panel {
  border-top: 5px solid #1769aa;
  animation: top-border-highlight 2s ease-in-out infinite alternate;
}

Reduced Motion Preference

.panel {
  border-top: 4px solid #1769aa;
  transition: border-top-color 0.3s ease;
}
.panel:hover {
  border-top-color: #c04b37;
}
@media (prefers-reduced-motion: reduce) {
  .panel {
    transition: none;
  }
}

For smooth effects, animate border-top-color or border-top-width rather than the style keyword.

Responsive Design

The top border can change in response to viewport width, container size, or component layout.

Media Query

.panel {
  border-top: 6px solid #1769aa;
}
@media (max-width: 600px) {
  .panel {
    border-top-width: 3px;
  }
}

Container Query

.panel-container {
  container-type: inline-size;
}
.panel {
  border-top: 3px solid #1769aa;
}
@container (min-width: 500px) {
  .panel {
    border-top-width: 8px;
  }
}

Fluid Border Width

.panel {
  border-top: clamp(2px, 0.5vw, 8px) solid #1769aa;
}

Removing the Top Border on Small Screens

.panel {
  border-top: 5px solid #1769aa;
}
@media (max-width: 600px) {
  .panel {
    border-top: none;
  }
}

Changing Border Style

.panel {
  border-top: 4px solid #1769aa;
}
@media (max-width: 600px) {
  .panel {
    border-top-style: dashed;
  }
}

Logical Border Alternative

.panel {
  border-block-start: 4px solid #1769aa;
}

The logical version follows the block-start edge when the writing mode changes.

Responsive borders can emphasize important sections without adding unnecessary decoration to smaller layouts.

Practical Uses

The border-top property is commonly used for section separators, cards, navigation, notifications, and decorative accents.

Section Separator

.content-section {
  padding-top: 24px;
  border-top: 2px solid #cccccc;
}

Featured Card

.featured-card {
  padding: 20px;
  background-color: #f3f8fc;
  border-top: 6px solid #1769aa;
}

Notification Panel

.notification {
  padding: 16px;
  background-color: #fff8e8;
  border-top: 5px solid #b7791f;
}

Navigation Indicator

.nav-link {
  display: inline-block;
  padding: 12px 20px;
  border-top: 3px solid transparent;
}
.nav-link[aria-current="page"] {
  border-top-color: #1769aa;
}

Dashed Divider

.divider {
  padding-top: 20px;
  border-top: 3px dashed #888888;
}

Double-Line Heading

.section-heading {
  padding-top: 12px;
  border-top: 6px double #1769aa;
}

Article Footer Separator

.article-footer {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #dddddd;
}

Accent Panel With Rounded Corners

.accent-panel {
  padding: 20px;
  border: 1px solid #cccccc;
  border-top: 6px solid #1769aa;
  border-radius: 12px;
}

Top borders can establish visual hierarchy and separate related content without requiring additional HTML elements.

border-top Example

The following example compares several top border styles, widths, and colors.

<div class="border-top-demo">
  <div class="demo-box solid">4px solid</div>
  <div class="demo-box dashed">4px dashed</div>
  <div class="demo-box dotted">4px dotted</div>
  <div class="demo-box double">8px double</div>
  <div class="demo-box groove">8px groove</div>
  <div class="demo-box ridge">8px ridge</div>
  <div class="demo-box thick">10px solid</div>
  <div class="demo-box transparent">Transparent</div>
  <div class="demo-box current">currentcolor</div>
  <div class="demo-box none">none</div>
</div>
.border-top-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 20px;
}
.border-top-demo .demo-box {
  box-sizing: border-box;
  min-height: 100px;
  padding: 20px;
  background-color: #f3f8fc;
  color: #173b5e;
  font-weight: bold;
  text-align: center;
}
.border-top-demo .solid {
  border-top: 4px solid #1769aa;
}
.border-top-demo .dashed {
  border-top: 4px dashed #1769aa;
}
.border-top-demo .dotted {
  border-top: 4px dotted #1769aa;
}
.border-top-demo .double {
  border-top: 8px double #1769aa;
}
.border-top-demo .groove {
  border-top: 8px groove #1769aa;
}
.border-top-demo .ridge {
  border-top: 8px ridge #1769aa;
}
.border-top-demo .thick {
  border-top: 10px solid #c04b37;
}
.border-top-demo .transparent {
  border-top: 6px solid transparent;
}
.border-top-demo .current {
  border-top: 5px solid currentcolor;
}
.border-top-demo .none {
  border-top: none;
}
Browser Result
4px solid
4px dashed
4px dotted
8px double
8px groove
8px ridge
10px solid
Transparent
currentcolor
none

The first examples show different line styles. The thick example uses a wider red border, while the transparent example reserves border space without displaying a visible line.

The currentcolor example uses the element's text color. The final example has no visible top border.

Important Considerations

Keep the following points in mind when using border-top:

  • Purpose: Sets the width, style, and color of the physical top border.
  • Property type: Shorthand for three top border longhand properties.
  • Initial components: medium, none, and currentcolor.
  • Inheritance: The component properties are not inherited.
  • Value order: Width, style, and color can appear in any order.
  • Omitted components: Reset to their initial values.
  • Border style: A style other than none or hidden is normally needed for a visible border.
  • Width values: Accept non-negative lengths or thin, medium, and thick.
  • Percentages: Not accepted for border width.
  • Colors: Accept valid CSS color values, including currentcolor and transparent.
  • none and hidden: Produce a computed top border width of zero.
  • Physical edge: Always affects the top side regardless of writing mode.
  • Logical alternative: Use border-block-start for a direction-aware block-start border.
  • Shorthand interactions: Broader border shorthands can override the top border components.
  • Box model: A visible top border contributes to the element's dimensions.
  • Rounded corners: The top border follows applicable border radius curves.
  • Custom properties: Can store complete border declarations or individual components.
  • Animation: Width and color can interpolate smoothly, while style changes are discrete.
  • Browser support: The property is widely supported.

Use border-top when you need a complete border along the physical top edge without changing the other three sides.

Browser Support

The border-top property is widely available and supported in Chrome, Edge, Firefox, Safari, and other major browsers.

The shorthand syntax and its width, style, and color components have long-standing browser support. No vendor prefix is required.

.panel {
  border-top: 4px solid #1769aa;
}

This declaration works in modern browsers and older browsers that support standard CSS borders.

For current compatibility details, visit Can I Use? and search for border-top.

Specifications

The border-top property is defined in the CSS Backgrounds and Borders Module Level 3 specification.

CSS Backgrounds and Borders Module Level 3

The specification defines the border shorthands and their relationships to border width, style, and color longhand properties.