2. CSS Styling & Layouts
CSS: Painting the Web
Cascading Style Sheets (CSS) brings your HTML to life. It controls colors, spacing, typography, and complex layouts. In this module, we break down CSS conceptually so you don't just memorize properties, but understand how the rendering engine thinks.
CSS Selectors: Elements
The most basic way to style something is to target the HTML element directly. This will apply the style to EVERY instance of that tag on the page.
tag on the page */ p { color: #333333; font-size: 16px; line-height: 1.5; }
CSS Selectors: Classes
Classes are the most common way to style elements. You add a class="card" to your HTML, and target it in CSS with a dot (.). Classes can be reused on multiple elements.
CSS Selectors: IDs
IDs are meant to be entirely unique. An ID should only appear ONCE per HTML page. You target them in CSS with a hash (#). They are very powerful and will override class styles.
The Box Model: Margin
Every element in CSS is a box. Margin is the transparent space on the OUTSIDE of the box. It pushes other elements away.
The Box Model: Padding
Padding is the space on the INSIDE of the box, between the border and the actual content (text/images). It makes elements look breathable.
The Crucial Reset: Box-Sizing
By default, if you give a box a width of 100px and a padding of 20px, the box actually becomes 140px wide on the screen. This makes layout math a nightmare. We fix this by forcing padding to be calculated INSIDE the width.
Flexbox: The Container
Flexbox is a 1-dimensional layout system used for rows OR columns. To use it, you must first apply display: flex to a parent container. By default, it places all its children in a horizontal row.
Flexbox: Justify Content (Main Axis)
justify-content aligns items along the primary direction (horizontally, if it's a row).
Flexbox: Align Items (Cross Axis)
align-items aligns items perpendicular to the primary direction (vertically, if it's a row).
CSS Grid: 2D Layouts
While Flexbox is for a single line, CSS Grid is for entire page layouts involving both rows and columns simultaneously.
Responsive Design: Media Queries
A website must look good on a phone and a 4K monitor. Media Queries allow you to write CSS rules that only activate when the screen size matches your condition.
Knowledge Check
Ready to test your understanding of 2. CSS Styling & Layouts?