CSS border-top-width Property

The border-top-width property sets the thickness of an element's top border.

It accepts the keywords thin, medium, and thick, or a non-negative CSS length such as 2px, 0.25rem, or 0.3em.

The top border must have a visible style, such as solid, dashed, or dotted, for its width to appear. The property changes only the top border's thickness without directly modifying its style or color.

border-top-width Syntax

Specify one width keyword or one non-negative CSS length to control the thickness of the top border.

selector {
  border-top-width: 4px;
}

The general syntax is:

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

Thin Border

.panel {
  border-top-width: thin;
}

Medium Border

.panel {
  border-top-width: medium;
}

Thick Border

.panel {
  border-top-width: thick;
}

Pixel Width

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

Relative Length

.panel {
  border-top-width: 0.25rem;
}

Calculated Width

.panel {
  border-top-width: clamp(2px, 0.5vw, 8px);
}

Zero Width

.panel {
  border-top-width: 0;
}

CSS-Wide Keywords

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

Only one width value or one CSS-wide keyword is allowed. Percentages, negative lengths, and multiple width values are not valid for this property.

Property Values

The border-top-width property accepts three predefined width keywords or a non-negative length.

Value Description
thin Specifies a thin border. The exact thickness is determined by the browser.
medium Specifies a medium-width border. This is the initial value.
thick Specifies a thick border. The exact thickness is determined by the browser.
<length> Specifies a non-negative border width using a CSS length such as px, rem, or em.
0 Sets the top border width to zero, making it invisible.

CSS-Wide Keywords

Value Description
inherit Inherits the computed top border width from the parent element.
initial Resets the specified width to medium.
unset Resets the specified width to medium because the property is not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value medium
Inherited No
Applies to All elements
Computed value Absolute length, or zero if the top border style is none or hidden
Animation type By computed value type
Property type Longhand
Physical side Top
Percentages Not accepted

Although the initial width is medium, the computed width becomes zero when border-top-style is none or hidden.

How It Works

The browser combines the top border width with the border style and color to determine how the border is displayed.

Step 1: Create an Element

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

Step 2: Add Basic Styling

.panel {
  padding: 24px;
  background-color: #f3f7fa;
}

Step 3: Set the Border Style

.panel {
  border-top-style: solid;
}

The browser displays a solid top border using the initial medium width and currentColor unless other declarations override them.

Step 4: Set the Border Width

.panel {
  border-top-width: 6px;
}

The top border becomes 6 pixels thick.

Step 5: Set the Border Color

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

Step 6: Change the Width

.panel {
  border-top-width: 12px;
}

The top border becomes thicker while retaining its existing style and color.

Step 7: Remove the Visible Border

.panel {
  border-top-width: 0;
}

The top border disappears because its width is zero.

The other three border sides are not directly affected by changes to border-top-width.

Keyword Widths

CSS provides three predefined border width keywords: thin, medium, and thick.

thin

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

The browser uses a relatively thin border.

medium

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

The browser uses its medium border thickness.

thick

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

The browser uses a relatively thick border.

Comparing the Keywords

.thin {
  border-top-width: thin;
}
.medium {
  border-top-width: medium;
}
.thick {
  border-top-width: thick;
}

The keywords maintain the order thin less than or equal to medium less than or equal to thick, but their exact pixel measurements are not fixed by the specification.

Choosing Between Keywords and Lengths

Keyword widths are convenient when an exact measurement is unnecessary. Use explicit lengths when consistent thickness is important to the design.

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

Length Values

The border-top-width property supports non-negative CSS length values.

Pixels

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

Pixels provide a specific CSS pixel measurement.

rem Units

.panel {
  border-top-width: 0.25rem;
}

The width is based on the root element's font size.

em Units

.panel {
  border-top-width: 0.3em;
}

The width is based on the element's font size.

Viewport Units

.panel {
  border-top-width: 0.5vw;
}

The border width scales with the viewport width.

Calculated Length

.panel {
  border-top-width: calc(2px + 0.2rem);
}

Clamped Length

