The media breakpoints do not seem to be working as intended, it could be due to

My media queries are not working as expected. To illustrate, I have created a simplified example below.

In this example, the "myDiv" div should display:

  • "normal width" when no media breakpoints are active
  • "mw1200" when the screen has a max width of 1200px
  • "mw992" when the screen has a max width of 992px
  • "mw768" when the screen has a max width of 768px

However, it currently displays "normal width" when the screen is over 1200px and "mw1200" when below.

.myContainer {
  position: absolute;
  top: 100px;
  left: 100px;
  color: #FFF;
  background-color: #000;
  padding: 10px;
  width: auto;
  height: auto;
}

.myDiv {
  padding-left: 10px;
  padding-right: 10px;
}

.myDiv:after {
  content: 'normal width';
}

@media screen and (max-width:768px) {
  .myDiv:after {
    content: 'mw768';
  }
}

@media screen and (max-width:992px) {
  .myDiv:after {
    content: 'mw992';
  }
}

@media screen and (max-width:1200px) {
  .myDiv:after {
    content: 'mw1200';
  }
}
<div class="myContainer">
  <div class="myDiv">
    myDiv inside myContainer
  </div>
</div>

Answer №1

If I were to revamp this, my approach would be mobile-first. Consider the following structure:

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>

.myContainer {
    position: absolute; 
    top: 100px;
    left: 100px;
    color: #FFF;
    background-color: #000;
    padding: 10px;
    width: auto;
    height: auto;
}
.myDiv {
    padding-left:10px;
    padding-right:10px;
}

.myDiv:after {
    content: 'mw768';
}

@media screen and (min-width: 769px) {
    .myDiv:after {
        content: 'mw992';
    }
}

@media screen and (min-width: 993px) and (max-width: 1200px) {
    .myDiv:after {
        content: 'mw1200';
    }
}
</style>
</head>
<body>
<div class="myContainer">
    <div class="myDiv">                             
        myDiv inside myContainer
    </div>
</div>
</body>
</html>

This strategy enables a more concise implementation with no compromise on functionality.

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

Pause autoplay in Bootstrap v5.0 Carousel when a carousel item is clicked

I've been attempting to disable the carousel autoplay using jQuery after clicking on a carousel item, but all my efforts have been unsuccessful. Can anyone provide some assistance? Thank you! This is what I've tried so far: $(".carousel-i ...

Is there a way to retrieve the Location header of a 302 redirection URL using PHP?

Seeking a solution to expand shortened URLs universally. I am aware that popular services like bit.ly, TinyURL, and goo.gl use 302 redirection for forwarding users to new destinations. How can a HEAD request be made in PHP to retrieve the "Location" sectio ...

Utilizing Bootstrap divs for multiline display in Yii2 platform

Currently, I am utilizing a list in Bootstrap CSS which is responsive and beneficial. However, I am faced with the challenge of creating a notification list with lengthy text akin to that on Facebook. My query pertains to the technique required to make th ...

What is the best way to split the vertices of a mesh in Three.js?

Is there a way to disassemble the vertices of a Three.js mesh? I am interested in tearing apart a 3D object using mouse input. Recently, I developed a GLSL vertex shader for this web application that shifts the vertices of a mesh based on the brightness of ...

CSS Flexbox: Dealing with Overlapping Flex Items on Narrow Screens

Hey there! I've successfully created a custom timeline using flexbox, but I'm encountering an issue on smaller screens. As the window size decreases, the flex items start to overlap. Is there a way you can assist me in adding some space without d ...

Is there a way to extract the content from a dynamic textbox using a dynamic button in HTML and AJAX?

My current task involves fetching data from a database and updating the records individually. I have created a table with a text input field and a button that will be generated dynamically for each record. The text input field is populated with the previou ...

Would you like to place some text within a content box?

I am facing a challenge where I want to insert content into a contentbar that has an opacity of 0.6. However, I do not want the content (such as text or videos) to have the same opacity as the contentbar. I attempted to place them in separate div tags with ...

Within jQuery lies the power to perform multiplication operations effortlessly

I'd like to accomplish this using jQuery: var menuItems = document.getElementsByTagName("li"); for (var k = 0; k < menuItems.length; k++) { if (menuItems[k].className == "menu") { var child = menuItems[k].firstChild; if ...

Using either Canvas.toBlob or Canvas.toDataURL results in me obtaining an image with a transparent

Hey there! I'm currently working on a project that requires the user to crop and upload images. For cropping, I am utilizing react-cropper. My challenge lies in dealing with Chrome's limitation on dataURL to 65529 pixels as mentioned in this M ...

Tips for removing borders around a textbox with a background image: When incorporating a background image into a

After removing the default borders on a textbox using outline:none;, I noticed that adding a background image caused the border to reappear, and I couldn't get rid of it! How can I solve this issue? Here is the code for the textbox: <input type = ...

I'm looking to have the checkbox automatically checked when the <p> Tag is clicked in JavaScript

I need to modify the checkbox input so that when I click on any Todo Item, the checkbox is checked. However, after implementing the code below, it only works for the first element clicked. How can I make it work for each individual todo list item without c ...

The process is consuming a significant amount of time to generate the expected result

My code is currently running all the queries inside a while loop and it's taking around 1 minute to complete. I need suggestions on how to optimize it for faster performance. Here is my code: <table style="font-size:13px" width="150%" border="0" c ...

The most efficient method for creating a substantial amount of pre-existing HTML within the DOM using jQuery

This particular webpage layout showcases articles contributed by users. Our platform allows users to publish their articles, which are added to the top of the page instantly while also being saved to the database via AJAX. <div class = 'article-wr ...

transferring a model or numerous parameters to another action using an anchor tag or hyperlink functionality

Displaying data in a grid-like table can be achieved using the code shown below: Column1 Column2 Colum3 Siva 1200 200 Ram 2000 300 To make specific row values hyperlinks, such as all values in Column2, you can use the following ...

Is there a way to upload numerous images from my local disk onto a canvas using Fabric.js?

I'm currently working on an innovative Image Collage application using the power of HTML5 canvas and Fabric.js. One of the key features I want to implement is the ability for users to simply drag and drop files into the designated 'File Drag and ...

Is there a way to specifically dictate the order in which flexbox items expand or contract?

I am facing an issue with my flex container setup involving three children elements. I have specified both minimum and maximum widths for each child, but I want them to grow or shrink in a specific order as the window width changes. Ideally, Item1 should s ...

Guide to extend the width of h1 container to fill the entire parent div

Here is a snippet of code showcasing parent and child elements along with their current styling main #main-content-wrapper div { padding: 0; margin: 0; } img { border-radius: 8px; } .overlay1 { position: absolute; top: 0; right: .1px; bo ...

Website route organization tool

Seeking assistance with implementing a route planning feature on my website. The user should be presented with multiple route options upon entering their origin and destination. Take a look at this reference site for inspiration: Any help would be great ...

Email validation causing an internal server error in Perl

Whenever I attempt to utilize Email::Valid in my CGI script, I am encountering the dreaded Internal server error 500. Being new to this, I am unable to pinpoint the root cause of the issue. My goal is to create a basic form validator using Email::Valid for ...

Make sure the inputs in separate table data cells are lined up in

I need help aligning two input fields in separate td elements to be on the same line. The issue I am encountering is that when an input is added to a td, it covers up any text within the td. https://i.stack.imgur.com/c7GiQ.png There are two scenarios: I ...