Responsive design makes websites look good on all devices like phones, tablets, and computers. It changes the layout to fit the screen size.
What is responsive design in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
@media (max-width: 600px) { /* CSS rules here for small screens */ }
The @media rule lets you apply CSS only when the screen matches certain conditions.
Common conditions check screen width to change styles for phones or tablets.
@media (max-width: 600px) { body { background-color: lightblue; } }
@media (min-width: 601px) { body { background-color: white; } }
img {
max-width: 100%;
height: auto;
}This example shows a simple page with two columns on wide screens. On small screens (600px or less), the columns stack vertically. The image and text resize nicely.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Responsive Design Example</title> <style> body { font-family: Arial, sans-serif; margin: 1rem; background-color: white; color: black; } header { background-color: #4CAF50; color: white; padding: 1rem; text-align: center; } main { display: flex; gap: 1rem; } article { flex: 1; background-color: #f4f4f4; padding: 1rem; border-radius: 0.5rem; } img { max-width: 100%; height: auto; border-radius: 0.5rem; } @media (max-width: 600px) { main { flex-direction: column; } header { font-size: 1.2rem; } } </style> </head> <body> <header> <h1>Welcome to Responsive Design</h1> </header> <main> <article> <h2>About</h2> <p>This layout changes when the screen is small.</p> </article> <article> <h2>Image</h2> <img src="https://via.placeholder.com/300" alt="Placeholder image" /> </article> </main> </body> </html>
Always include the <meta name="viewport"> tag for responsive design to work on phones.
Use flexible units like percentages or rem instead of fixed pixels for widths and fonts.
Test your design by resizing the browser window or using device simulation in browser DevTools.
Responsive design makes websites adapt to different screen sizes.
Use CSS @media queries to change styles based on screen width.
Flexible layouts and images help keep content readable and usable everywhere.
Practice
Solution
Step 1: Understand the goal of responsive design
Responsive design aims to make websites adjust their layout and content to fit different screen sizes like phones, tablets, and desktops.Step 2: Compare options to the goal
Only To make websites look good on all screen sizes matches this goal by focusing on making websites look good on all screen sizes.Final Answer:
To make websites look good on all screen sizes -> Option AQuick Check:
Responsive design = adapt to screen sizes [OK]
- Confusing responsive design with animations
- Thinking responsive design is only for desktops
- Believing it only improves loading speed
Solution
Step 1: Identify correct media query syntax for max-width
The correct syntax to target screens smaller than 600px is using max-width: 600px inside @media.Step 2: Check each option
@media screen and (max-width: 600px) { ... } uses max-width: 600px correctly. @media screen and (min-width: 600px) { ... } uses min-width which targets larger screens. Options C and D use incorrect syntax.Final Answer:
@media screen and (max-width: 600px) { ... } -> Option DQuick Check:
max-width targets smaller screens [OK]
- Using min-width instead of max-width for small screens
- Incorrect media query syntax
- Missing 'screen' keyword
body { background-color: white; } @media (max-width: 600px) { body { background-color: lightblue; } }Solution
Step 1: Understand media query condition
The media query applies styles when the screen width is 600px or less.Step 2: Check the browser width and applied styles
At 500px width, the media query condition is true, so the background color changes to lightblue, overriding the default white.Final Answer:
The background color will be lightblue -> Option AQuick Check:
Width 500px < 600px triggers lightblue background [OK]
- Ignoring media query and thinking default style applies
- Confusing max-width with min-width
- Assuming no style change occurs
@media max-width: 800px { .container { width: 100%; } }Solution
Step 1: Check media query syntax
The correct syntax requires parentheses around the condition, like (max-width: 800px).Step 2: Identify the error in the snippet
The snippet misses parentheses, so it is invalid CSS and won't work as intended.Final Answer:
Missing parentheses around max-width condition -> Option BQuick Check:
Media queries need parentheses [OK]
- Omitting parentheses in @media queries
- Confusing max-width with width property
- Thinking selector causes error
Solution
Step 1: Understand image resizing in responsive design
Images should scale with their container to fit different screen sizes without distortion.Step 2: Evaluate CSS options for images
Setting width to 100% and height to auto keeps the image's aspect ratio and allows it to resize smoothly.Final Answer:
Set image width to 100% and height to auto -> Option CQuick Check:
Width 100% + height auto = responsive images [OK]
- Using fixed pixel sizes causing images to not resize
- Ignoring height causing distortion
- Thinking background images solve all responsive needs