.panel {
  border-top-width: clamp(2px, 0.5vw, 8px);
}

The clamp() function provides a preferred responsive width with minimum and maximum limits.

Subpixel Width

.panel {
  border-top-width: 0.5px;
}

Fractional CSS pixel widths are valid. Their rendered appearance depends on device resolution and browser rasterization.

Percentage Values Are Invalid

.panel {
  border-top-width: 10%;
}

This declaration is invalid because border-top-width does not accept percentages.

For most interface borders, pixel or rem values provide predictable results.

Zero and Invalid Values

The property accepts zero but does not accept negative widths or percentage values.

Zero Width

.panel {
  border-top-style: solid;
  border-top-width: 0;
}

The border has no thickness and is not visible.

Negative Width

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

This declaration is invalid. Browsers ignore it rather than converting the negative value to a positive width.

Percentage Width

.panel {
  border-top-width: 20%;
}

This declaration is invalid because percentages are not supported.

Multiple Width Values

.panel {
  border-top-width: 2px 4px;
}

This declaration is invalid. The individual property accepts only one width.

Missing Unit

.panel {
  border-top-width: 4;
}

This declaration is invalid because nonzero length values require a unit.

Valid Unitless Zero

.panel {
  border-top-width: 0;
}

Zero does not require a unit.

Invalid Calculated Width

.panel {
  border-top-width: calc(2px - 10px);
}

This calculation produces a negative result. For border widths, a calculated value that becomes negative is clamped to zero rather than producing a negative border thickness.

Use non-negative length values and valid CSS units to ensure the browser can apply the intended border width.

Border Style and Color

The visible top border depends on its width, style, and color.

Width Without a Visible Style

.panel {
  border-top-width: 8px;
}

If the top border style remains none, no border appears and the computed width is zero.

Adding a Visible Style

.panel {
  border-top-width: 8px;
  border-top-style: solid;
}

The top border becomes visible using the element's current text color by default.

Setting the Color

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

Dashed Border

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

Dotted Border

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

Double Border

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

The specified width includes both visible lines and the gap between them.

Transparent Border

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

The border is transparent but still occupies space in the box model.

Using currentColor

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

The top border uses the element's computed text color.

Setting all three components explicitly is the clearest way to create a predictable border.

Border Shorthands

The border-top-width property is a longhand component of several border shorthand properties.

border-top Shorthand

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

The shorthand sets the width, style, and color of the top border.

border-width Shorthand

.panel {
  border-width: 5px 3px 2px 4px;
}

The four values apply clockwise: top, right, bottom, and left. The top border receives a width of 5 pixels.

border Shorthand

.panel {
  border: 3px solid #1769aa;
}

This shorthand sets the width, style, and color for all four border sides.

Overriding the Top Width

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

The top border becomes 8 pixels thick while the other sides remain 3 pixels thick.

Shorthand Override

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

The later shorthand resets the top border width to 3 pixels.

Omitted Width in a Shorthand

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

The later border-top shorthand resets the omitted width component to its initial value, medium.

Using Longhands

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

This is equivalent to the shorthand border-top: 5px solid #1769aa;.

Use the individual property when you need to change only the top border's thickness without resetting its style or color.

Individual Border Sides

CSS provides separate width properties for the top, right, bottom, and left borders.

Top Border

.panel {
  border-top-width: 6px;
}

Right Border

.panel {
  border-right-width: 4px;
}

Bottom Border

.panel {
  border-bottom-width: 2px;
}

Left Border

.panel {
  border-left-width: 8px;
}

Different Widths on Every Side

.panel {
  border-style: solid;
  border-color: #1769aa;
  border-top-width: 6px;
  border-right-width: 4px;
  border-bottom-width: 2px;
  border-left-width: 8px;
}

Equivalent Shorthand

.panel {
  border-style: solid;
  border-color: #1769aa;
  border-width: 6px 4px 2px 8px;
}

Two-Value Shorthand

.panel {
  border-style: solid;
  border-width: 6px 3px;
}

