CSS controls how your HTML looks. This lesson covers selectors, the box model, basic layout, the browser's inspector, and the Sass basics we use in all projects.
Important
Work on this lesson in
teachermode.
Learning goals
- You can explain how the cascade and specificity decide which CSS rule applies to an element.
- You can explain the box model and the difference between inline and block elements.
- You can apply basic styling โ colors, typography, spacing โ and keep spacing consistent across an application, e.g. by limiting yourself to a handful of margin values.
- You can explain what margin collapse is and recognize it when spacing looks wrong.
- You can lay out two columns side by side with the basics of Flexbox, e.g. a fixed sidebar next to a content area that fills the rest. Flexbox in depth and CSS Grid come in later cards.
- You can use positioning for small adjustments and explain how
position: relativechanges the coordinate origin for its children. - You can inspect any element's styles in the browser's developer tools, see where each rule comes from, and try changes live.
- You can explain what a reset stylesheet does and why we use one.
- You can write stylesheets with a preprocessor โ Sass in the indented syntax we use โ using variables, nesting, partials and mixins, and you follow the stylesheet structure of the project you're in.
Resources
Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.
Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.
Selectors, cascade and the box model
- ๐ MDN: Basic selectors Show archive.org snapshot , Attribute selectors Show archive.org snapshot and Combinators Show archive.org snapshot โ lessons with live editors
- ๐ MDN: Handling conflicts Show archive.org snapshot โ cascade, specificity, inheritance and source order in one lesson
- ๐ MDN:
The box model
Show archive.org snapshot
โ block vs. inline, padding/border/margin,
box-sizing - ๐ The Rules of Margin Collapse Show archive.org snapshot โ interactive; skip the negative-margin edge cases. Shorter: MDN's Mastering margin collapsing Show archive.org snapshot
- ๐ Learn CSS (web.dev) Show archive.org snapshot โ the longer text alternative to the MDN lessons; read Box Model, Selectors, The Cascade, Specificity, Inheritance, Color, Sizing Units, Spacing, Borders and Typography
- โถ๏ธ The 6 most important CSS concepts for beginners Show archive.org snapshot โ Kevin Powell, 29 min; a good first hour if you prefer video
Layout
- ๐ MDN: Flexbox Show archive.org snapshot โ read up to and including "Horizontal and vertical alignment"; the rest comes in a later card
- ๐ฎ Flexbox Froggy Show archive.org snapshot โ 24 short levels
- โถ๏ธ Learn flexbox the easy way Show archive.org snapshot โ Kevin Powell, 34 min
- ๐ MDN:
Positioning
Show archive.org snapshot
โ especially "Positioning contexts":
position: relativeon a parent moves the origin for absolute children
Tools
- ๐ฎ View and change CSS Show archive.org snapshot โ Chrome DevTools walkthrough; also Inspect Flexbox Show archive.org snapshot . Firefox users: MDN's Debugging CSS Show archive.org snapshot
- ๐ฎ CSS Specificity Calculator Show archive.org snapshot โ paste two selectors, see who wins and why
Reset stylesheets and Sass
- ๐ Reset style sheet Show archive.org snapshot โ Wikipedia; short
- ๐ Sass Basics Show archive.org snapshot โ switch every example to "Sass syntax"; variables, nesting, partials, mixins
- ๐ Sass:
The indented syntax
Show archive.org snapshot
โ where
.sassdiffers from.scss - ๐ Sass:
@useShow archive.org snapshot and@importShow archive.org snapshot โ in our projects we@importour own Sass files (variables, mixins) and@useSass's built-in modules (e.g. color functions). Current Dart Sass prints a deprecation warning for every@import; that is expected and not something you broke
Note
Sass has two different syntaxes,
.scssand.sass. We use the indented syntax (.sass).
Exercises
Basic layouting
We want to layout a classic website with a header, a sidebar with navigation links, a big content area and a footer:
+------------------------------------------------------+
| |
| BIG LOGO |
| |
+------------+-----------------------------------------+
| | |
| Link 1 | HEADLINE |
| | |
| Link 2 | Lorem ipsum dolor amit. |
| | At vero eos et accusam et justo. |
| Link 3 | Stet clita kasd gubergren, no sea. |
| | Sed diam nonumy eirmod tempor. |
| | Lorem ipsum dolor amit. |
| | At vero eos et accusam et justo. |
| | Stet clita kasd gubergren, no sea. |
| | Sed diam nonumy eirmod tempor. |
| | |
| | |
| | |
| | |
| | |
| | |
+------------+-----------------------------------------+
| Footer |
+------------------------------------------------------+
Here are some more details how the layout should behave:
- Give each element (header, sidebar, etc.) a distinct background color to see where boxes start and end.
- The layout should fill the entire width and height of the screen.
- The sidebar should have a fixed width. The content area takes up the remaining width of the screen.
- The header should have a fixed height. The sidebar and content area takes up the remaining height of the screen.
- The layout should work on a variety of screen resolutions, not just your particular monitor. You can assume a minimum resolution of 800ร600 pixels.
- The footer is always at the bottom edge. The sidebar and content area stretches to fill all available height.
- The screen should only show scrollbars if the text in the main content area exceeds the available viewport height.
Build the layout with Flexbox: one column layout that stacks header, middle and footer vertically; inside the middle, a row layout that puts the sidebar next to the content area. (CSS Grid could build this in a single container โ you'll meet it in a later card.)
Tip
You can implement this exercise in plain CSS, you don't need to use Sass.
Give each layout element a CSS class so you can apply styles. Some layout techniques may require more HTML elements than others, or a different nesting.
You can generate paragraphs of random text on https://loremipsum.de/ Show archive.org snapshot .
A coat of paint for MovieDB
Apply some basic styles to MovieDB. You should author your styles in Sass, using the indented syntax (.sass).
Put one .sass file per concern directly into app/assets/application/. They are picked up automatically by the glob in application.js โ the same way your JavaScript will be, in a later card. Keep shared variables and mixins in partials outside that folder (e.g. app/assets/shared/_variables.sass) and @import them where you need them, so the glob doesn't compile them on their own.
A partial that holds only variables and mixins produces no CSS of its own, which is what makes it safe to import from many files. Never put a style rule in one, or you will get that rule once per file that imports it.
Here are some hints for this exercise:
- The UI does not need to be a master piece, but it should be tidy and clear.
- If you have generated styles from Rails scaffolding, delete them all. They don't look nice and we do not use them.
- Pay attention to the margins in your styles. Elements that belong together logically (e.g. two navigation sections) should be closer to each other than elements that have no relation.
- Try to limit your margins to very few (e.g. 6) distinct values across the entire application to achieve a consistent look & feel.
- Your MovieDB should have a navigation bar that lets the user switch between the different model screens (E.g.
Movies | Actors | Roles). - Please don't use Bootstrap or another CSS framework. If your MovieDB is currently using a CSS framework, remove it and build your own styles from scratch.
Tip
Your MovieDB uses Rails 8 with esbuild. Sass support is already configured through the
esbuild-sass-pluginlibrary โ there is nothing to install.
Tip
Every time you add a new SASS or JS file to your application (e.g.
app/assets/application/navbar.sass), you need to restart your development server for it to show up in your browser. This is caused by a limitation of the esbuild-plugin-import-glob Show archive.org snapshot , which we use to import all assets in this directory: Seeimport './application/**/*.sass'in yourapplication.js. Direct import statements likeimport 'app/assets/application/navbar.sass'would work without restarting the server. You'll learn the details of esbuild and the frontend build pipelines in a later card, for now it's enough to know why new files might be missing.