The one code snippet you need to understand css media queries

Media queries can be confusing. But they really are simple. If you only need to target 3 device types (mobile, tablet, desktop), the below css is all you need!

/* Base Class - Phones will always have a red button  */
/* Less than 767px */
/* .button will have red background and black text */
.button {
    background-color: red;
    color: white;
}

/* Laptop & Tablet */
/* CSS is used for screens 767px - 1280px */
/* .button will have white background and black text */
@media (min-width: 767px ) {
    .button {
        background-color: orange;
        color: black;
    }
}

/* Desktop */
/* CSS is used for screens 1280px and up */
/* .button will have blue background and black text */
@media (min-width: 1280px) {
    .button {
      background-color: lightblue;
    }
}

The Full Stack Dev Newsletter

The Full Stack Dev Avatar

Hey, I'm Wes. Every month I send a newsletter with articles on:

  • ​🔥 Hot Web Dev tips
  • 🎨 Web Design
  • 🔨 Product Development

As seen at the top of Hacker News, Indie Hackers, and Hacker Noon.