Media Query example for the Web Page in CSS

HTML and CSS for the Web Page

HTML (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Web Page</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <h1>Responsive Web Page</h1>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </header>
    <main>
        <section class="intro">
            <h2>Welcome to Our Website</h2>
            <p>This is a sample responsive design created using media queries.</p>
        </section>
        <section class="content">
            <div class="box">Content Box 1</div>
            <div class="box">Content Box 2</div>
            <div class="box">Content Box 3</div>
        </section>
    </main>
    <footer>
        <p>&copy; 2025 Responsive Design Tutorial. All rights reserved.</p>
    </footer>
</body>
</html>

CSS (styles.css)

/* Basic Styles */
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

header {
    background-color: #007bff;
    color: white;
    padding: 1em;
    text-align: center;
}

nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    justify-content: center;
    gap: 1em;
}

nav ul li a {
    text-decoration: none;
    color: white;
    font-weight: bold;
}

main {
    padding: 2em;
}

.intro {
    text-align: center;
    margin-bottom: 2em;
}

.content {
    display: flex;
    gap: 1em;
    justify-content: space-between;
}

.box {
    flex: 1;
    padding: 1em;
    background-color: #f0f0f0;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 5px;
}

/* Responsive Design */

/* For devices with a width of 768px or less (Tablets and smaller devices) */
@media screen and (max-width: 768px) {
    nav ul {
        flex-direction: column;
        align-items: center;
    }
    .content {
        flex-direction: column;
    }
    .box {
        margin-bottom: 1em;
    }
}

/* For devices with a width of 480px or less (Mobile devices) */
@media screen and (max-width: 480px) {
    header {
        padding: 0.5em;
    }
    header h1 {
        font-size: 1.5em;
    }
    nav ul {
        gap: 0.5em;
    }
    .content {
        gap: 0.5em;
    }
    .box {
        padding: 0.5em;
    }
}

Explanation of the Code

1. HTML Structure

  • The header contains a site title and navigation menu.

  • The main has two sections:

    • Intro Section: A welcoming message.

    • Content Section: Three content boxes displayed side by side (on larger screens).

  • The footer includes a copyright notice.

2. CSS Styling

  • Default Styles: The default layout is designed for larger screens with:

    • A horizontal navigation bar.

    • Three content boxes displayed in a row using flexbox.

3. Media Queries

  • For Tablets (max-width: 768px):

    • The navigation menu changes to a vertical layout.

    • The content boxes stack vertically for better readability.

  • For Mobile Devices (max-width: 480px):

    • The header padding and font size are reduced.

    • The navigation and content gaps are adjusted to fit smaller screens.

    • The content boxes are further compacted with reduced padding.


How It Works

  1. Desktop: The layout is spacious, with a horizontal menu and content boxes in a row.

  2. Tablet: The layout adapts by stacking elements vertically for easier viewing on smaller screens.

  3. Mobile: The design becomes more compact, optimizing space and ensuring readability.

This approach ensures a responsive and user-friendly design suitable for various devices.