What are media queries?
Responsive design can be accomplished using media queries. How does this work? Think of media queries as a condition that you apply to your CSS. You tell the browser to add or remove certain CSS rules depending on the device or viewport size:
To apply those rules, we will need to use the CSS property @media
, as follows:
/* Mobile Styles */ @media only screen and (max-width: 400px) { body { background-color: #F09A9D; /* Red */ } }
@media only screen and (max-width: 400px)
means that if the screen/viewport size is fewer than or equal to 400px
, then we apply this CSS.
There are a few different types of properties you can add to media and target a different type of device.
For example, you can target especially the iPhone 4 with the following code:
@media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) and (orientation: portrait) { }
This translates as the following:
and (min-device-width...