Understanding CSS and the implementation of media queries

Imagine having this scenario

/*
css for elements section #1 in page
css for elements section #2 in page
*/

@media screen and (max-width: 960px) {
/* redefines/alters CSS for elements in section #1 in page */
}

@media screen and (max-width: 700px) {
/* redefines/alters CSS for elements in section #2 in page */
}

I've encountered conflicting information regarding the priority of media queries. Some sources claim only one media query is matched at a time, while others suggest that cascading may occur depending on window size.

In my personal tests, I noticed that when resizing the browser window, @media screen and (max-width: 960px) seems to take precedence initially. Subsequently, as the window size decreases further, the styles from @media screen and (max-width: 700px) are applied without overriding the previous ones.

This raised the question for me - is this behavior purely coincidental and against standard specifications? Or am I overlooking something fundamental?

Answer №1

Your examinations have confirmed the correctness of how it is supposed to function.

Using your specific scenario, if the browser width falls below 960px and also under 700px, both conditions would be deemed accurate. This aligns with the intended functionality and is precisely how I have applied responsive design on multiple websites, with each condition representing a unique threshold for graceful degradation into a smaller screen size.

Answer №2

It is important to remember that with media queries, the source order can impact how styles are applied.

To learn more about this concept, visit Logic in Media Queries

In certain situations, the first query will always be executed if the width is less than or equal to 960px.

To prevent this CSS from being applied in your scenario, consider adding a min-width value to it.

Answer №3

It seems like there may be some confusion about the purpose of utilizing a media query. The initial 960 declaration indicates that specific styles should be applied if the screen resolution is less than 960px, and then transitions to applying those styles once it reaches 700px. This process aligns with the fundamentals of responsive design.

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 image is taking up the entire div instead of just filling the area below the header

I'm currently facing an issue with positioning an image within a container below a header. The problem lies in the fact that the image extends beyond the header's position, filling up the entire container. Below is the code snippet I'm work ...

The ineffectiveness of setting width to 0

When I set @media screen and (max-width: 700px), aside {width: 0}, it doesn't disappear. Even after resizing the window, it remains as null space with width: 52px. What could be causing this issue and how can I resolve it? * { margin:0; padding ...

What is causing col-xs-0 to occupy the entire width of the row?

Here is the structure of my Bootstrap container: <main > <div class="row co-lg-12 col-md-12 col-xs-12"> <div class="col-lg-1 col-md-1 col-xs-1" style="background-color:red; height: 100vh;"></div> <div class="col-lg-4 col-m ...

Is there a way to set the focus on a text box automatically when a page is loaded, even without support for JavaScript?

On my form, I have a few text fields and I am looking to automatically set the cursor (auto focus) on the first text field when the page loads. I aim to accomplish this without relying on javascript. ...

Tips for structuring a webpage section on a smaller screen using Bootstrap

Good day, I am inquiring about how to adjust the formatting of a website element when viewed on a different browser size. For instance, this is what my top navigation bar looks like at the LG size: Nav LG However, when I switch to SM size, it appears u ...

Rather than overwriting it, append a new value to the localstorage

I am trying to insert the value of newUsername into the localStorage setUsernamesArray. However, my current code overwrites the existing value instead of adding to it. Code $('#signUpButton').click(function() { var newUsername = $('#userna ...

CSS vertical text not functional as expected

Trying to make some text vertical, here is the CSS I'm using: h2.verticle{ color:#1a6455; border:0px solid red; writing-mode:tb-rl; filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); -webkit-transform:rotate(90deg); -moz-transform:rota ...

Choose links that do not start with a particular pattern

I've been playing around with CSS and I've learned how to select links that start with a certain pattern, like a[href^="tutorials_"] { text-color: red; } But now I'm curious if there's a way to select links that don't start with ...

The background image is trapping my text and preventing it from moving forward

As a beginner developer embarking on my first web page project, I've encountered a challenge. The text I inserted into a flexbox refuses to align beneath the image. .title { text-align: center; width: 500px; margin-left: auto; margin-right: a ...

Nested div elements maintain background-image continuity

Is it possible to maintain the background image of the body within a child div that has a red background color with opacity? * { box-sizing: border-box; } body { background-image: url('https://d6scj24zvfbbo.cloudfront.net/306f4bc782c04bbe4939f8c ...

Attempting to design a layout with flexible elements for the header, main content, and footer

https://i.sstatic.net/PURno.png I'm currently working on creating a layout that has specific boundaries for each section. The header and footer need to be centered vertically, while the content should extend to the bottom of the viewport. Please keep ...

Customize the Position of Icons in Material UI

I am looking for guidance on how to position my components within an image using Material UI. Specifically, I want to move my heart component to the top right corner of the image and place the ratings at the bottom. import image from "../../Assets/pic ...

Stylish CSS dropdown menu with borders and a subtle shift

I am struggling to create a dropdown menu as shown in this image: Solution for CSS dropdown menu. Unfortunately, my current code is not producing the desired result: Here is my incorrect solution This is the code I am using: .parent-menu li { ...

`When utilizing $routeParams, the CSS fails to load`

Whenever I use parameters in ngRoute and go directly to the URL (without clicking a link on the site), the CSS fails to load. All my routes are functioning properly except for /chef/:id. I utilized yeoman's angular generator, and I am running everythi ...

What could be causing these cards to not show up correctly?

I am currently incorporating Angular Material's cards in a material grid. Here is the code snippet that I am working with: http://codepen.io/anon/pen/YWwwvZ The issue at hand is that the top row of images extends off the screen at the top, and the b ...

Show a table containing dynamic information, featuring 10 rows for each column. The header should be present for each additional column that is added

I have incoming dynamic data that I need to display in columns, with 10 records in each row. Currently, I am able to display 10 rows as expected. However, I want the header to repeat each time an additional column is added. How can I achieve this using the ...

Tips for maintaining an open side menu

I'm having trouble keeping the side menu open when a user clicks on one of the child menu items. Ideally, I want section 1 to display section 2 content when hovered over, and keep both sections open even when section 2 is clicked. The only time they s ...

Safari 5.1.7 causing unusual behavior on the website

Despite running smoothly in most browsers, this website goes haywire and crashes on Safari when viewed on a Mac. I've attempted troubleshooting by removing the Google map, CSS transforms, and the Stellar jQuery plugin, but the issue persists. Does any ...

Create a table with CSS styling that resembles a measuring scale

I am working on a for loop that will populate a table, similar to the diagram below: https://i.sstatic.net/xbZ8u.png Age is displayed in the first row as a header, followed by three rows with three columns each. The first row contains images, the second ...

TS2339 Error: The property 'Default' is not a valid property for type 'string'

Hello, I am a beginner with Angular 2. I have encountered some issues that I need help with: Error Messages: app/components/playing-cables/-update.ts(82,12): error TS2339: Property 'options' does not exist on type 'jQuery'. app/compone ...