Responsive design ensures that web pages look good on all devices by using fluid grids, flexible images, and media queries. It adapts the layout and content based on the screen size and orientation.
The viewport meta tag is essential for responsive design. It controls the layout on mobile browsers:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Media queries allow you to apply different styles based on device characteristics such as screen width. Here's an example of media queries:
@media (max-width: 600px) {
body {
background-color: #f4f4f4;
}
header {
font-size: 24px;
}
}
@media (min-width: 601px) {
body {
background-color: #fff;
}
header {
font-size: 32px;
}
}
Fluid grids use percentage-based widths to create layouts that adjust to different screen sizes:
body {
display: flex;
flex-wrap: wrap;
}
header, footer {
width: 100%;
}
main {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
To make images responsive, use CSS to ensure they adapt to different screen sizes:
img {
max-width: 100%;
height: auto;
}
The mobile-first approach involves designing for smaller screens first and then using media queries to adjust for larger screens. This approach prioritizes mobile users and ensures a good experience on all devices.
@media (min-width: 768px) {
.container {
max-width: 750px;
}
}
@media (min-width: 992px) {
.container {
max-width: 970px;
}
}
@media (min-width: 1200px) {
.container {
max-width: 1170px;
}
}