CSS border-top-width Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The border-top-width property sets the thickness of an element’s top border. Use a keyword (thin, medium, thick) or a length such as 2px or 0.25rem.

It applies to all elements, including ::first-letter, and it is not inherited. The initial value is medium. A width does nothing useful until border-top-style is a visible style such as solid. For width, style, and color together, prefer the border-top shorthand.

Standard-track.

Not deprecated. Keywords and length values are Baseline and widely available. The newer hairline keyword from CSS Borders Level 4 is still experimental and not safe as a sole value in production yet.

CSS Syntax

syntax.css
border-top-width: thin | medium | thick | <length> | initial | inherit;

Lengths must be non-negative. Negative values are invalid and ignored.

Property Values

ValueDescriptionTry it
thinThin line. Typically about 1pxTry it
mediumDefault thickness. Typically about 3pxTry it
thickThick line. Typically about 5pxTry it
<length>Exact size such as 1px, 4px, or 0.25emTry it
initialResets to the default, mediumTry it
inheritTakes the parent’s border-top-widthTry it

How to Use border-top-width

Pick a thickness, then make sure style (and usually color) are set so the line can paint.

  1. Choose the element that needs a top edge line.
  2. Set border-top-width to a keyword or length.
  3. Set border-top-style to something visible, such as solid.
  4. Optionally set border-top-color, or use border-top for all three at once.
PieceRequired?Role
border-top-widthYesHow thick the top line is
border-top-styleYes (to show)Without a style other than none, nothing paints
border-top-colorOptionalDefaults to the element’s color if omitted
border-topOptionalShorthand for width, style, and color

Live Preview

The box already has a solid blue top border. Change only the width and watch the line grow or shrink.

Style and color stay fixed. Only the top width changes.

Top edge

border-top-width: 4px

CSS Examples

Each snippet focuses on border-top-width. Style and color are only there so the line can show. Use Try It Yourself for a live editor.

Example 1 — Pixel Length

Set an exact 5px top border on a paragraph.

CSS
<style>
  p {
    border-top-width: 5px;
    border-top-style: solid;
    border-top-color: black;
  }
</style>

<p>Top border is 5px thick.</p>
Try It Yourself

How It Works

  1. Set the width. border-top-width: 5px makes the top edge five pixels thick.
  2. Give it a style. border-top-style: solid draws a continuous line. Without this, the default none hides the border.
  3. Pick a color. border-top-color: black paints the line. If you omit color, it uses the element’s text color.
  4. Only the top changes. Bottom, left, and right stay untouched unless you set them separately.

Example 2 — Thick Keyword

Use thick when you want a heavier line without picking a pixel value.

CSS
<style>
  h2 {
    border-top-width: thick;
    border-top-style: solid;
    border-top-color: #2563eb;
  }
</style>

<h2>Thick top border</h2>
Try It Yourself

How It Works

  1. Use a keyword. thick is usually about 5px. thin and medium are the lighter options.
  2. Keep style visible. solid still required so the thick line paints.
  3. Color the accent. The blue color makes the heavy edge easy to see.
  4. Prefer lengths for UI polish. Keywords are quick. Exact px or rem values are better when design specs matter.

Example 3 — Compare Widths

Three boxes share the same style and color. Only the top width changes.

CSS
<style>
  div {
    border-top-style: solid;
    border-top-color: #2563eb;
  }

  .w1 {
    border-top-width: 1px;
  }

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

  .w8 {
    border-top-width: 8px;
  }
</style>

<div class="w1">1px</div>
<div class="w4">4px</div>
<div class="w8">8px</div>
Try It Yourself

How It Works

  1. Share the base. Every div gets the same solid blue top style.
  2. Change only width. Classes set 1px, 4px, and 8px.
  3. Read the difference. Larger widths make a heavier rule above the text.
  4. Mind the box model. A thicker border adds to the element’s outer size unless you use box-sizing: border-box.

Edge Cases & Pitfalls

If the top line is missing or looks wrong, check these first.

style

Width without style stays invisible

The default border-top-style is none. Set solid, dashed, or another visible style, or use the border-top shorthand.

0

0 removes the painted edge

border-top-width: 0 is valid and draws no visible line, even if style and color are set.

physical

Physical top, not logical start

In vertical writing-mode, “top” can look unexpected. Use border-block-start-width when the edge should follow writing direction.

box

Thickness affects layout size

With the default content-box sizing, a thicker top border makes the element taller. box-sizing: border-box keeps the outer size stable.

hairline

hairline is experimental

The Level 4 hairline keyword means a just-visible line. Support is incomplete, so prefer 1px or thin for production.

Browser Support

border-top-width is a browser CSS property for element borders. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

border-top-width

Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, Internet Explorer 4+. Keywords and length values are long-established.

99% Global support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 3.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 4+
Full support
border-top-width Widely supported

Bottom line: Safe to use everywhere for thin, medium, thick, and length values. Treat hairline as experimental until browser support catches up.

Key Takeaways

  • Role: set only the top border’s thickness with a keyword or length.
  • Needs style: pair with a visible border-top-style, or use border-top.
  • Initial value: medium. Not inherited.
  • Physical edge: always the top side. Use border-block-start-width for writing-mode-aware borders.

One line: border-top-width is the thickness dial for the top edge — turn on style (and color) so the dial has something to draw.

Frequently Asked Questions

It sets how thick the top border of an element is. It only changes the physical top edge, not the other three sides.
Width alone is not enough when border-top-style is none, which is the default. Set a visible style such as solid, and usually a color, or use the border-top shorthand.
The initial value is medium, which is typically about 3px. The border still stays hidden until you also set a visible border-top-style.
They are keyword sizes for the line. In practice they map to about 1px, 3px, and 5px. For exact control, use a length such as 2px or 0.25rem.
border-top-width always targets the physical top edge. border-block-start-width follows writing-mode and stays on the logical block-start side.
It is Baseline and widely available: Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, and Internet Explorer 4+. It is a browser style property, not a Node.js, Deno, or Bun feature.

Next: border-width

Set border thickness on one, two, three, or all four sides.

border-width tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful