Article image Media Queries in CSS

Media Queries in CSS

Page 30 | Listen in audio

Media queries in CSS

Media queries are a CSS technique that allows you to define different styles for different devices and screen sizes. With media queries, you can create a responsive website that automatically adapts to different devices such as smartphones, tablets and desktops.

Media queries work by adding a condition that checks for screen size or device type and then applying different styles based on that condition. You can use media queries to change content width, text size, background color and much more.

There are several ways to write a CSS media query, but the most common is to use the keyword "media" followed by a condition. See an example:

@media(min-width: 768px) {
  /* Styles for screens larger than 768px */
}

In this example, the condition is "min-width: 768px", which means that styles within the media query will be applied to screens with a minimum width of 768 pixels. You can use other conditions, such as "max-width" for screens smaller than a certain size or "orientation" for devices in portrait or landscape orientation.

In addition, you can combine multiple conditions to create more complex media queries. For example:

@media (min-width: 768px) and (orientation: landscape) {
  /* Styles for screens larger than 768px in landscape orientation */
}

In this example, the condition is "min-width: 768px" and "orientation: landscape", which means that the styles within the media query will be applied to screens with a minimum width of 768 pixels and in landscape orientation.

Media queries are a powerful tool to create responsive websites that adapt to different devices. With them, you can ensure that your site is easy to use and navigate, no matter what device the user uses.

Now answer the exercise about the content:

_What is the purpose of media queries in CSS?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image css preprocessors

Next page of the Free Ebook:

31css preprocessors

2 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text