Tillitsdone
down Scroll to discover

CSS Border-Top-Width Enhance Your Design

Learn about the CSS border-top-width property.

Use it to control the thickness of the top border of elements.

Available options include keywords like thin, medium, thick, and length units like px, em, rem, vw, and vh.
thumbnail

Introduction

The border-top-width CSS property sets the width of the top border of an element. This property is essential for web design as it allows precise control over the top border’s thickness, enhancing visual appeal and element separation. You can set it using length units like pixels or predefined keywords like thin, medium, and thick.

Syntax

Here’s how to use the border-top-width property:

/* Keyword values */
border-top-width: thin;
border-top-width: medium;
border-top-width: thick;
/* Length values */
border-top-width: 10em;
border-top-width: 3vmax;
border-top-width: 6px;
/* Global keywords */
border-top-width: inherit;
border-top-width: initial;
border-top-width: revert;
border-top-width: revert-layer;
border-top-width: unset;

Values

The border-top-width property accepts several values:

Keywords

  • thin: Sets the top border to a thin width.
  • medium: Sets the top border to a medium width (default).
  • thick: Sets the top border to a thick width.

Length Units

  • <length>: Specifies the top border width using units like px, em, rem, vw, and vh.

Global Keywords

  • inherit: Inherits the value from the parent element.
  • initial: Sets the value to its default.
  • revert: Reverts to the user agent’s default stylesheet.
  • revert-layer: Reverts to the value for the current cascade layer.
  • unset: Resets the value to its natural state.

Note: The exact thickness of thin, medium, and thick can vary between browsers but always follow the pattern thin ≤ medium ≤ thick.

Formal Definition

The border-top-width property controls the width of the top border of an element.

  • Initial Value: medium
  • Applies To: All elements, including ::first-letter.
  • Inherited: no
  • Computed Value: Absolute length or 0 if border-top-style is none or hidden.
  • Animation Type: Length, interpolated as real, floating-point numbers.

Formal Syntax

border-top-width = <line-width>
<line-width> =
[<length [0,]>] |
thin |
medium |
thick

Examples

HTML

<div>Element 1</div>
<div>Element 2</div>

CSS

div {
border: 1px solid red;
margin: 1em 0;
}
div:nth-child(1) {
border-top-width: thick;
}
div:nth-child(2) {
border-top-width: 2em;
}

Result

Element 1 has a thick top border, while Element 2 has a top border width of 2em.

Additional Examples

HTML
<div id="thin">border-top-width: thin;</div>
<div id="medium">border-top-width: medium;</div>
<div id="thick">border-top-width: thick;</div>
<div id="length">border-top-width: 20px;</div>
<div id="initial">border-top-width: initial;</div>
CSS
div {
border-color: green;
border-top-style: solid;
margin: 1em 0;
}
#thin {
border-top-width: thin;
}
#medium {
border-top-width: medium;
}
#thick {
border-top-width: thick;
}
#length {
border-top-width: 20px;
}
#initial {
border-top-width: initial;
}
Result
  • The first <div> has a thin top border.
  • The second <div> has a medium top border.
  • The third <div> has a thick top border.
  • The fourth <div> has a top border width of 20px.
  • The fifth <div> has its top border width set to the default (medium).

Interactive Example

HTML
<button onclick="changeBorder('thin')">Thin</button>
<button onclick="changeBorder('medium')">Medium</button>
<button onclick="changeBorder('thick')">Thick</button>
<button onclick="changeBorder('20px')">20px</button>
<button onclick="changeBorder('initial')">Initial</button>
<div id="example">border-top-width: medium</div>
<script>
function changeBorder(value) {
document.getElementById('example').style.borderTopWidth = value;
}
</script>
CSS
#example {
border-color: green;
border-top-style: solid;
margin: 1em 0;
}

Click the buttons to see the border-top-width values in action.

Specifications

The border-top-width property is defined in the CSS Backgrounds and Borders Module Level 3 specification. This ensures consistent implementation across browsers.

Specification Details

Browser Compatibility

The border-top-width property is widely supported across all major browsers.

Supported Browsers

  • Google Chrome: Supported from version 1.0 (December 2008).
  • Firefox: Supported from version 1.0 (November 2004).
  • Internet Explorer / Edge: Supported from version 4.0 (September 1997).
  • Opera: Supported from version 3.5 (November 1998).
  • Safari: Supported from version 1.0 (June 2003).

Ensuring Compatibility

