CSS Responsive Design - johnverz22/webdev1-lessons GitHub Wiki
Responsive design makes your website look good on all devices—phones, tablets, laptops, desktops.
- Avoid fixed widths.
- Use percentage-based widths.
.container {
width: 90%;
margin: auto;
}Use Flexbox or CSS Grid for dynamic layouts.
.parent {
display: flex;
flex-wrap: wrap;
}
.child {
flex: 1 1 300px;
}.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}-
%for widths -
emorremfor spacing and font sizes -
vwandvhfor full-screen elements - Avoid
pxfor layouts
body {
font-size: 1rem;
padding: 2vw;
}Change styles based on screen size.
@media (max-width: 768px) {
.menu {
flex-direction: column;
}
}-
max-width: 600px→ Phones -
max-width: 768px→ Tablets -
max-width: 1024px→ Small laptops -
min-width: 1025px→ Desktops
img {
width: 100%;
height: auto;
}<picture>
<source media="(max-width: 600px)" srcset="small.jpg">
<img src="large.jpg" alt="responsive image">
</picture>You're right.
That example uses px, not rem. Here's the corrected version using rem properly:
- Set the base font size using
rem - All other font sizes should scale from this base
html {
font-size: 1rem; /* This equals 16px by default */
}
body {
font-size: 1rem; /* 16px */
line-height: 1.5rem; /* 24px */
}
h1 {
font-size: 2rem; /* 32px */
}@media (max-width: 600px) {
html {
font-size: 0.875rem; /* Scales everything down to 14px */
}
}- Collapse into a hamburger on small screens.
- Use media queries to toggle display.
.nav {
display: flex;
}
@media (max-width: 600px) {
.nav {
display: none;
}
.menu-toggle {
display: block;
}
}Add this to your HTML <head>:
<meta name="viewport" content="width=device-width, initial-scale=1.0">- Design for small screens first.
- Add larger breakpoints later.
/* Mobile default */
.card {
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 768px) {
.card {
padding: 2rem;
}
}- Chrome DevTools device toolbar
- Responsively App
- Screen size simulator websites
Do you want a sample responsive page to try this out?