Card: responsive Design and Media Queries

Responsive Design

The Chameleon of Websites

Media queries are the secret spells of CSS, ensuring your website knows exactly when to transform. Like an invisible wizard behind the scenes, they monitor screen sizes and whisper to your layout: “Time to change shape!” With media queries, you can craft a website that dynamically adjusts depending on whether your user is on a tiny smartphone or a massive monitor.

This is where the true power of adaptation lies—a few clever rules, and your layout becomes a shapeshifting masterpiece. No more cumbersome adjustments! With a simple incantation, you’ll guide your website in the right direction—curse-free!

Websites with Adaptability

Whether a tiny display or a massive screen – your site will look great everywhere.

Media Queries

Casting Flexible Designs

Media Queries are your CSS spells, ensuring your website looks fantastic on any screen size. They work by applying CSS rules only under specific conditions—like when the screen becomes narrower.

@media (Condition) {
   /* here goes the magic */
}

The basic formula

@media (max-width: 768px) {
    .container {
        grid-template-columns: 1fr;
    }
}

This media query ensures that when the screen width is 768 pixels or less, the .container grid column reduces to a single column.

Precise Media Queries

Combining Multiple Conditions

You can combine multiple conditions using “and”. For instance, you can apply CSS rules only for screens larger than 600px and smaller or equal to 1200px.

@media (min-width: 600px) and (max-width: 1200px) {
    /* For screens between 600px and 1200px */
}

This media query targets screens between 600px and 1200px, optimizing content for medium devices like tablets or small laptops.

The Key to Flexibility

Media Queries and the Cascade

Media queries allow you to define different CSS rules for various screen sizes. The cascade plays a crucial role here: rules are applied in the order they appear in the stylesheet. More specific or later-defined rules overwrite previous ones, ensuring your layout stays flexible without redesigning everything from scratch.

Example Cascade:

  • The standard layout with three columns is applied first.
  • When the screen width is below 768px, the first media query reduces the layout to two columns.
  • For even smaller screens (less than 480px), the second media query adjusts the layout to a single column.
/* Standard layout with three columns */
.container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
}

/* smaller than 768px, 2 columns */
@media (max-width: 768px) {
    .container {
        grid-template-columns: 1fr 1fr;
    }
}

/* smaller than 480px, 1 column */
@media (max-width: 480px) {
    .container {
        grid-template-columns: 1fr;
    }
}

The New Magic of Media Queries

Simpler Comparisons

With new comparison operators in media queries, designing responsively becomes more precise and elegant. Instead of juggling min-width and max-width, you can use > and < to define exactly when your layout changes should occur.

This modern syntax makes responsive design clearer and more intuitive—no more guessing with min and max conditions. Now you can directly and explicitly dictate how your layout behaves based on screen size.

@media (width<480px) {
    .new {
        grid-template-columns: 1fr;
    }
}

@media (width>=480px) {
    .new {
        grid-template-columns: 1fr 1fr;
    }
}

@media (width>=768px) {
    .new {
        grid-template-columns: 1fr 1fr 1fr;
    }
}

The Magic of Bootstrap Media Queries

Guardians of the Breakpoints

Media queries are the vigilant keepers that know exactly when your website needs to transform to look flawless on any device. Bootstrap’s powerful breakpoints help you master this transformation effortlessly.

Bootstrap, a widely used, free CSS framework, provides developers with pre-designed layouts, components, and media queries that flexibly adapt to various screen sizes. It significantly simplifies the development of responsive websites for both mobile and desktop applications.

Bootstrap-Breakpoints:

Extra Small (< 576px)
For tiny displays that fit in any pocket. This is where responsive magic begins.

Small (>= 576px)
Compact screens with a little more breathing room but still perfectly mobile-friendly.

Medium (>= 768px)
Tablets and smaller laptops that offer more stage space for your designs to shine.

Large (>= 992px)
Desktops with ample space for anything you want to showcase.

Extra Large (>= 1200px)
Large screens that make your website feel like a sprawling kingdom.

Extra Extra Large (>= 1400px)
Displays so vast you might wonder why you’re even using media queries in the first place!

@media (max-width: 567px) {
    .max-min {
        background: green;
    }
}

@media (min-width: 567px) {
    .max-min {
        background: dodgerblue;
    }
}

@media (min-width: 768px) {
    .max-min {
        background: orange;
    }
}

@media (min-width: 992px) {
    .max-min {
        background: purple;
    }
}

@media (min-width: 1200px) {
    .max-min {
        background: limegreen;
    }
}

@media (min-width: 1400px) {
    .max-min {
        background: coral;
    }
}

@media all

The Universal Spell

Sometimes, you want your website to look great everywhere—truly everywhere. That’s where @media all comes in, the jack-of-all-trades among media queries. Whether it’s a screen, print, or even a screen reader, this command ensures your styles apply across all devices and media types.

@media all {
    /* Styles for all media forms */
}

@media screen

The Mirror Spell for Screens

With @media screen, you speak directly to all screens in the world. It’s the classic incantation to ensure your site shines on monitors of every kind.

@media screen and (max-width: 768px) {
    /* Styles for screens up to 768px wide */
}

@media print

The Art of Paper

For those rare moments when the digital world isn’t enough, and someone wants to immortalize your website on paper, @media print steps in. It ensures your site looks as elegant in print as it does online.

@media print {
    /* Styles for printed pages */
}

@media speech

The Voice of the Internet

If you want your website not just to be read but also heard, @media speech is the right spell. It ensures screen readers—devices that vocalize web content—present your site clearly and effectively.

@media speech {
    /* Styles for speech-based devices */
}

prefers-color-scheme

The Sorcery of Dark Mode

Imagine tailoring your website’s color theme to match the user’s preference for shadowy darkness or radiant light—that’s the magic of prefers-color-scheme!

With a simple spell, your site can detect whether someone has summoned Dark Mode or prefers to bask in the glow of light mode. Your website seamlessly adapts to the user’s device settings, creating an enchanting experience for both bright and shadowy moods.

@media (prefers-color-scheme: dark) {
    body {
        background-color: #121212;
        color: #ffffff;
    }
}

When your visitor chooses the shadowy path, your site transforms into an elegant, dark masterpiece.

@media (prefers-color-scheme: light) {
    body {
        background-color: #ffffff;
        color: #000000;
    }
}

But for those who seek the light, your site shines in brilliant brightness.

CSS
@media (max-width: 567px) {
    .max-min {
        background: green;
    }
}

@media (min-width: 567px) {
    .max-min {
        background: dodgerblue;
    }
}

@media (min-width: 768px) {
    .max-min {
        background: orange;
    }
}

@media (min-width: 992px) {
    .max-min {
        background: purple;
    }
}

@media (min-width: 1200px) {
    .max-min {
        background: limegreen;
    }
}

@media (min-width: 1400px) {
    .max-min {
        background: coral;
    }
}
Scroll to Top