Sub topic 4
Border width and style
This chapter covers how Tailwind sets the width and style of a border. It assumes you know basic HTML and CSS, including the box model, and that you have a working Tailwind project or playground. It also assumes you have met utility classes before. Colors come from the previous chapters, so here we focus on width and style.
The working model
In plain CSS, a visible border needs a width, a style, and a color. If the style is none, which is the default, no border is drawn, whatever the width is.
Tailwind's preflight base styles change this default. They set border-style: solid and a neutral border color on every element, and they set the border width to 0. Because of this, one class is enough to show a border:
<div class="border p-4">A one-pixel solid border</div>The border class sets the width to 1px. The style and color come from preflight. Preflight is the reset stylesheet that Tailwind includes by default.
Width utilities
Width utilities have the form border-{n}. The number is in pixels.
| Class | Width |
|---|---|
| `border-0` | 0px |
| `border` | 1px |
| `border-2` | 2px |
| `border-4` | 4px |
| `border-8` | 8px |
For other values, use square brackets, such as border-[3px]. The next chapters cover this in more detail.
<div class="border-2 border-slate-300 p-4">Two pixels wide</div>Width changes the size of the element. Borders count toward the layout box, and Tailwind sets box-sizing: border-box, so a wider border takes space from the content area instead of growing the element.
Style utilities
The style utilities map directly to the CSS border-style values.
border-soliddraws a continuous line.border-dasheddraws a series of dashes.border-dotteddraws a series of dots.border-doubledraws two parallel lines. It needs a width of at least 3px to be visible.border-noneremoves the border style, so nothing is drawn.border-hiddenhides the border, and it wins over other borders when table cells collapse.
<div class="rounded-lg border-2 border-dashed border-slate-300 p-4">
Dashed border
</div>This is the first part of the example from the plan. The border-2 class sets the width, border-dashed sets the style, and border-slate-300 sets the color.
How the pieces combine
Common errors
- Writing
border-dashedwithoutborderorborder-2. The style is set, but the width is still 0. - Expecting
border-doubleto show two lines at 1px. The lines need room, so useborder-4or wider. - Switching style on hover without keeping the width. Changing only the style is fine, but changing the width makes the layout shift. Keep the width constant and change the color or style instead.
- Using
border-noneand expecting to restore it later withborder. Theborderclass only sets the width, so addborder-solidas well.
Exercise
An element has the classes border-dashed border-slate-400 p-4, but no border appears. Which class would you add to fix it, and why?
Show answer
Add border (or another width such as border-2). The style and color are set, but preflight sets the border width to 0, so nothing is drawn until a width utility is applied.
Exercise
Write the classes for a 4px double border. Why is 4px used instead of 1px?
Show answer
border-4 border-double. A double border draws two lines with a gap between them, and at 1px there is no room for both lines to be visible.