Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Frontend Basics for Backend Developers  ›  Lesson

CSS — Files You Serve

CSS 10 minCSS runs in the browser, not on the server, so a backend's job is to serve stylesheets at the right path with the right Content-Type and sensible caching.

CSS — Files You Serve

A backend developer rarely writes much CSS, but you will be asked why "the site looks broken" after a deploy. The HTML arrives, the text is there, and everything is plain black on white. Nine times out of ten the stylesheet never reached the browser, and that part is the backend's job.

The rule

CSS runs in the browser; the server only delivers the file.

CSS is a set of rules that say how elements should look:

.book-card {
  border: 1px solid #ddd;
  padding: 1rem;
}

The HTML points at it:

<link rel="stylesheet" href="/static/site.css">

When the browser meets that tag, it makes a separate GET /static/site.css. From the server's point of view, CSS is a static file: it does not change per user and no code runs to make it. Serving it well comes down to three things:

  • The right path: the file must exist at the URL the HTML names. A 404 here gives you an unstyled page.
  • The right type: the response must say Content-Type: text/css. Browsers can refuse a stylesheet sent with the wrong type.
  • Sensible caching: CSS rarely changes, so browsers should keep a copy instead of downloading it on every page.

Caching creates a problem: if a browser keeps site.css for a month, how does it see tomorrow's change? The usual answer is a content hash in the file name. The build step produces site.3f9a1c.css; when the file changes, its name changes to something like site.b72e05.css, and the HTML points at the new name.

HTTP/1.1 200 OK
Content-Type: text/css
Cache-Control: public, max-age=31536000, immutable

A hashed file can be cached for a year, because any new version is a different URL.

The shortcut

For a styling problem, follow the file, not the rules: find the <link>, find its request in the browser's network panel, then read the status code and Content-Type. Only if those are right is it a CSS question for the frontend.

The unstyled page after a deployStep 1 of 4
  1. Find the link

    The page's HTML has <link rel="stylesheet" href="/static/site.3f9a1c.css">.

The trap is the opposite mix: long-caching the HTML and giving assets fixed names. Then users keep old pages or old styles long after you deploy.

Drills

Answer fast — trust the rule.

quick drill

Which Content-Type should a server send with a .css file?

quick drill

Where does the CSS in a stylesheet actually run?

quick drill

Why rename site.css to something like site.3f9a1c.css on each build?

quick drill

A page loads unstyled and the network panel shows the stylesheet request returned 404. Where is the fix?

Remember

  • CSS runs in the browser; the server only delivers the file.
  • Follow the file: link, request, status, Content-Type.
  • Cache hashed assets for a long time, and keep HTML short-lived.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainAn unstyled page

After a deploy, the bookshop's pages load but look completely unstyled. Explain what a backend developer should check, and how file names with a content hash help browsers cache stylesheets safely.