Emmanuel Akinfulubi

HTML and CSSPractical III

Print stylesheet with @media print in CSS

45 A page that prints properly

fair

Add print styles: drop the interface furniture, make the type economical, print link destinations that would otherwise be lost, and stop headings being orphaned at the foot of a page.

Stuck? Three hints, smallest first
  1. 1. @media print is a media type, not a feature query, so it takes no parentheses.
  2. 2. attr(href) in generated content is how you print the URL behind a link.
  3. 3. break-inside: avoid and break-after: avoid are the modern replacements for the page-break-* properties.

index.html

<nav class="bar no-print" aria-label="Main">
  <a href="#routes">Routes</a>
  <a href="#fares">Fares</a>
</nav>

<article class="ticket">
  <h2>Boarding pass</h2>
  <p>Lagos to Abuja, 06:30, bay B, seat 14.</p>
  <p>Conditions at <a href="https://example.com/terms">example.com/terms</a>.</p>
</article>

<p class="hint no-print">Use your browser’s print preview to see the rules below
  applied.</p>

style.css

.bar {
  display: flex;
  gap: 14px;
  margin-bottom: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #f2f3f5;
  font-size: 0.85rem;
}

.bar a { color: #3055d8; }

.ticket {
  padding: 12px;
  border: 1px solid #c9cad1;
  border-radius: 8px;
  font-size: 0.85rem;
}

.ticket h2 { margin: 0 0 6px; font-size: 1rem; }
.ticket p { margin: 0 0 6px; }

.hint { margin: 10px 0 0; font-size: 0.78rem; color: #5c5c66; }

@media print {
  /* Interface furniture is not part of the document. */
  .no-print { display: none; }

  body {
    font-size: 11pt;      /* pt is the sane unit on paper */
    color: #000000;
    background: #ffffff;
  }

  .ticket {
    border: 1px solid #000000;
    border-radius: 0;
    break-inside: avoid;  /* keep the pass on one page */
  }

  h2 {
    break-after: avoid;   /* never leave a heading stranded at the foot */
  }

  /* A printed link loses its destination, so print it. */
  a[href^='http']::after {
    content: ' (' attr(href) ')';
    font-size: 9pt;
  }
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 45

const print = (/@media print \{([\s\S]*?)\n\}\s*$/.exec(css) || [, ''])[1];

test('there is a print block, with no parentheses on the media type', /@media print \{/.test(css), true);
test('the interface furniture is dropped', /\.no-print \{ display:\s*none/.test(print), true);
test('and both such elements are marked in the markup', (html.match(/no-print/g) || []).length, 2);
test('type is sized in points for paper', /font-size:\s*11pt/.test(print), true);
test('the ticket is kept on one page', /break-inside:\s*avoid/.test(print), true);
test('and headings are not stranded at the foot of one', /break-after:\s*avoid/.test(print), true);
test('link destinations are printed from the attribute', /content:\s*' \(' attr\(href\)/.test(print), true);
test('the old page-break properties are not used', /page-break-/.test(css), false);

Output


      

@media print takes no parentheses because print is a media type, not a feature — @media (print) is invalid and silently does nothing, which is the usual first mistake. The rest is about what paper cannot do: it has no navigation, so the bar goes; it has no hover and no clicking, so a link’s destination has to be printed with attr(href) or the information is simply lost; and it has page boundaries, so break-inside and break-after stop a ticket being split in half or a heading being left alone at the bottom. pt is the right unit here for once, because a physical page has physical dimensions.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.