The top and bottom borders are 6 pixels thick, while the left and right borders are 3 pixels thick.

Three-Value Shorthand

.panel {
  border-style: solid;
  border-width: 6px 3px 2px;
}

The top border is 6 pixels, the left and right borders are 3 pixels, and the bottom border is 2 pixels.

Use individual width properties for targeted adjustments and the shorthand when several sides need to be configured together.

Physical vs. Logical Borders

The border-top-width property always targets the physical top border, regardless of writing mode or text direction.

Logical border properties identify edges according to the block and inline directions.

Physical Top Border

.panel {
  border-top-width: 6px;
}

Logical Block-Start Border

.panel {
  border-block-start-width: 6px;
}

Horizontal Writing Mode

.panel {
  writing-mode: horizontal-tb;
  border-block-start-width: 6px;
}

In horizontal top-to-bottom writing mode, the block-start edge corresponds to the physical top edge.

Vertical Right-to-Left Writing Mode

.panel {
  writing-mode: vertical-rl;
  border-block-start-width: 6px;
}

In vertical right-to-left writing mode, the block-start edge corresponds to the physical right edge.

Vertical Left-to-Right Writing Mode

.panel {
  writing-mode: vertical-lr;
  border-block-start-width: 6px;
}

In vertical left-to-right writing mode, the block-start edge corresponds to the physical left edge.

Physical and Logical Conflict

.panel {
  writing-mode: horizontal-tb;
  border-top-style: solid;
  border-top-width: 4px;
  border-block-start-width: 8px;
}

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

Logical Border Shorthand

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

This shorthand sets the width, style, and color of the logical block-start border.

Use physical properties for fixed visual placement and logical properties when borders should adapt to writing mode.

Box Model and Layout

Border width contributes to an element's dimensions according to the CSS box model.

Default content-box Sizing

.panel {
  box-sizing: content-box;
  width: 300px;
  padding: 20px;
  border-top: 10px solid #1769aa;
}

The specified width applies to the content box. The top border adds 10 pixels to the element's total outer height.

border-box Sizing

.panel {
  box-sizing: border-box;
  width: 300px;
  height: 160px;
  padding: 20px;
  border-top: 10px solid #1769aa;
}

The declared width and height include padding and borders. Increasing the top border width reduces the available content-box height when the outer height remains fixed.

Border Width and Total Height

.panel {
  height: 100px;
  padding: 20px;
  border-top: 8px solid #1769aa;
  border-bottom: 2px solid #1769aa;
}

With the default content-box sizing, the total outer height is 150 pixels, excluding margins.

Preventing Layout Shifts

.nav-link {
  border-top: 4px solid transparent;
}
.nav-link:hover {
  border-top-color: #1769aa;
}

Reserving the border width in the default state prevents the border from being added only on hover.

Width Changes on Hover

.panel {
  border-top: 2px solid #1769aa;
}
.panel:hover {
  border-top-width: 10px;
}

The border becomes thicker, but the element's outer dimensions or content area may change depending on its box sizing and layout constraints.

Using an Inset Box Shadow

.panel {
  box-shadow: inset 0 6px 0 #1769aa;
}

An inset box shadow can create a border-like visual effect without contributing to the box model.

When precise layout dimensions matter, account for border thickness or use a decorative alternative that does not affect layout.

Table Borders

Border widths can be affected by the table border model, especially when border-collapse: collapse is used.

Separate Table Borders

table {
  border-collapse: separate;
}
td {
  border-top: 4px solid #1769aa;
}

Each cell draws its own border independently.

Collapsed Table Borders

table {
  border-collapse: collapse;
}
td {
  border: 2px solid #777777;
}

Adjacent cell borders share an edge, and the browser resolves competing border declarations.

Different Border Widths

table {
  border-collapse: collapse;
}
td {
  border: 2px solid #777777;
}
td.highlight {
  border-top-width: 6px;
}

When competing collapsed borders have compatible styles and no higher-priority conflict rule applies, the thicker border generally wins.

Hidden Border

