Media queries are behaving incorrectly, causing them to trigger when regular CSS should be applied, and vice versa

When I switch to laptop/desktop view, the regular CSS styles are being overridden in the inspect element, and the query styles are taking precedence. Can anyone explain why this is happening?

For instance, let's take a look at the code for the nav section (which was my main focus initially, as it was the only part I had managed to style responsively before encountering this issue).

I have double-checked that all the element names, divs, and ids are correct. I have tried using percentages and pixels, both individually and together. I have also confirmed that there are no duplicate min-width media queries that could be causing conflicts.

CSS

/* Nav Section */
    a.navbar-brand{
      font-size: 90% !important;
    }
/* About Section */

    .mlAboutSec{
      padding: 112px 0;
      background-color: #f9f9f9;
    }
    .mlAboutSec h1{
      font-size: 40px;
    }
    @media screen and (min-width:320px){
    .mlAboutSec h1{
      font-size: 20px !important;
    }
    .img-responsive{
      width: 50% !important;
    }
    a.navbar-brand{
      font-size: 90% !important;
    }
  }

Html

  <div class="mx-auto order-0">
    <a class="navbar-brand" href="#">Sample Logo Text</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
        <span class="navbar-toggler-icon"></span>
    </button>
</div>

<div class="jumbotron jumbotron-fluid text-center">
<img src="LogoImage.png" class="img-responsive" alt="Logo Image">
</div>

<div class="col-sm-8 fadeInUp animate">
        <h1>About Section</h1>
        <p>Sample Text Description about stuff goes here for the reader to view.</p>
    </div>

I was expecting the font sizes to decrease for better mobile readability, and for the logo image to be resized for optimal viewing on mobile devices.

Answer №1

Your default styles are getting overridden by your mobile styles on desktop, which becomes evident when we examine your CSS code closely.

Following the declaration of your regular styles, you introduce a media query with min-width: 320px.

.mlAboutSec h1 {
  font-size: 40px;
}
@media screen and (min-width: 320px) {
  .mlAboutSec h1{
    font-size: 20px !important;
  }
  .img-responsive {
    width: 50% !important;
  }
}

This setup signifies that the media query styles will take precedence for any screen wider than 320 pixels, including most desktops. As a result, your normal styles essentially never get applied since they only apply to screens less than 320px wide, even small smartphones like the iPhone SE exceed this width.

If you wish to establish default styles for desktop and override them for mobile, a quick fix would be to substitute min-width with max-width in your media query without altering anything else; this adjustment will align with your expectations.

Alternatively, if you're adhering to a mobile-first approach as indicated in the comments, a complete reversal of your implementation is necessary. Mobile styles become the default, while desktop styles migrate inside a media query.

For instance:

.mlAboutSec h1 {
  font-size: 20px;
}
.img-responsive{
  width: 50%;
}
@media screen and (min-width: 992px) {
  .mlAboutSec h1 {
    font-size: 20px;
  }
  .img-responsive{
    width: 100%;
  }
}

Note that shifting media queries to the top of your stylesheet won't solve the issue due to specificity rules. Media queries carry greater specificity than ordinary styles, leading them to override regardless of their position unless additional adjustments are made.

To streamline your styling process, it's advisable to place media queries either at the end of the stylesheet or directly after the corresponding default styles. Utilizing the suggested approaches above can help achieve the desired outcome effortlessly.

In the example provided for a mobile-first strategy, no !important declarations are required because the selectors remain consistent across media queries and default styles, maintaining harmony in specificity and cascade order.

Answer №2

After some tweaking, I successfully adjusted the font sizes of the text, headings, and links to ensure they are easily readable on various devices. Surprisingly, this simple change did the trick and the scaling now works seamlessly. Grateful for all the valuable suggestions and feedback I received. I made sure to consider every single one!

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

Error encountered while building Dart WebUI

I am encountering issues when attempting to integrate web_ui into my existing Dart application. I have not yet incorporated any of the webui-specific code into my HTML file, I am simply trying to build. I updated the pubspec.yaml file with the web_ui pac ...

How about trying out the magic of Bootstrap columns?

My issue pertains to bootstrap columns. Whenever I zoom in on the browser, the columns start overlapping and the text from col-md-7 ends up over the image. Does anyone know of a solution to this problem? <div class="row"> <div class="col-md-5 ...

Creating a sleek CSS animation for a slot machine effect: Ensuring it stops smoothly and at a specific time limit

