Responsive Design Media Queries
Responsive Design: Websites für alle Geräte
Über 60% des Web-Traffics kommt von Mobilgeräten. Responsive Design ist keine Option mehr, sondern Pflicht. Hier lernen Sie die Grundlagen.
Der Viewport Meta-Tag
<!-- PFLICHT für Responsive Design --> <meta name="viewport" content="width=device-width, initial-scale=1">
Mobile-First Ansatz
Beginnen Sie mit dem Mobile-Layout und erweitern Sie für größere Screens:
/* Basis-Styles (Mobile) */
.container {
padding: 1rem;
}
.card {
width: 100%;
}
/* Tablet */
@media (min-width: 768px) {
.container {
padding: 2rem;
}
.card {
width: 50%;
}
}
/* Desktop */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
margin: 0 auto;
}
.card {
width: 33.333%;
}
}
Gängige Breakpoints
| Gerät | Breite | Media Query |
|---|---|---|
| Mobile S | 320px | Basis (kein Query) |
| Mobile L | 425px | @media (min-width: 425px) |
| Tablet | 768px | @media (min-width: 768px) |
| Laptop | 1024px | @media (min-width: 1024px) |
| Desktop | 1440px | @media (min-width: 1440px) |
Media Query Syntax
/* Min-Width (Mobile-First) */
@media (min-width: 768px) { ... }
/* Max-Width (Desktop-First) */
@media (max-width: 767px) { ... }
/* Bereich */
@media (min-width: 768px) and (max-width: 1023px) { ... }
/* Orientation */
@media (orientation: landscape) { ... }
@media (orientation: portrait) { ... }
/* Hover-Fähigkeit (Touch vs. Maus) */
@media (hover: hover) { ... }
@media (hover: none) { ... }
/* Prefers-Reduced-Motion */
@media (prefers-reduced-motion: reduce) {
* { animation: none !important; }
}
/* Dark Mode */
@media (prefers-color-scheme: dark) {
body { background: #1a1a1a; color: #fff; }
}
Flexible Bilder
/* Bilder skalieren mit Container */
img {
max-width: 100%;
height: auto;
}
/* Oder mit object-fit */
.image-container {
width: 100%;
aspect-ratio: 16 / 9;
}
.image-container img {
width: 100%;
height: 100%;
object-fit: cover;
}
Responsive Images mit srcset
<img
src="image-800.jpg"
srcset="image-400.jpg 400w,
image-800.jpg 800w,
image-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw,
(max-width: 1000px) 50vw,
33vw"
alt="Beschreibung"
>
Flexible Typografie
/* Fluid Typography mit clamp() */
h1 {
font-size: clamp(1.5rem, 4vw, 3rem);
/* Min: 1.5rem, Preferred: 4vw, Max: 3rem */
}
p {
font-size: clamp(1rem, 2.5vw, 1.25rem);
}
/* Container Query für Text */
.card {
container-type: inline-size;
}
@container (min-width: 400px) {
.card h2 {
font-size: 1.5rem;
}
}
Responsive Layouts
Mit Flexbox
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 300px; /* Min 300px, wächst gleichmäßig */
}
Mit Grid
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1rem;
}
Responsive Navigation
/* Mobile: Hamburger Menu */
.nav {
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-links {
display: none;
position: absolute;
top: 60px;
left: 0;
right: 0;
flex-direction: column;
background: white;
}
.nav-links.active {
display: flex;
}
.hamburger {
display: block;
}
/* Desktop: Normale Navigation */
@media (min-width: 768px) {
.nav-links {
display: flex;
position: static;
flex-direction: row;
}
.hamburger {
display: none;
}
}
Container Queries (modern)
/* Container definieren */
.card-container {
container-type: inline-size;
container-name: card;
}
/* Basierend auf Container-Größe stylen */
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 150px 1fr;
}
}
@container card (min-width: 600px) {
.card {
grid-template-columns: 200px 1fr;
}
}
CSS-Einheiten für Responsive
| Einheit | Beschreibung | Verwendung |
|---|---|---|
rem |
Relativ zu Root-Font-Size | Font-Sizes, Spacing |
em |
Relativ zu Parent-Font-Size | Komponenteninterne Abstände |
vw/vh |
Viewport-Breite/Höhe | Hero-Sections, Fluid Type |
% |
Prozent vom Parent | Widths, Margins |
dvh |
Dynamic Viewport Height | Mobile (inkl. Browser-UI) |
Testing
- Chrome DevTools: Device Toolbar (Ctrl+Shift+M)
- Firefox: Responsive Design Mode (Ctrl+Shift+M)
- Echte Geräte: Immer auch auf echten Phones testen
- BrowserStack: Viele Geräte simulieren
💡 Tipp:
Verwenden Sie Content-basierte Breakpoints statt gerätespezifischer. Ändern Sie das Layout, wenn der Inhalt es erfordert, nicht bei festen Pixelwerten.