td.hidden-top {
  border-top-style: hidden;
  border-top-width: 10px;
}

The hidden style suppresses the shared border during collapsed border conflict resolution, regardless of its specified width.

None Border

td.no-top {
  border-top-style: none;
  border-top-width: 10px;
}

The computed border width becomes zero when the style is none.

For predictable table borders, use consistent styles and widths unless a particular border needs emphasis.

Cascade and Overrides

The CSS cascade determines which top border width applies when multiple declarations target the same element.

Later Declaration

.panel {
  border-top-width: 4px;
  border-top-width: 8px;
}

The later declaration wins when cascade priority is otherwise equal.

Higher Specificity

.panel {
  border-top-width: 4px;
}
.panel.featured {
  border-top-width: 10px;
}

Shorthand Override

.panel {
  border-top-width: 8px;
  border-width: 3px;
}

The later shorthand sets all four border widths to 3 pixels.

Longhand Override

.panel {
  border-width: 3px;
  border-top-width: 8px;
}

The top border becomes 8 pixels thick while the other sides remain 3 pixels thick.

CSS Layers

@layer base, components;
@layer base {
  .panel {
    border-top-width: 2px;
  }
}
@layer components {
  .panel {
    border-top-width: 6px;
  }
}

For normal declarations, the later layer takes precedence.

Resetting the Width

.panel {
  border-top-width: 8px;
}
.panel.no-top-border {
  border-top-width: 0;
}

The reset class removes the top border's thickness.

When troubleshooting border widths, inspect declarations involving border-top-width, border-width, border-top, and border.

CSS Custom Properties

Custom properties can store reusable border widths and make component variations easier to maintain.

Shared Border Width

:root {
  --top-border-width: 4px;
}
.panel {
  border-top-width: var(--top-border-width);
}

Fallback Width

.panel {
  border-top-width: var(--top-border-width, 3px);
}

Component Variants

.card {
  --top-border-width: 3px;
  border-top-style: solid;
  border-top-color: #1769aa;
  border-top-width: var(--top-border-width);
}
.card.featured {
  --top-border-width: 8px;
}
.card.compact {
  --top-border-width: 1px;
}

Complete Border Variables

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

Calculated Custom Property

:root {
  --base-border-width: 2px;
}
.panel {
  border-top-width: calc(var(--base-border-width) * 3);
}

Responsive Custom Property

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

Custom properties help maintain consistent border thickness across related components and responsive layouts.

Transitions and Animation

The border-top-width property can animate smoothly between compatible length values.

Hover Width Transition

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

Selected State

.card {
  border-top: 3px solid #1769aa;
  transition: border-top-width 0.3s ease;
}
.card.is-selected {
  border-top-width: 10px;
}

Keyframe Animation

@keyframes border-grow {
  0%, 100% {
    border-top-width: 2px;
  }
  50% {
    border-top-width: 12px;
  }
}
.animated-panel {
  border-top-style: solid;
  border-top-color: #1769aa;
  animation: border-grow 3s ease-in-out infinite;
}

Width and Color Transition

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

Avoiding Layout Changes

.panel {
  box-shadow: inset 0 2px 0 #1769aa;
  transition: box-shadow 0.3s ease;
}
.panel:hover {
  box-shadow: inset 0 8px 0 #1769aa;
}

An inset box shadow can simulate a growing top border without changing the element's box dimensions.

Reduced Motion Preference

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

Animating border width may cause layout recalculations. For decorative effects, consider an inset shadow or positioned pseudo-element.

Responsive Design

Border thickness can adapt to viewport width, container size, and component dimensions.

Media Query

.panel {
  border-top: 8px 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-style: solid;
  border-top-color: #1769aa;
  border-top-width: clamp(2px, 0.5vw, 8px);
}

Responsive rem Width

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

The border width follows the root font size.

Removing the Border on Small Screens

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

Logical Border Alternative

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

Logical border properties can adapt to writing modes, while physical border properties always target the same visual side.

Responsive border widths can maintain visual balance across different screen and component sizes.

