What is the best way to merge different sets of CSS media queries?

In CSS Media queries, you have the option to use , (for "or") and and to meet media query criteria. For instance:

@media (min-width: 768px) and (min-resolution: 2dppx) { ... }

But what if you wish to combine and and , within the same media query? An example could be when min-resolution is not universally supported across browsers. In such cases, you might have to structure it like this:

@media
(-webkit-min-device-pixel-ratio: 2),
(min-device-pixel-ratio: 2),
(min-resolution: 192dpi),
(min-resolution: 2dppx) { ... }

For this scenario, any of those conditions must match for the media query to take effect.

If you wanted to add another requirement like min-width: 768px to the media query, how would you go about it? Is there a way to group the "or" queries together and link them with an and? Perhaps something along these lines:

@media
(
  (-webkit-min-device-pixel-ratio: 2),
  (min-device-pixel-ratio: 2),
  (min-resolution: 192dpi),
  (min-resolution: 2dppx)
)
and
(min-width: 768px) { ... }

The above syntax may not function as intended, but it demonstrates the concept. It's akin to how one can group SQL AND and OR queries using (...) parentheses.

Can this sort of grouping be achieved in CSS?

EDIT:

This approach appears to work, although it does seem somewhat cumbersome in terms of syntax:

@media
  (-webkit-min-device-pixel-ratio: 2) and (min-width: 768px),
  (min-device-pixel-ratio: 2) and (min-width: 768px),
  (min-resolution: 192dpi) and (min-width: 768px),
  (min-resolution: 2dppx) and (min-width: 768px)
  { ... }

Is this the only method available to achieve this outcome?

Answer №1

Here is a suggestion for using it:

@media (-webkit-min-device-pixel-ratio: 2), (min-device-pixel-ratio: 2), (min-resolution: 192dpi), (min-resolution: 2dppx) and (min-width: 768px) {...}

The comma "," serves as the delimiter in a list of individual media queries. Each media query between the commas is evaluated first, and then all of them are ORed together, giving the comma/OR the lowest precedence.

The "and" operator has the highest precedence.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Achieve a CSS design similar to the Cinemax logo with angled background ends

Is there a way to customize the angle at which the ends of a background are cut off when using -webkit-transform: rotate(-#deg); and background selectors for tilted text with a background? I'm looking to achieve a design similar to the Cinemax logo. ...

the battle between width and max-width for creating responsive web layouts

Many tutorials suggest using max-width:100% for images in responsive web design to ensure flexibility. However, why is it also possible to achieve the same result by using width:100%? ...

Ways to split images and text in list items using CSS

Can the text be formatted in a specific location on the page, separate from the image and heading? This is the HTML code I am currently using: <div class="tab-pane container fade" id="environmental"> <div class="row"> <div class="c ...

Disappearance of icon upon hovering in CSS

I am currently working with Angular JS (1.x) and I have a requirement to display tooltip text when hovering over an info icon. The tooltip text appears properly, but the issue is that the icon disappears when hovered over. It reappears once the hover is re ...

How to change the border color of a Bootstrap card when hovered over

I'm struggling to achieve a border color change effect on hover for Bootstrap cards. <b-card no-body class="my-card border"> <button>Click here</button> <b-collapse> <b-card-body> <b-c ...

HTML and CSS site lacking responsiveness

I am new to HTML and I have created a page that looks good on desktop, but it is not responsive on mobile devices. Can someone please help me with my HTML and CSS code? .link-menu { color: black; } .topbar-p ...

CSS Problem: The buttons beneath the div disappear when I apply a margin

Kindly click on the links below to view the images. The first image displays the desired design I am aiming for. The text and blue background are part of the image, and there is a button located below them. However, whenever I adjust the top margin, it ge ...

Other options besides re-flowing and repainting

After doing some research on various platforms like Stack Overflow, I've come across information stating that re-paints and re-flows can be quite taxing on a browser's resources. I am interested in learning about alternative CSS/JS techniques to ...

Display overlay objects specifically focused around the mouse cursor, regardless of its position on the screen

I am currently working on a project using SVG files and processing.js to develop a unique homepage that incorporates both animation and static elements. The concept is to maintain the overall structure of the original homepage but with varying colors, esse ...

What are the steps to incorporate an iframe containing uniquely designed XML content?

I'm currently working on a website project for a client who specifically wants to include news from the BBC. Despite spending 3 hours searching online, I haven't been able to successfully insert an XML file into an iframe and style it to the clie ...

The design of websites does not always translate well to mobile devices

While creating a website that is meant to be responsive across both desktop and mobile screens, I encountered an issue. The design looks perfect on desktop screens, but when viewed on a mobile screen, it shrinks and doesn't fit properly. I am using Bo ...

"Getting Started with Respond.js: A Step-by-Step

I've been struggling to find clear instructions on how to properly set up respond.js. Should I just unzip it into the htdocs folder, or do I only need respond.min.js in there? Then, do I simply reference the file like this... <script src="respon ...

How can I create a navigation bar with a search bar and a dropdown button using Material

Hello, excited to be posting my third question on SO after lurking for like 7 years. Currently, I am working with the Materialize framework and trying to design a search navigation bar with a vertical dropdown on the right side. This is how my code looks: ...

Align labels on top and inputs at the bottom using Bootstrap

I'm facing an issue with alignment in my form using Bootstrap 4.4. Whenever a select element is included, it disrupts the layout by breaking the alignment of labels and inputs on the same row. Is there a way to always keep the labels at the top and t ...

Questions about syntax: What is the correct course of action?

If there is a child inside a div with an id, such as id="mother", how should the css be written correctly? Example: 1) #mother ul li {...} or 2) .mother ul li {...} Is there a difference? The second example I came across when MOTHER had a class name, ...

Is there a way to verify if an ID includes more than one word?

I am trying to target a specific div with a unique id in jQuery: <div id="picture_contents_12356_title"></div> The '12356' is autogenerated and must be included in the id. I need to create a jQuery selector that combines "picture_co ...

Change the width of a div element without causing text to wrap when resizing the browser window

Looking to create a layout with three div elements, each containing an h1 element. The goal is to have the first div on the left side, the second div in the center, and the third div on the right side, all in a single row. When the window is resized for re ...

Troubleshooting: HTML drop-down menu experiencing malfunctions

As a newcomer to the realm of HTML, I am eager to kickstart my online portfolio. After successfully setting up the navigation bar, I have now turned my focus towards creating a dropdown menu within the nav bar. Unfortunately, I've hit a roadblock as t ...

Position an element to the right inside its parent container with varying width in Internet Explorer 7 or earlier

I am trying to align a button to the right edge of a table inside a container-div that has no set width. The goal is to have the button float on the right side of the table, lining up with its right edge. While my current approach works in most browsers, i ...

"Utilizing a background image within an email using a table data cell (

Struggling to add a background image quickly for a cell within a table in order to showcase dynamic text on top of it I attempted : <td style="background-image:url('bla... But it's not working as expected. What would be the most effective m ...