Media Query (CSS)

·

3 min read

Table of contents

No heading

No headings in the article.

A crucial component of responsive web design are media queries. On the basis of the user's screen size, resolution, and other device parameters, you may modify the layout and style of your website using them. To get you started, we'll go through media queries in this post and offer some sample code.

What are Media Queries?

A CSS technique called media queries enables you to layout your website differently depending on the user's device. You may specify alternative styles using media queries for various screen sizes, resolutions, and even device orientations. This enables you to provide a more personalized and user-friendly experience for visitors to your website.

Media Queries use

You must specify the circumstances under which media queries will be used if you want to utilize them. This is done by first applying a series of CSS rules that will be implemented if the required criteria are satisfied, followed by the @media rule. Here's an illustration:

cssCopy code@media screen and (max-width: 768px) {
  /* CSS rules for screens with a maximum width of 768 pixels */
}

In this example, we're using the @media rule to apply a set of CSS rules to screens with a maximum width of 768 pixels. This is commonly used to adapt the layout of a website for smaller screens, such as those on mobile devices.

You can also use multiple conditions to create more complex media queries. Here's an example:

cssCopy code@media screen and (max-width: 768px) and (orientation: portrait) {
  /* CSS rules for screens with a maximum width of 768 pixels and a portrait orientation */
}

In this example, we're using two conditions to apply a set of CSS rules to screens with a maximum width of 768 pixels and a portrait orientation.

Media Query Examples

Let's take a look at some practical examples of media queries in action.

cssCopy code/* Styles for desktop screens */
.some-element {
  font-size: 24px;
}

/* Styles for mobile screens */
@media screen and (max-width: 480px) {
  .some-element {
    font-size: 16px;
  }
}

In this example, we're using a media query to apply a smaller font size to a specific element when viewed on a mobile device.

cssCopy code/* Styles for screens with a width greater than 768px */
.some-element {
  width: 50%;
}

/* Styles for screens with a width less than or equal to 768px */
@media screen and (max-width: 768px) {
  .some-element {
    width: 100%;
  }
}

In this example, we're using a media query to adapt the width of a specific element based on the user's screen size. This allows the element to take up the full width of the screen on smaller devices, while still maintaining a fixed width on larger screens.

Conclusion

Media queries are a crucial component of responsive web design since they let you provide your website users a more individualized and user-friendly experience. Your website's layout and appearance can be modified to meet the user's device specifications by specifying the circumstances under which various styles will be used. You should now have a strong basis to start using media queries and advance your website thanks to the code samples included in this post.