Learn how to make your website look great on both a desktop computer and a mobile phone.
Have you ever wondered how a website 'knows' it's being viewed on a tiny phone versus a massive gaming monitor? It's not magic—it's a digital superpower called Responsive Design.
In the early days of the web, designers built for desktops first and tried to 'shrink' sites for phones. Today, we do the opposite: Mobile First. This means we write our base CSS for the smallest screens first. Why? Because it's much easier to add complexity as screen space increases than it is to remove it. By starting with a single-column layout, we ensure the most important content is accessible to everyone. We use fluid layouts where widths are defined by percentages rather than fixed pixels, such as instead of . This ensures the content flows like water to fit its container.
Quick Check
Why is 'Mobile First' considered more efficient than 'Desktop First'?
Answer
It is easier to add features and expand layouts for larger screens than it is to shrink and remove complex elements for small screens.
1. Define your base styles for a mobile device (no media query needed yet). 2. Set the body background to light blue. 3. Set the main content width to .
```css body { background-color: lightblue; font-size: 16px; } .container { width: 100%; } ```
A Media Query is like a logical 'if-statement' for your CSS. It asks the browser: 'Is the screen at least this wide?' If the answer is yes, the browser applies a new set of rules. The syntax uses the `@media` rule combined with a feature like `min-width`. For example, if we want to change the background color only when the screen is wider than , we wrap that CSS in a media query. This allows us to create conditional styling, where the look of the site evolves as the user resizes their window or rotates their device.
Quick Check
What does the 'min-width' property in a media query actually check?
Answer
It checks if the browser viewport is equal to or wider than the specified value.
1. Start with a mobile-first stack (1 column). 2. Add a media query for tablets (). 3. Inside the query, change the layout to 2 columns using percentages.
```css / Mobile First / .column { width: 100%; }
/ Tablet and up / @media (min-width: 600px) { .column { width: 50%; float: left; } } ```
A Breakpoint is the specific pixel width where a website's layout 'breaks' and needs to change to stay readable. While every site is different, the industry uses standard ranges based on common devices. We usually target: - Small (Phones): to - Medium (Tablets): to - Large (Laptops/Desktops): and up. Instead of designing for every single phone model, we design for these ranges. This ensures that whether a user has an iPhone, a Galaxy, or a Pixel, the site looks intentional and professional.
Create a layout that changes from 1 column (mobile) to 2 columns (tablet) to 4 columns (desktop).
1. Base: `.card { width: 100%; }` 2. Tablet (): `.card { width: 50%; }` 3. Desktop (): `.card { width: 25%; }`
This uses the logic: as screen real estate increases.
Which CSS rule is used to create a media query?
In a Mobile First approach, where do you write your desktop-specific styles?
A breakpoint is a physical button on a smartphone that changes the website layout.
Review Tomorrow
In 24 hours, try to explain the difference between 'min-width' and 'max-width' to a friend, and sketch a 3-column layout turning into a 1-column layout.
Practice Activity
Open your favorite website on a desktop and slowly shrink the browser window width. Try to spot the exact 'breakpoints' where the menu or images change size!