CSS Responsive Design

Responsive Layout

A Responsive Layout is a web page layout that automatically adjusts to different screen sizes and devices.

Instead of creating separate versions of a website for mobile, tablet, and desktop users, responsive layouts adapt dynamically using CSS techniques.

Responsive layouts help ensure that content remains:

  • Readable
  • Accessible
  • User-friendly
  • Consistent across devices

Modern responsive layouts are commonly built using:

  • Flexible widths
  • CSS Flexbox
  • CSS Grid
  • Responsive images
  • Media Queries

Syntax

A responsive layout typically combines flexible units with media queries.

Responsive Layout Syntax

css

.container {
    width: 100%;
    max-width: 1200px;
}

Allows the layout to shrink and grow based on screen size.

Attributes

TechniqueDescriptionExample
width: 100%Makes elements flexiblewidth: 100%;
max-widthPrevents excessive stretchingmax-width: 1200px;
FlexboxCreates flexible layoutsdisplay: flex;
GridCreates responsive gridsdisplay: grid;
Media QueriesApplies styles for specific screen sizes@media (...)

Example

Responsive Layout Complete Example

html

<!DOCTYPE html>
<html>
<head>
    <title>Responsive Layout Example</title>
    <style>
        .container {
            display: flex;
            gap: 20px;
            max-width: 1200px;
            margin: auto;
        }
        .box {
            flex: 1;
            background-color: lightblue;
            padding: 20px;
        }
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">Section 1</div>
        <div class="box">Section 2</div>
        <div class="box">Section 3</div>
    </div>
</body>
</html>

Output

Browser Output

css

Desktop View
Section 1 | Section 2 | Section 3

Mobile View
Section 1
Section 2
Section 3

Desktop displays sections side by side
Mobile stacks sections vertically
The layout automatically adjusts to screen size

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE
YesYesYesYesYesPartial

Notes

Responsive Images

Responsive Images

css

img {
    max-width: 100%;
    height: auto;
}

Prevents images from overflowing their container.

Responsive Grid Example

Responsive Grid Example

css

.container {
    display: grid;
    grid-template-columns:
        repeat(auto-fit, minmax(250px, 1fr));
}

Automatically adjusts column count based on screen size.

Responsive Flexbox Example

Responsive Flexbox Example

css

.container {
    display: flex;
    flex-wrap: wrap;
}

Allows items to move onto new rows when space is limited.

Best Practices

PracticeBenefit
Use relative unitsBetter scalability
Use Flexbox/GridEasier responsive layouts
Use media queriesDevice-specific styling
Use responsive imagesPrevent overflow
Test on multiple devicesBetter user experience

Conclusion

Responsive Layouts ensure that websites work effectively across different devices and screen sizes. By combining flexible units, responsive images, Flexbox, Grid, and Media Queries, developers can create layouts that provide a consistent and user-friendly experience for all users.