Step 3 / 933%

Step 3

Step 3 — Styling Tasks and Filters

20 min

Style the list, completed tasks, filters and the responsive layout.

Definition

Responsive design means building a page so that it adapts to every screen size, from phones to desktop computers. It relies in particular on media queries: CSS rules that only apply when a condition is met, for example a window width below a given value.

Goal of this step

You still need to style the filters, the task list, the "completed" state, the delete button and the footer. You will also add a rule for small screens.

Why use state classes?

In this step, you will write CSS for situations that don't exist yet: an active filter and a completed task. The idea is to prepare state classes, such as active or done, which JavaScript will add or remove later depending on what the user does.

This division of roles is very common: CSS describes what each state looks like, and JavaScript only decides which state each element is in. You will never need to change colors from the script, which keeps your code clean, readable and easy to maintain throughout the project, even as the application grows.

Completing todo.css

Add the following code after what you have already written in todo.css (at the end of the file).

todo.css (add at the end)
/* ─── Filters ───────────────────────────────── */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.filter {
  padding: 6px 14px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--color-text-soft);
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

.filter:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.filter.active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #ffffff;
}

/* ─── Task list ─────────────────────────────── */
.list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.task {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  transition: border-color 0.2s;
}

.task:hover {
  border-color: var(--color-primary);
}

.task input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--color-primary);
  cursor: pointer;
}

.task__text {
  flex: 1;
  overflow-wrap: anywhere;
}

/* Completed task */
.task.done .task__text {
  text-decoration: line-through;
  color: var(--color-text-soft);
}

.task__delete {
  padding: 2px 8px;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--color-text-soft);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s, color 0.2s;
}

.task__delete:hover {
  color: var(--color-danger);
  background: #fee2e2;
}

/* ─── "Empty list" message and footer ───────── */
.empty-message {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  color: var(--color-text-soft);
}

.app__footer {
  margin-top: 16px;
  text-align: right;
}

.btn--link {
  padding: 6px 0;
  font-weight: 500;
  color: var(--color-text-soft);
  background: none;
  text-decoration: underline;
}

.btn--link:hover {
  color: var(--color-danger);
}

/* ─── Small screens ─────────────────────────── */
@media (max-width: 480px) {
  body {
    padding: 24px 12px;
  }

  .app {
    padding: 16px;
  }

  .app__header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}

New things to look out for in the code

You will recognize notions you have already seen, but a few properties deserve a closer look:

  • border-radius: 999px: a very large radius that fully rounds the sides of the filter buttons, giving them their pill shape.
  • flex-direction: column: in the list, tasks stack vertically instead of lining up in a row.
  • list-style: none: removes the default bullets of the <ul> list.
  • flex-wrap: wrap: lets the filters wrap onto a new line when the screen is too narrow.
  • text-decoration: line-through: strikes through the text of a completed task.

Previewing a task

For now, no tasks exist: JavaScript will create them later. To see the result of your CSS, temporarily replace the line <ul id="list" class="list"></ul> in your HTML with this test code:

todo.html (temporary test)
<ul id="list" class="list">
  <!-- TEST: delete afterwards -->
  <li class="task">
    <input type="checkbox" aria-label="Mark as completed" />
    <span class="task__text">Review the CSS lesson</span>
    <button type="button" class="task__delete" aria-label="Delete task">×</button>
  </li>
  <li class="task done">
    <input type="checkbox" checked aria-label="Mark as completed" />
    <span class="task__text">Create the HTML structure</span>
    <button type="button" class="task__delete" aria-label="Delete task">×</button>
  </li>
</ul>

You get one task to do and one completed task (struck-through, greyed-out text). Warning: this is only a test. You can delete these two <li> elements in step 5, when JavaScript generates this HTML itself.

What you need to understand

  • .filter.active: this selector targets an element that has both the filter and active classes. JavaScript will move the active class from one button to another.
  • .task.done .task__text: the text is only struck through if the task has the done class. Checking a task will therefore simply mean adding or removing a class.
  • accent-color lets you color the native checkbox with your primary color, without rebuilding it.
  • overflow-wrap: anywhere prevents a very long text with no spaces from overflowing the task.
  • The media query max-width: 480px reduces the margins and stacks the header on mobile: this is responsive design.

Responsive design and media queries

The last rule in the code starts with @media (max-width: 480px). It means: "apply the following rules only if the window is 480 pixels wide or less". This is exactly the case on most phones held vertically.

Inside it, you reduce the margins of the page and the card, and you stack the header (title on top, counter below) to save space. Above 480 pixels, these rules are simply ignored. To test the result, you can narrow your browser window or turn on the "device mode" in the developer tools, which you can open with F12.

Common mistakes to avoid

  • Pasting the code in the wrong place: it must go at the end of todo.css, not replace the existing content.
  • Getting the space wrong in a selector: .task.done (no space) targets a single element with both classes, whereas .task .done (with a space) targets an element nested inside another.
  • Keeping the test HTML: don't forget to delete the two temporary <li> elements in step 5.

Check your work

  • The three filters appear as "pills", with All colored purple.
  • Hover over a task: its border turns purple. Hover over the cross: it turns red on a pink background.
  • The completed task has struck-through, greyed-out text.
  • Narrow the window: the card adapts without any horizontal scrolling.
Screenshot of the todo.html page in the browser