D DevBrainBox

CSS Media Queries

CSS

What are CSS Media Queries?

Media Queries let you apply different styles depending on the device’s:

  • Width / height
  • Screen size
  • Orientation
  • Resolution This is how you make a website mobile-friendly or responsive.
@media media-type and (condition) {
  /* CSS rules go here */
}

@media screen and (max-width: 600px) {
  body {
    background-color: lightblue;
  }
}

This will apply a blue background only when the screen width is 600px or smaller (mobile/tablet).

FeatureExampleMeaning
max-width(max-width: 768px)Up to 768px (phones/tablets)
min-width(min-width: 1024px)From 1024px and up (desktops)
orientation(orientation: landscape)Wide view (sideways)
resolution(min-resolution: 2dppx)High-density displays (retina)

Media Types

TypeDescription
allDefault, applies to all
screenComputer, phone, tablets
printPrint preview & paper
speechScreen readers

Multiple Conditions

@media screen and (min-width: 600px) and (max-width: 1024px) {
  /* Tablet styles */
}

Example

/* Default style for large screens */
.container {
  width: 1000px;
}

/* For tablets */
@media (max-width: 768px) {
  .container {
    width: 90%;
  }
}

/* For phones */
@media (max-width: 480px) {
  .container {
    width: 100%;
    font-size: 14px;
  }
}
ConceptUse Case
@mediaDefine conditional CSS
max-widthApply styles below a width
min-widthApply styles above a width
Responsive UIAdapts to phone, tablet, desktop screens

On this page