Draw Borders, Corners and Shadows

Your event cards now have room inside, space between them, and widths that mean what they say. Their edge, though, is still the border: 2px solid var(--green) line you typed in See Every Element as a Box without taking it apart.

Borders, corners, and shadows do a lot to make a page look finished. In this lesson, you’ll read and write the border shorthand, round the cards’ corners, rule the cells of the plots table, and add a shadow. You’ll also learn to predict which of these push other boxes around and which are only painted on, and why the focus outline must stay. Start with the card’s border, the line you already have.

Draw a Border and Round Its Corners

The border declaration in your card rule packs three values into one line, and each one sets a different part of the border:

  • The width, 2px, is how thick the line is.
  • The style, solid, is what kind of line it is. Others include dashed, dotted, and double.
  • The color, var(--green), is the garden’s green from your tokens.

The three values can come in any order because each kind of value looks different from the others. But the style is the one you can’t leave out. Unlike the one to four values of padding, where a missing value copies one you gave, border resets every part you leave out to its default, and the default style is none.

So border: 2px var(--green) is valid CSS that draws no border at all, and nothing warns you. If a border doesn’t show, check its style first.

You can also set one side at a time. The properties border-top, border-right, border-bottom, and border-left take the same three values, so border-bottom: 1px solid var(--green) draws a line under a box and nowhere else.

Rosa would like the cards to look less like boxes and more like notices, which starts with softer corners. The border-radius property rounds them. Add it to the card rule in events.css:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px 24px;
  border: 2px solid var(--green);
  margin-bottom: 24px;
  border-radius: 8px;
}

The value is the radius of the curve at each corner, so a larger value gives a rounder corner. Save, and both the border and the honeydew background inside it follow the curve:

The work day card with rounded corners, its green border and honeydew background curving together at each corner.
The Event Cards With Rounded Corners

Rounding the corners doesn’t change the box’s size. The card takes up exactly the same rectangle on the page as before, and only the painting at its corners changes.

Rule the Plots Table

Rosa’s other request is for the plots table, where the rows of plot numbers, sizes, and names run together. Lines between the cells and a little room inside each one would make the table easier to read across. You’ll practice on a sample of the table, its first four plots, so your real plots.html stays as it is until the course’s project.

Create a new file, events/plots-sample.html, that links to the same practice stylesheet as the events page:

Language: HTML Filename: events/plots-sample.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport"
      content="width=device-width, initial-scale=1">
    <title>Plots Sample | Elm Street Community Garden</title>
    <link rel="stylesheet" href="events.css">
  </head>
  <body>
    <main>
      <h1>Garden Plots</h1>
      <table>
        <caption>The first four plots</caption>
        <thead>
          <tr>
            <th scope="col">Plot</th>
            <th scope="col">Size</th>
            <th scope="col">Tended by</th>
          </tr>
        </thead>
        <tbody>
          <tr><th scope="row">1</th>
            <td>3 × 4 m</td><td>Ana</td></tr>
          <tr><th scope="row">2</th>
            <td>3 × 4 m</td><td>Jamal</td></tr>
          <tr><th scope="row">3</th>
            <td>3 × 4 m</td><td>Mei</td></tr>
          <tr><th scope="row">4</th>
            <td>3 × 4 m</td><td>Tomás</td></tr>
        </tbody>
      </table>
    </main>
  </body>
</html>

Every cell of a table, header cell or data cell, is a box like any other, so it takes padding and a border. Add a rule for both kinds of cell at the end of events.css:

Language: CSS Filename: events/events.css
th,
td {
  padding: 8px 16px;
  border: 1px solid var(--green);
}

Save and open http://127.0.0.1:3000/events/plots-sample.html:

The sample plots table with a separate green frame around every cell and a thin gap between neighboring frames, so every line between cells is doubled.
Each Cell Draws a Border of Its Own

Each cell has room around its text now, but every line between two cells is doubled. That’s because each cell draws its own border, and by default the table also keeps a gap of 2px between its cells. So between plot 1’s size and Ana, you see the right border of one cell, the gap, and then the left border of the next.

The table that holds the cells decides how their borders meet through its border-collapse property. Add a rule for table after the cell rule:

Language: CSS Filename: events/events.css
table {
  border-collapse: collapse;
}

The value collapse makes neighboring cells share one border instead of drawing two with a gap between them. Save, and the table is ruled with single lines:

The sample plots table ruled with single green lines between all its cells, with room around the text in each cell.
Collapsed Borders Give the Table Single Lines

