What is the best way to handle media queries when determining the validity of both statements?

I'm trying to figure out how to apply CSS based on whether one of two statements is true. For instance:

@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape)
OR
and (max-device-width : 1024px) 
and (orientation : portrait){

}

I am developing a multi-platform website and have already completed the mobile version. However, I want iPads to display the laptop/desktop version. Everything was going well with this approach:

@media only screen and (min-device-width: 0px){Mobile Version CSS}

@media only screen and (min-device-width: 500px){Other Version CSS}

But then I realized that when I changed a mobile device to landscape mode, it would switch back to the desktop mode because the width in the media query was less than the screen's landscape width. What would be the best combination of media queries that allow me to target mobile devices and other platforms separately, while taking into account whether a phone is in landscape or portrait mode? I don't want to repeat my CSS code across multiple queries due to changes in orientation. I simply want the mobile version to remain active regardless of whether the mobile device is in landscape or portrait mode.

Thank you!

Answer №1

Why not try a quick search on Google to find the answer: (give it a shot) https://css-tricks.com/logic-in-media-queries/

Understanding media query logic:

  • And: and
  • Or: ,
  • Not: not


For your specific case, your CSS should be as follows:

@media
  only screen and (min-device-width: 768px) and (max-device-width: 1024px) and (orientation: landscape),
  only screen and (max-device-width: 1024px) and (orientation: portrait) {
    /* your css rules here */
}


A few things to consider:

  1. Your media queries should also cater to scenarios like "portrait and device-width < 768px" and "device-width > 1024px". Make sure to address these cases too.
  2. An iPad in landscape mode may have a width of 1024px, but presenting a desktop view to all devices with a minimum width of 768px might not be ideal. Consider the visual aspect of your website based on different device sizes.
  3. Have you thought about using a grid system such as Bootstrap? It could simplify your layout process.

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

The anchor tag seems to be malfunctioning within the div container

<style type="text/css> .xyz { position: absolute; top: 120px; left: 160px; z-index: -1; } #container { position: relative; overflow: visible; opacity: .3; } </style> <body> <div> <video i ...

Is there a way to incorporate an 'ended' feature into the .animate() function?

I'm currently working on a jQuery statement where I want to activate pointer events once the button animation is finished. However, I'm unsure how to incorporate an ended or complete function. Any suggestions on how to approach this? $('#ce ...

Clicking on the Primary Division

Experimenting with the onclick event on a parent div led me to realize that making the entire div clickable at 100% width is not what I intended. I only wanted the sub div to trigger a specific function. Here's an example: <div id="loginContain ...

The application of CSS transition fails in the context where top property is set as auto

I have been exploring an online tutorial that almost met my requirements. However, I encountered a challenge with the CSS 'transitions' effects. Basically, I need the text to be positioned at a specific distance from the top because the title wi ...

Using jQuery and Bootstrap to Enhance Checkbox Button Styling

I am currently working on an asp.net webform and attempting to style my checkbox similar to the design shown here: When I try to replicate the HTML and JS code from the above link on my page, it does not seem to be functioning properly, most likely due to ...

Tips for creating a layout featuring images and text placed next to each other in harmony

Is it possible to replicate this section layout using Bootstrap? Take a look at Image 1. ...

Struggling to get the onHover effect working with the Grid item component in MaterialUI

I'm currently working on a Grid layout with nested Grid components, but I've run into an issue where applying inline styles to the Grid item component isn't working as expected. As a newcomer to MaterialUI in ReactJS, I could really use some ...

Attempting to execute code on a database using PHP and MySQL

Hey there! I am just diving into the world of HTML and PHP, attempting to create a page that can execute an SQL query to transfer data from one table to another. However, I'm facing some difficulties in making it work as I need a user-provided date t ...

Ways to dynamically assign a class to a single element within a group of identical elements

I have three identical items in the DOM. Specifically, I am referring to a moving line <span class="caret"></span> <ul> <li class="nav-item-1"> <a href="#">ITEM 1</a> <span class="caret"></span> ...

Angular's ng-model is unable to access the value of an object array

When selecting the days, users should be able to input check-in and check-out time ranges dynamically. However, there seems to be an issue with retrieving the values and data format. The ng model is unable to capture the check-in and check-out values. The ...

Leverage the power of jQuery to manipulate video tags

Here's the challenge: I need to utilize a jQuery function to extract a URL from a link's REL attribute and then transfer it to a video element. The extraction process and transmission seem to be working smoothly. However, my struggle lies in act ...

Expanding the content to fit the text alignment

I have implemented text justification on a block of content. Here is a link to view the text: Click here Upon closer inspection, you may notice that within the text block, some words are stretched out to fill the space but this causes an abnormal spacing ...

The fullCalendar plugin fails to display properly when placed within a tab created using Bootstrap

My current challenge involves integrating fullCalendar into a Bootstrap tab. It works perfectly when placed in the active tab (usually the first tab), however, when I move it to another tab that is not active, the calendar renders incorrectly upon page loa ...

Modifying the appearance of a WordPress website with CSS styling

I am currently using WordPress and I am excited about making changes to the CSS style sheet. After visiting , I realized that there is no option for editing the 'CSS' under Appearance → Customize → 'CSS'. My usual process is going ...

The options in the Dropdown choices-js remain hidden beneath a div within a bootstrap table-responsive element

When utilizing the choices-js drop-down menu within a Bootstrap 5 responsive table, I am encountering display issues. Specifically, when ID 1 is selected, it appears incorrectly while ID 3 displays correctly. It is crucial that overflow-x incorporates a s ...

A visible white line appears within the image when the Windows scaling is set to 125%

I encountered a peculiar issue. A certain website displays an image with a 5px solid border, but when I adjust the Windows scale settings to 125%, there is a noticeable gap between the image and the border. https://i.sstatic.net/FWLIN.png https://i.sstat ...

Is it possible to establish a connection between an HTML5 web socket and a Java Socket?

Recently, I created a system where a Java program was running on a server and a Java applet was embedded in a client's browser page. These two components were communicating via Java sockets. However, I am now contemplating the idea of transitioning fr ...

String Replacement in JavaScript

Here's the scenario: I have a string var str="abc test test abc"; Is there a way to specifically replace the second occurrence of "abc" in the string using the str.replace() method? ...

Using jQuery and CSS to create a temporary placeholder for images that have varying sizes

I am currently developing an innovative image gallery feature where a temporary heart image is displayed for 1 second in place of the actual images (image 1, image 2 etc). If you want to view the implementation, check out my jsfiddle below: https://jsfid ...

When the mouse reaches the far left portion of the screen, an action will be triggered

Is there a way to trigger an action when the mouse reaches the far left or right of the screen using HTML, CSS, jQuery, or any other method? I'm specifically trying to show my navbar when the mouse reaches the left end of the screen. Any assistance wo ...