Home >Web Front-end >CSS Tutorial >How Can I Use `@media` Queries with `min-width` and `max-width` for Consistent Cross-Browser Styling?

How Can I Use `@media` Queries with `min-width` and `max-width` for Consistent Cross-Browser Styling?

Linda Hamilton
Linda HamiltonOriginal
2024-11-30 18:18:15433browse

How Can I Use `@media` Queries with `min-width` and `max-width` for Consistent Cross-Browser Styling?

Using @Media with Min-Width and Max-Width

In the context of CSS, @media queries allow you to target specific screen sizes or devices with different styles. However, setting up media queries to work consistently across various devices and browsers can be challenging.

In your case, you've defined three @media rules:

@media screen and (min-width: 769px) {...}
@media screen and (min-device-width: 481px) and (max-device-width: 768px) {...}
@media only screen and (max-device-width: 480px) {...}

While this setup functions on an iPhone, it's not clear why it doesn't work in a browser. One potential issue is that you're using "device" in the meta tag and "max-width" instead of "max-device-width" in the third rule.

However, the recommended approach is to define default CSS styles for older browsers that do not support @media queries. Then, use @media rules to target specific screen sizes or devices with additional styles. For example:

/* Default styles for older browsers */
body {
  font-size: 14px;
}

/* Styles for screens wider than 960px */
@media only screen and (min-width: 960px) {
  body {
    font-size: 16px;
  }
}

/* Styles for mobile browsers narrower than 480px */
@media only screen and (max-device-width: 480px) {
  body {
    font-size: 12px;
  }
}

By following this approach, you can ensure compatibility with a wider range of devices and browsers.

The above is the detailed content of How Can I Use `@media` Queries with `min-width` and `max-width` for Consistent Cross-Browser Styling?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn