The border around an element in CSS allows you to customize the style, color and width/size. Setting the border size is a common task in web development to delineate containers, buttons and other components. In this comprehensive guide, we‘ll explore the various ways to configure border size in CSS.

The Border-Width Property

The primary CSS property for controlling border thickness is border-width. It accepts values in pixels or preset keywords.

For example:

.box {
  border-width: 2px; 
}

This sets a 2 pixel border around .box.

You can also use keywords:

.box {
  border-width: thick;
}

The allowed keywords for border-width are:

  • thin (typically 1px)
  • medium (typically 3px)
  • thick (typically 5px)

Or you can set widths individually:

.box {
  border-top-width: 1px;
  border-right-width: 2px; 
  border-bottom-width: 3px;
  border-left-width: 4px;
}

When all four border widths are set individually like this, the order is: top, right, bottom, left.

Using Border Style Shorthand

You can set all the border properties at once with border style shorthand:

.box {
  border: 1px solid blue; 
}

This sets the width, style and color in one declaration.

The format is:

border: [width] [style] [color];

Let‘s dissect the shorthand syntax:

  • Width – Set border thickness with pixels or keywords
  • Style – Border style such as solid, dashed, dotted etc.
  • Color – The border color

So an equivalent way to set a 2px red dotted border is:

.box {
  border: 2px dotted red; 
}

You can omit properties you don‘t want to set. For example, to only set width and style:

.box {
  border: 3px dashed;  
}

Border Width with Inline CSS

You can also directly set the border property on an element with inline CSS styles:

<div style="border: 5px solid blue;">...</div> 

This avoids needing an extra CSS rule just for styling this single div.

One issue with inline styles is specificity. Any inline border styles will override CSS rules. This can make maintenance harder long-term. But for one-off situations, inline styles are useful for rapid prototyping and situations where high specificity is required.

Setting Individual Border Sides

CSS provides longhand properties for styling each border side independently:

.box {
  border-top-width: 5px;
  border-right-width: 10px;
  border-bottom-width: 20px;
  border-left-width: 1px; 
}

The independent border properties are:

  • border-top-width
  • border-right-width
  • border-bottom-width
  • border-left-width

This allows crafting borders with unique styles per side – like having one thick and one thin side.

diagram showing border sides of an element

The four border properties let you style each side independently

Border colors and styles can also be set individually this way using border-[side]-color and border-[side]-style properties.

Controlling Border Size with CSS Classes

For reusable UI components, you can create CSS classes that handle the border styling:

.thick-border {
  border: 5px solid black;
}

.thin-border {
  border: 1px solid black;
}  

Then these classes can be added to elements with standard class attribute:

<div class="thick-border">Thick border</div>

<div class="thin-border">Thin border</div>

The benefit here is the border size can be changed in one place – the CSS file – instead of individually styling elements. This is more maintainable at scale.

Size Keywords: Thin, Medium, and Thick

As we saw earlier, keywords can be used as shorthand for common border widths:

.border {
  border: thin solid red; 
}

The actual pixel size varies by browser and OS, but the relative widths are:

  • thin – ~1px
  • medium – ~3px
  • thick – ~5px

For instance:

So using these keywords can make code that works well across browsers and devices. Defining explicit pixels risks inconsistencies in rendering.

Border Radius and Size

Border radius can clip a portion of the border edges. In effect, the visible border size is reduced with high radius values.

For example, consider two boxes with 5 pixel borders:

.box-sharp {
  border: 5px solid red;  
  /* Sharp corners */
}

.box-round {
  border: 5px solid red;
  border-radius: 40px;
  /* Rounded corners */  
}
Sharp corners

Rounded corners

Notice the visible border size is smaller on the rounded box, since the corners are clipped. Just be aware of this interaction when using border radius. Set a larger than expected border width if needed to compensate.

Browser Support

The main border properties like border-width, border-style and border-color have excellent cross-browser support. Border size can be used reliably in CSS without worries.

According to CanIUse.com, global support for basic border features is:

  • Chrome: Full support
  • Firefox: Full support
  • Safari: Full support
  • Edge: Full support
  • IE: Full support from IE6+

The individual border side properties like border-left-width have good but slightly less consistent support. So try to fall back to the all-sides border shortcut if supporting legacy browsers from 1990s.

Conclusion

Adjusting border size is a necessary CSS skill for highlighting interface elements, grouping common components visually, and laying out content sections.

Key takeaways:

  • Use the border-width property or border shorthand syntax
  • Pixel values and keywords like thick can size borders
  • Side properties independently style each border edge
  • Border radius clips and reduces visible border size

Hopefully this gives you more confidence for handling border size across projects. The various sizing methods scale well from simple to advanced layouts.

There‘s also much more to explore with borders like responsive styling, shapes and animations. But width and thickness are the foundations to master first.

Similar Posts