“Everything from here to here is one chunk” — a border is what makes that visible to the eye. Boxing off a note, drawing the edge of a card. With just border, the shape of a part becomes much clearer.
Let’s write one
.box {
border: 2px solid teal;
}A part with class="box" gets a teal border. The three things lined up after border each mean this:
2px… the width of the line (number + unit)solid… the style of the line (solidis a plain solid line)teal… the color of the line (a color name or code)
You can read it left to right: “a 2-pixel, solid, teal border.” Being able to write all three at once, joined by spaces, is what makes border so convenient.
Change the line style (solid, dashed, dotted) and the border’s look changes like this.
solid.Adding a line to just one side
border puts a line on all four sides, but with a direction-specific name like border-bottom (bottom only) or border-left (left only), you can add a line to just one side.
.header {
border-bottom: 1px solid #e9e3d9;
}
.quote {
border-left: 4px solid #e8734e;
}border-bottom is often used to draw a line at the bottom edge of a header to show “the header ends here,” and border-left to add a thicker line on the left of a quote to signal “this is a quote.” The same idea works for all four directions with -top and -right too.
When you want to remove a border: border: none;
Buttons (<button>) and input fields (<input>) come with a border from the start. When you want to style them your own way, a common move is to clear it first with border: none;.
.btn {
border: none;
background-color: teal;
color: white;
}Not just “adding a line,” but “removing the default line” too — that’s one of border’s important jobs.
Summary of this lesson
- A border is
border - Line up width, style, and color, separated by spaces
- Think of a border and
paddingas a set so things don’t feel cramped - Use
border-bottom/border-leftto add a line to just one side