Test your web pages on different browsers to ensure consistent behavior. While border-top-width is well-supported, there may be slight differences in how keywords are interpreted. Using length units can provide more precise control.

Handling Older Browsers

For very old browsers, consider using polyfills or fallback styles, though this is rarely necessary due to widespread support.

Related Properties

Understanding related properties can help you achieve a cohesive design.

Other Border Width Properties

  • border-left-width: Controls the width of the left border.
  • border-right-width: Controls the width of the right border.
  • border-bottom-width: Controls the width of the bottom border.
  • border-width: Shorthand for setting the width of all four borders.

Border Style Properties

  • border-top-style: Defines the style of the top border.
  • border-left-style: Defines the style of the left border.
  • border-right-style: Defines the style of the right border.
  • border-bottom-style: Defines the style of the bottom border.
  • border-style: Shorthand for setting the style of all four borders.

Border Color Properties

  • border-top-color: Sets the color of the top border.
  • border-left-color: Sets the color of the left border.
  • border-right-color: Sets the color of the right border.
  • border-bottom-color: Sets the color of the bottom border.
  • border-color: Shorthand for setting the color of all four borders.

Shorthand Border Properties

  • border: Shorthand for setting the width, style, and color of all four borders.
  • border-top: Shorthand for setting the width, style, and color of the top border.
  • border-left: Shorthand for setting the width, style, and color of the left border.
  • border-right: Shorthand for setting the width, style, and color of the right border.
  • border-bottom: Shorthand for setting the width, style, and color of the bottom border.

Example of Using Related Properties

HTML
<div class="example">Example Element</div>
CSS
.example {
border-width: 2px;
border-style: solid;
border-color: blue;
border-top-width: 5px;
border-top-color: red;
padding: 10px;
margin: 20px;
}

In this example, the border-width property sets the width of all borders to 2px, while the border-top-width property specifically sets the top border width to 5px. The border-style property sets the style of all borders to solid, and the border-color property sets the color of all borders to blue. Additionally, the border-top-color property sets the color of the top border to red.

FAQs

What is the border-top-width property in CSS?

The border-top-width property sets the thickness of the top border of an element. This helps you create visually appealing designs.

What units can be used with border-top-width?

You can use units like px, em, rem, vw, and vh. You can also use keywords like thin, medium, and thick.

How do I apply a thick top border?

To apply a thick top border, use the thick keyword or specify a length value:

border-top-width: thick;

or

border-top-width: 5px;

Can I set different border widths for each side of an element?

Yes, you can set different border widths for each side using properties like border-top-width, border-left-width, border-right-width, and border-bottom-width. For example:

border-top-width: 3px;
border-left-width: 1px;

Does border-top-width affect the element’s layout?

Yes, border-top-width can affect the element’s overall height and layout, especially when combined with padding and margins.

What is the default value for border-top-width?

The default value is medium. If you don’t specify a value, the top border will have a medium thickness.

How do I inherit the border-top-width from a parent element?

To inherit the border-top-width value from a parent element, use the inherit keyword:

border-top-width: inherit;

What is the difference between border-top-width and border-width?

border-top-width sets the width of the top border specifically, while border-width sets the width of all four borders (top, right, bottom, and left) in one declaration.

Can I use border-top-width without setting a border style?

No, for border-top-width to be visible, you need to set a border-top-style property. The border-top-width property only defines the thickness; the style property determines how the border is displayed.

What happens if I set border-top-width to 0?

If you set the border-top-width to 0, the top border will not be visible. This can be useful if you want to remove the top border while keeping the other borders intact.

How can I revert to the default border-top-width?

To revert to the default value, use the initial keyword:

border-top-width: initial;

Is border-top-width supported in all major browsers?

Yes, border-top-width is widely supported in all major browsers, ensuring a consistent user experience across different platforms.

Can I animate the border-top-width property?

Yes, you can animate the border-top-width property using CSS animations or transitions. The animation type for border-top-width is a length, interpolated as real, floating-point numbers.

What is the computed value of border-top-width?

The computed value of border-top-width is the absolute length or 0 if the border-top-style is none or hidden. This means that the actual thickness of the border is calculated based on the specified value or the default if not set.

How can I ensure the border-top-width is consistent across different browsers?

To ensure consistency, use specific length units (e.g., px, em) instead of keywords (thin, medium, thick), as the interpretation of keywords can vary between browsers. Also, test your web pages on multiple browsers to identify and fix any inconsistencies.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.