I'm working on creating a CSS animation that mimics a slot machine, but I'm struggling to achieve a smooth transition. .scrollable { height: 150px; width: 150px; margin: 0 auto; padding: 0; background: #000; ...

Utilize the <a> element as a button to submit the data form

I am looking to transfer data from a form to another PHP page without using a button within the form itself. Instead, I have placed a separate button outside of the form for submission. How can I achieve this by sending the form data to the other page? Bel ...

What could be causing the position sticky to not function properly in my script?

Can someone help me troubleshoot an issue with the TopOptions image and component sticking to the top of the page? {!categorySearch && ( <div className="max-w-2xl mx-auto z-40 relative overflow-x-hidden font-metropolis_semibold" ...

"Implementing a button in a flask data table: A step-by-step guide

Hello, I am facing an issue with adding a button to a Bootstrap datatable in Flask (Python) using JavaScript. Any tips or solutions would be greatly appreciated. I am trying to achieve something like this: https://i.sstatic.net/NtaB3.png I have attempted ...

Adjusting the width of a Material UI select/dropdown component

In my material-ui grid setup, each <Grid> contains a <Paper> element to define the grid's boundaries precisely. Inside every <Paper>, there is a custom <DropDown> component (a modified version of Material-UI's Select). I ...

I am having trouble with the CSS Hover and Active styling on my website

Below is the code for my navigation bar: <ul> <li><a href="index.html">HOME</a></li> <li><a href="portfolio.html">PORTFOLIO</a></li> <li><a href="resources.html">RESOURCES</a ...

Designing a flexible layout that allows for both vertical and horizontal scrolling of content

I have successfully implemented a flexbox layout with fixed header, fixed footer, fixed menu, and scrolling content based on resources from various articles and Stack Overflow answers. However, I am facing difficulty in achieving horizontal scrolling for ...

How to conceal 'subnavigation' elements within Buddypress

Hey everyone, I've been looking into how to hide subnav items in a drop-down list. Can this be achieved using just CSS or will I need to write some code for it to work properly? My main goal is to have the sub nav items appear when the user hovers ove ...

What is the best way to smoothly reveal images one by one?

My goal is to smoothly slide three images inside a div, one by one. However, I'm facing an issue where the images stack on top of each other and end up stuck in their original positions. Here is the code snippet I've been working with: <div ...

What could be causing the PAGE CSS to malfunction?

I am looking to export HTML text as a word document with A4 size and portrait orientation. My JavaScript currently allows me to export the text, but it appears like a webpage format rather than in A4 or portrait mode. I have tried adding @page CSS styling ...

Python struggles to find HTML elements when gathering information from a website through

I attempted to extract data from a website by utilizing the following code snippet: import requests requests.get("myurl.com").content Unfortunately, certain crucial components of the website were not retrieved. Is there a way for me to access th ...

When scrolling, the text or logo inside the navbar seems to dance with a slight wiggling or

I recently implemented a code to create a logo animation inside my navigation bar that expands and contracts as I scroll, inspired by the functionality of the Wall Street Journal website. However, this implementation has caused some unintended side effects ...

Update the form field with today's date in a JHipster application

In our current project in JHipster, we are facing a challenge with setting the default value of a form field as the current date. JHipster offers a moment format for dates, which is essentially an ngbdatepicker. However, when attempting to change the inpu ...

Issue with table not remaining inside the div on the content page of WordPress site

I am having trouble with a div on my website. When I include a table in one div it resizes perfectly, but when I do the same in another div using a different template, it doesn't work as expected. To see the issue, please visit: Home page, bottom lef ...

Reducing div size when clicked - Using JQuery version 1.9

I am looking to make a specific div shrink in size, while keeping all the data visible, each time a user clicks on a certain icon with the class legend-icon. For example, I want the div with the ID #Chart to shrink when clicked. This is the HTML code: &l ...

The issue arises when trying to implement CSS keyframes with a CSS image slideshow

I'm currently troubleshooting a CSS slideshow that isn't functioning properly. The issue lies within the keyframes. I'm attempting to make the images come down from the top mask and then have the previous image go back up into the mask. This ...

Prevent scrolling while popup is open

I found a helpful tutorial online for creating a jQuery popup (). However, due to my limited understanding of jQuery, I'm having trouble getting it to meet my needs. The issues I'm facing are: I need to prevent the webpage from scrolling when ...

Trouble with integrating HTML5 canvas from an external JavaScript file

Having trouble with storing canvas js in an external file. If the javascript responsible for drawing on the canvas is included in the html header, then the rectangle is displayed correctly. Here is the working html (javascript in html header): <!DOCT ...