Practical Uses

The border-top-width property is useful for section separators, highlighted cards, navigation indicators, and decorative accents.

Section Separator

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

Featured Card

.featured-card {
  padding: 24px;
  background-color: #f3f7fa;
  border-top: 8px solid #1769aa;
}

Thin Divider

.divider {
  border-top: 1px solid #cccccc;
}

Thick Accent Border

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

Active Navigation Item

.nav-link {
  border-top: 4px solid transparent;
}
.nav-link.active {
  border-top-color: #1769aa;
}

The transparent border reserves space before the active state appears.

Notification Panel

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

Card Variants

.card {
  border-top: 3px solid #1769aa;
}
.card.featured {
  border-top-width: 8px;
}
.card.compact {
  border-top-width: 1px;
}

Decorative Heading

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

Use border thickness to establish visual hierarchy, but avoid relying on border size alone to communicate important information.

border-top-width Example

The following example compares keyword widths, fixed lengths, relative lengths, and zero-width borders.

<div class="border-top-width-demo">
  <div class="demo-box thin">thin</div>
  <div class="demo-box medium">medium</div>
  <div class="demo-box thick">thick</div>
  <div class="demo-box one">1px</div>
  <div class="demo-box four">4px</div>
  <div class="demo-box eight">8px</div>
  <div class="demo-box rem">0.5rem</div>
  <div class="demo-box zero">0</div>
</div>
.border-top-width-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 20px;
}
.border-top-width-demo .demo-box {
  box-sizing: border-box;
  min-height: 100px;
  padding: 20px;
  border-top-style: solid;
  border-top-color: #1769aa;
  background-color: #e7f3fc;
  color: #173b5e;
  font-weight: bold;
  text-align: center;
}
.border-top-width-demo .thin {
  border-top-width: thin;
}
.border-top-width-demo .medium {
  border-top-width: medium;
}
.border-top-width-demo .thick {
  border-top-width: thick;
}
.border-top-width-demo .one {
  border-top-width: 1px;
}
.border-top-width-demo .four {
  border-top-width: 4px;
}
.border-top-width-demo .eight {
  border-top-width: 8px;
}
.border-top-width-demo .rem {
  border-top-width: 0.5rem;
}
.border-top-width-demo .zero {
  border-top-width: 0;
}
Browser Result
thin
medium
thick
1px
4px
8px
0.5rem
0

The keyword examples use browser-defined thicknesses. The length examples use specific CSS measurements, while the zero-width example has no visible top border.

The exact rendering of fractional and relative widths depends on the browser, font settings, and device resolution.

Important Considerations

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

  • Purpose: Controls the thickness of the physical top border.
  • Initial value: medium.
  • Inheritance: The property is not inherited.
  • Accepted values: thin, medium, thick, or a non-negative CSS length.
  • Single value: Only one width is allowed.
  • Percentages: Not supported.
  • Negative lengths: Invalid.
  • Zero width: Removes the visible thickness of the top border.
  • Visible style: A border style other than none or hidden is required for the width to appear.
  • Computed width: Becomes zero when the top border style is none or hidden.
  • Keyword measurements: Exact thicknesses are browser-defined.
  • Physical side: Always targets the top edge regardless of writing mode.
  • Logical alternative: Use border-block-start-width for the logical block-start edge.
  • Shorthand interactions: Later border, border-top, or border-width declarations may override the property.
  • Box model: Border thickness contributes to element dimensions according to box-sizing.
  • Layout changes: Increasing border width can affect layout and cause content movement.
  • Animation: Compatible width values can interpolate smoothly.
  • Browser support: The property is widely supported.

Use border-top-width when you need precise control over the top border's thickness without changing its style, color, or the other border sides.

Browser Support

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

Modern browsers support keyword widths, non-negative length values, and transitions without requiring vendor prefixes.

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

This declaration creates a 6-pixel solid top border.

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

Specifications

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

CSS Backgrounds and Borders Module Level 3

The specification defines border width keywords, accepted length values, computed widths, and interactions with border styles.