CSS media query for minimum and maximum width seems to be ineffective

Check out this Codepen example: https://codepen.io/codepenuserpro/pen/ExQrEbo

HTML:

<div></div>

CSS:

div
{
  height:400px;
  width:400px;
  background-color:red;
}

@media only screen and (min-width: 1068px) and (max-width: 1380px)
{
  background-color:blue;
}

I've noticed that the div is not changing its background color when I resize the browser window to between 1068 - 1380px. Any ideas on why this might be happening?

Answer №1

Understanding Media Query Syntax

A media query is made up of a media type and can include multiple expressions that evaluate to either true or false.

If the evaluation is true, then the corresponding CSS code will be implemented.

@media not|only mediatype and (expressions) {
  <stylesheet>
}

It's crucial to specify the element - in this case div - within the media query as shown below:

@media only screen and (min-width: 1068px) and (max-width: 1380px) {
  div {
    background-color: blue;
  }
}

div {
  height: 400px;
  width: 400px;
  background-color: red;
}

@media only screen and (min-width: 1068px) and (max-width: 1380px) {
  div {
    background-color: blue;
  }
}
<div></div>

Answer №2

To target a specific element inside a media query, you can use the following code snippet:

@media only screen and (min-width: 1068px) and (max-width: 1380px){
  div{
    background-color:blue;
  }
}

Answer №3

In the alternative method, you failed to choose the div element.

Consider adding the following code snippet:

@media only screen and (min-width: 1068px) and (max-width: 1380px) {
    div {
        background-color: blue;
    }
}

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

Some divs are not inheriting class properties as expected

I am currently working on a footer section that consists of 4 div elements, each with a shared class. Oddly enough, the first div is extending its width more than expected. The other 3 div elements are adjusting their width based on the content within them ...

Achieving unique proportions with HTML5 video resizing

On my page, I plan to showcase a video specifically for desktop browsers. I have devised some code to determine whether the video is in landscape or portrait orientation. The challenge lies in deciding what to do based on this orientation: If the video is ...

Just obtained a new Bootstrap Template and currently in the process of personalizing it by replacing the default image with my

I'm facing an issue with the image in the masthead of a Bootstrap Template. I tried to change the image URL to my desired picture in the CSS file of the downloaded Bootstrap files, but even after saving it, the image on the page remains the same old o ...

Create a new div element in your HTML structure that is not currently defined in your Sass/CSS

Currently, I am developing a React Component that includes a DatePicker feature using PrimeReact's Calendar component. I have a specific requirement to display an arrow on top of the datepicker when it pops up. You can view the desired layout in the ...

Selectors in PHP DOMDocument that mimic jQuery's functionality

Currently, I am working with a DOMDocument and I am curious if there is a way to utilize CSS-like selectors to choose nodes similar to how it is done in jQuery. For instance, let's say I am analyzing an XML file and a portion of it appears as follows ...

What is the best way to eliminate an unassigned margin from my layout?

The content of my css file is as follows: .menu { background-color: black; color: white; height: 100px; opacity: 0.5; } html { background-image: url('image.jpg'); background-size:cover; } Despite the lack of text ...

Setting up global color variables for React with Material UI theming is essential for ensuring consistent and

Is there a way to customize the default material-ui theme using global CSS? Additionally, how can we incorporate HEX values for colors when setting primary and secondary colors in the theme? In my App.js root file, I have defined a custom theme and want t ...

Arrange elements prior to the treeview within the unordered list/ list items

Below is a snippet of code that I am working with: <ul> <li>group 1 <ul> <li>group 1.1</li> <li> group 1.2</li> <li>group 1.3 <ul> <li>group 1.3.1</li> <l ...

"Embedding Bootstrap, jQuery, and Twitter Bootstrap directly onto

I'm currently working on customizing some source code to fit my needs. To do this, I need to download and use Bootstrap, jQuery, and Bootstrap locally. I have already downloaded all of them to a directory named "libs". However, when I try to run the P ...

Craft a CSS triangle shape from scratch

Looking at the image of a blue triangle, I am curious about how to recreate it using CSS. Would using a background image be the best approach, or is there a way to achieve this with CSS transforms? I noticed the triangle is on the left side but not on the ...

Modify the icon in the header of MaterializeCSS Collapsible when it is opened

I'm struggling to figure out how to change the icon of a toggled collapsible element. I have been reviewing their documentation but am having trouble making it work as intended. $('.collaps_roles_permission').collapsible({ accordion: tr ...

The issue with jQuery click function seems to be limited to Firefox

After a long hiatus from posting here, I hope this isn't breaking any rules. For those who prefer visual examples, you can visit the live page at: The drop-down menu functions perfectly in IE, Chrome, and Safari, but seems to be causing issues in Fir ...

What is the best way to make a scrollable div with top and bottom padding without adding extra elements inside?

I have created a fiddle to demonstrate my question. Essentially, I am looking to create a scrollable container with padding at the top and bottom that remains visible during scrolling, ensuring there is always a consistent distance from the edge to the con ...

Ensuring Consistent Height for a Responsive HTML Document Across Devices

My html page is facing a problem when printing from different Android devices. The page height changes dynamically based on label updates through JavaScript, and I pass this height to adjust the printed page (approximately 856x3000 pixels). However, issu ...

Maintain the menu item color even after clicking

Here is the menu code snippet: <div class="header"> <div id="logo"></div> <ul class="menu"> <li><a href="/index.aspx">Home</a></li> <li><a href="/about.aspx"& ...

Adjust the navigation bar for smaller screens

I am diving into my first project using Bootstrap and am currently in the process of setting up the navbar. My goal: When the XS model is activated, I want to adjust the font size of the header, modify the height of the navbar, and left-align the header. ...

Creating a table with multiple rows condensed into a single row (Using Material-UI/CSS)

Here is the array structure I am working with: const sample = [ { name: 'apple', detail: [{'a', 'b'}]}, { name: 'banana', detail: [{'a', 'b'}]}, ]; In order to create a table similar to the ...

Anomaly in Form Validation with Semantic-UI

This time around, I am implementing Semantic-UI with React. In the past, I have successfully utilized Semantic-UI's form and form validation features in various projects without encountering any issues. However, a new problem has arisen, which I will ...

Gather data from various HTML elements with JavaScript when submitting a form

How can I extract values from HTML elements and send them for processing through a form? I'm attempting to compile a menu item list with the individual items' structure in place, but I'm struggling to figure out how to fetch the values upon ...

When a container is styled with 'overflow-y: auto', it will display additional horizontal and vertical scrolling bars

I have two files, one HTML and one CSS. The HTML file contains the following code: <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> ...