Your real plots.html keeps its zebra stripes from style.css for now. When the course’s project moves the design into the site’s stylesheet, these two rules go with it, and the full table gets the same lines.

Highlight Without Moving Anything

Back on the events page, Rosa would like the cards to sit slightly above the page, like paper pinned to a board. A shadow does that. Add a box-shadow to the card rule:

Language: CSS Filename: events/events.css
.event-card {
  background-color: honeydew;
  padding: 16px 24px;
  border: 2px solid var(--green);
  margin-bottom: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px oklch(0 0 0 / 0.2);
}

The four values are, in order, the horizontal offset (0), the vertical offset (2px), the blur (8px), and the color, black at an alpha, or opacity, of 0.2. The vertical offset places the shadow below the card. Save, and each card casts a soft shadow along its lower edge:

The work day card with rounded corners and a soft gray shadow along its lower edge, with the seed swap card below it.
A Soft Shadow Lifts Each Card Off the Page

Look in the gap below the work day card: the soft gray band there is the only change, and nothing else on the page moved. A shadow is painted outside the box but isn’t one of its layers, so it takes no space. If you select the card and open the box diagram in the Computed tab, the margin, border, and padding are exactly the values they were before.

Rosa would also like the card under the pointer to stand out. You can make it darker and lift it a little higher by changing only two painted things, the border’s color and the shadow. Add a hover rule after the card rule:

Language: CSS Filename: events/events.css
.event-card:hover {
  border-color: var(--green-dark);
  box-shadow: 0 4px 16px oklch(0 0 0 / 0.25);
}

The border-color property sets the color alone, so the width stays at the 2px the card rule set. Save and move the pointer onto the work day card. It darkens and lifts, and the seed swap card below stays where it was:

The hovered work day card with a dark green border and a deeper shadow, its text and the seed swap card below in the same places as before.
A Darker Border and Deeper Shadow on Hover, With Nothing Moving

Watch out for the tempting alternative, a thicker border such as border: 4px solid var(--green-dark). A border is one of the box’s layers, so on hover this card would grow 4px taller, and its text would move 2px in from each edge. The seed swap card below it would be pushed down too, as this hover with that thicker border and the card’s original shadow shows:

The hovered work day card with a thick dark green border, its heading set slightly further in, and the seed swap card below sitting four pixels lower than before.
For Comparison, a Thicker Border on Hover Pushes the Next Card Down

Compare it with the previous image. The top of the seed swap card sits a little lower, and Next Work Day has moved in from the card’s edge. On the page, that shift happens every time the pointer enters or leaves a card, so the content below jumps up and down.

The card isn’t a link or a button, so the hover is decoration for mouse users. Keyboard users move between the links inside the cards instead, and those links show where the focus is in their own way.

One more line can appear on a box’s edge, and it’s the one keyboard users rely on. Press Tab until The work day’s jobs has the focus. It turns brown, from the a:hover, a:focus-visible rule already in your file, and it gets the browser’s dark focus ring. That ring is the link’s outline, a line that’s drawn like a border, outside the link’s border. It takes no space, so the text around the link doesn’t move when the ring appears.

Watch out: the outline is how keyboard users see where they are, and the brown color alone is too weak a cue, so never remove the outline without drawing a visible one in its place.

You can keep the browser’s ring. If you’d rather draw your own, it can match the garden’s colors and stand a little clear of the text. Set it on :focus-visible, which applies when the browser decides focus should show, as it does after Tab. The outline property takes the same three values as border, and outline-offset sets a gap between the outline and the box. Add this rule at the end of events.css:

Language: CSS Filename: events/events.css
.event-card a:focus-visible {
  outline: 3px solid var(--green-dark);
  outline-offset: 2px;
}

Save, then in the browser, press Tab until the work day’s link has the focus again:

The work day card's link, The work day's jobs, in brown with a thick dark green outline a little way out from its text, and the card's text in its usual place.
A Focus Outline That Takes No Space

The link now has a thick dark green outline with a little gap around its text, and the card’s text sits exactly where it did before.

A good test of any focus style is to put the mouse away and press Tab through the whole page. You should always be able to see where you are. To check that the ring stands out against your own colors, ask the mentor:

If you’re not sure yet which edge styles can move other boxes, the button below asks the mentor to quiz you on it:

Your cards have rounded borders and a soft shadow, the plots sample is ruled, the cards darken and lift on hover without moving anything, and the links keep a visible focus outline. The cards’ backgrounds are still a single plain color, though. In the next lesson, Fill Boxes With Images and Gradients, you’ll give a box a photo or a gradient as its background.

Become a Member to join the conversation.