Free Ebook cover Complete CSS course

Complete CSS course

4.5

(2)

35 pages

Media Queries in CSS

Capítulo 30

Estimated reading time: 2 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

@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.

Media queries in CSS are used to set different styles for different devices and screen sizes. They allow a website to respond to the conditions of the device it is being viewed on, such as its screen size, ensuring a responsive and user-friendly experience across smartphones, tablets, and desktops.

Next chapter

css preprocessors

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.