Usability & Accessibility

CSS Fundamentals


Learning Objectives

  • You can explain how selectors, the cascade, and the box model affect a component.
  • You can inspect and adjust component spacing using the browser’s computed styles and box model.

CSS controls how the document is presented. Spacing, text, and layout affect whether people can read the information and find and operate its controls.

The browser preview below contains the complete example. Open its browser view and use the browser’s developer tools to inspect or temporarily change its CSS.

Rules, selectors, and values

The notice has internal spacing, a border and a link to the session details. The linked section is included so View session has a destination:

index.html
<article class="notice">
<h2>Room change</h2>
<p>The afternoon session has moved to room B204.</p>
<a href="#session-details">View session</a>
</article>
<section id="session-details">
<h2>Afternoon session</h2>
<p>Starts at 13:00 in room B204.</p>
</section>
<style>
.notice {
max-inline-size: 42rem;
padding: 1rem;
border: 1px solid #64748b;
border-radius: 0.5rem;
background: #fff;
color: #18212f;
}
.notice h2 {
margin-block: 0 0.5rem;
}
.notice p {
line-height: 1.5;
}
.notice a {
text-decoration-thickness: 0.12em;
}
</style>

The selector identifies the elements to which a rule applies: .notice selects elements with the notice class, while .notice h2 selects their h2 descendants. A declaration assigns a value to a property, such as padding: 1rem. Logical properties such as padding-inline and margin-block describe directions relative to the writing mode. For example, in horizontal English text, the inline direction runs horizontally and the block direction vertically.

In a Svelte component, a <style> block is normally scoped to that component. Put application-wide defaults in a shared stylesheet imported by the root layout; avoid redefining global defaults in each card.

The cascade and selector specificity

The cascade chooses which declaration applies when several rules set the same property. Among ordinary rules in the same stylesheet and cascade layer, a more specific selector wins. For example, .notice h2 selects a heading inside a notice and has higher specificity than h2, which selects every heading:

.notice h2 {
color: #1247ad;
}
h2 {
color: #18212f;
}

The notice heading is blue even though the h2 rule comes later. When selectors have equal specificity, the later declaration wins. In the preview’s Styles panel, add .notice { padding: 2rem; } after the existing rule, inspect the computed padding, and then move it before the original rule. Restore the example when you can explain the change.

The Styles panel also shows declarations that lost. Source order is only one part of the cascade: browser defaults, user styles and declarations marked !important also affect the result. Prefer simple class selectors over escalating specificity to fix each conflict. The MDN cascade guidance explains the full ordering.

The box model: content, padding, border, and margin

An element’s box has a content area, padding around that content, and a border around the padding. Margin provides space outside the border. The box-sizing property determines which of these dimensions a declared width includes.

By default, the content-box model adds padding and borders outside a declared width. Temporarily set the article to width: 240px; padding: 16px; border-width: 2px; box-sizing: content-box. At a sufficiently wide viewport, its rendered width including padding and borders is 276px: 240 + 32 + 4. Change only box-sizing to border-box; that width becomes 240px, leaving less space for content. Margin is outside both width calculations. Confirm both in the browser’s box-model inspector, then remove the temporary declarations.

A shared stylesheet can establish consistent defaults for a page:

*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
}
button,
input,
select,
textarea {
font: inherit;
}
img {
max-inline-size: 100%;
block-size: auto;
}

Long unbreakable content, fixed minimum widths, and grid children can still overflow with border-box. Inspect the box that exceeds its available space.

Compare content-box and border-box widths

0 / 5 points

Two panels each declare width 300px, padding 20px on each side, and a 2px border on each side, with no transforms. One uses content-box and the other border-box. Which gives their outer border widths and the border-box panel’s content width?

Use relative units such as rem for text-related dimensions. Avoid fixed heights for text containers: translations, browser zoom and user font settings can require more space. The next chapter keeps long content readable as the layout changes.

Optional: share values with CSS custom properties

A custom property names a reusable value. This example gives every notice the same padding; changing the root value updates that choice:

:root {
--notice-spacing: 1rem;
}
.notice {
padding: var(--notice-spacing, 1rem);
}

Custom properties inherit from a parent unless overridden. The second value in var() is a fallback when the named property is unavailable.

Check Your Understanding

  1. Why is source order only part of the cascade?
  2. What does border-box include in a declared width?
  3. Where would you inspect why a declaration is not taking effect?
  4. Why can .notice h2 override a later h2 rule when both are ordinary rules in the same stylesheet and cascade layer?