Styling with CSS

Making the application legible and navigable with a stylesheet

Concepts

Experimenting with CSS

A stylesheet for the application

Layout with Flexbox

Review what the LLM produced

Check for Understanding

What is the difference between padding and margin in the CSS box model?

Padding is the space between the element's content and its border; it shares the element's background color. Margin is the space outside the border between the element and its neighbors. Collapsing margins (where two adjacent vertical margins merge into one) is a common source of unexpected spacing.

If two CSS rules both target p elements—one with color: red and one with color: blue—which one applies, and why?

It depends on specificity and source order. If both rules have equal specificity (both use a plain tag selector), the one that appears later in the stylesheet wins. If one uses a class selector (e.g., .highlight { color: blue; }) it has higher specificity and wins regardless of order.

What does display: flex do to the children of an element?

It makes the element a flex container, which changes how its direct children are laid out. By default, children are placed in a row from left to right, and Flexbox controls how leftover space is distributed and how children align along the cross-axis. Children that would normally stack vertically (block elements) now sit side by side.

Why does the stylesheet need to be served from a route (or as a static file), rather than just linked as a file path?

A web browser cannot read files directly from the server's disk; it can only request URLs. The server must respond to the stylesheet's URL by sending the file's contents with a Content-Type: text/css header. Litestar's static files feature registers a URL prefix (typically /static/) that maps to a directory on disk.

Exercises

A color rule with no effect

The following CSS is supposed to make the page heading red, but has no visible effect. Find the bug and fix it without asking the LLM first, then reload the page to confirm the heading is red. Once it works, prompt the LLM to explain why this particular mistake is especially hard to spot.

h1 {
    font-color: tomato;
}

A style that has no effect

The following snippet is supposed to highlight error messages with a red background, but the paragraph looks completely unstyled. Find the bug and fix it without asking the LLM first. Then prompt the LLM to explain what strategy it would use to catch this kind of mistake before it reaches users.

<style>
.warning {
    background-color: #ffcccc;
    color: darkred;
}
</style>

<p class="error">File could not be saved.</p>

Audit for accessibility

Run the application and navigate through it using only the keyboard (Tab to move between links and buttons, Enter to activate them). Note every place where the focused element is not clearly visible. Ask the LLM to add focus styles to the stylesheet to fix each one.

Add a dark mode

Ask the LLM to add a dark-mode variant to the stylesheet using CSS custom properties (variables) for colors and the prefers-color-scheme: dark media query. Test it by changing your operating system's appearance setting.

Spot the magic numbers

Find every hard-coded pixel value in the generated stylesheet (e.g., padding: 16px or font-size: 14px). For each one, prompt the LLM to replace it with a named CSS variable and add a comment explaining what the value controls and why that specific number was chosen. Are its explanations reasonable?

Responsive layout

Ask the LLM to make the navigation bar collapse to a vertically stacked layout on screens narrower than 600px using a CSS media query. Test it by resizing the browser window or by using the browser's developer tools to simulate a mobile screen. Note any elements other than the nav that also need adjustment.