IDs within CSS remain consistent across different media queries

Having trouble updating my media query properly. Check out this example HTML:

 <!DOCTYPE HTML>

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="main.css">
        <title>Test </title>
    </head>
    <body>
        <div class="row">
            <div id="verde" class="col-5">
                <h2>Lorem Site</h2>
                <h3>Test Site</h3>
            </div>
        </div>
        <div class="row">
<div class="col-12">
    <p>Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
</div>
        </div>
        <div class="row">

<div id="verde" class="col-4">
                <p> Col 4Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
            </div>
            <div id="verde" class="col-4">
                <p>Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
            </div>
            <div class="col-4">cOL4
                <p>Bacon ipsum dolor sit amet pastrami ribeye sausage, sirloin short ribs pork belly strip steak tenderloin boudin. Landjaeger beef ham hock, doner bacon pork belly filet mignon tenderloin short loin beef ribs tongue. Bresaola salami short ribs venison, pastrami jerky landjaeger. Hamburger meatball cow pork loin ham. Sirloin tail venison jerky kevin. Turducken ribeye sirloin fatback tenderloin short loin. Boudin sirloin tail venison filet mignon rump leberkas landjaeger bacon shank beef ribs ground round. Tail boudin landjaeger rump, chuck shankle leberkas pork belly pork tongue brisket short ribs ground round. Strip steak jowl pork loin beef ribs, ribeye tail spare ribs rump</p>
            </div>
        </div>
    </body>

</html>

Here is the CSS being used:

    @media screen and (max-width:760px){
     #verde{
        color:red;
    }
}
*{
    border: 1px solid red !important;
}
*{
    box-sizing:border-box;
}

p {
    margin: 10px;
}
#verde{
    color:green;
}
.row{
    width: 100%;
    margin:auto;
    display: flex;
    flex-wrap:wrap;
}
.col-1{
    width: 8.33%;
}
.col-2{
    width: 16.66%;
}
.col-3{
    width: 25%;
}
.col-4{
    width: 33.33%;

}
.col-5{
    width: 41.66%;
    margin-left:auto;
}
.col-6{
    width: 50%;
}
.col-7{
    width: 58.33%;
}
.col-8{
    width: 66.66%;
}
.col-9{
    width: 75%;
}
.col-10{
    width: 83.33%;
}
.col-11{
    width: 91.66%;
}
.col-12{
    width: 100%;
    margin-left:20%;
}

The issue I'm facing is that the media query only works when I am more specific with the selection. For example, using body #verde . Any insights on why this is happening?

Thank you in advance

Answer №1

To ensure proper styling hierarchy, it's important to place your media query at the end of your stylesheet where styles cascade accordingly.

If your media queries are placed first, any styles that come after may be overwritten if they are not more specific.

...Code before...

@media screen and (max-width:760px){
     #verde{
        color:red;
    }
}

I have rearranged your CSS in this JSFiddle for reference.

Answer №2

The issue arises from the placement of your media query at the start of your CSS file, causing it to be overridden by subsequent "generic" styles. To resolve this, consider relocating the media query to the bottom of your CSS or implementing a secondary media query specifically for styling #verde when the viewport width exceeds 760px.

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

CSS3 animations: the speed of transitioning between animations is sluggish

The slider I created has animations that are taking too long to transition between slides. I have tried adjusting the properties, but I can't seem to find the right one to control the speed of the transitions. /* Keyframes */ @-webkit-keyframes anim ...

Retrieve the array index of the parent node of the clicked element

I am looking for a way to click on a button, retrieve its parent array number, and then apply a specific style to only one of the many divs with the same class name. Ideally, I would like to accomplish this using vanilla JavaScript. <div class="c ...

Changing the border color of a Bootstrap 5 button using custom CSS styling

I have been working on a page to control my amp using various JavaScript libraries. 1.) How can I change the blue border of a selected Bootstrap 5 button to a lighter green without using SCSS? Example: Repository (apologies for the mess, it's not t ...

Showing ngx-dropdowns within a table while utilizing the overflow visible property

I am experiencing an issue with the NGX dropdown menu within a scrollable table. The problem arises on the last row, where the dropdown is cut off due to the overflow being hidden by default. https://i.sstatic.net/2JfUY.png To address this issue, I appli ...

Is there a way to position the icon in the top left corner within the image using Vue and CSS?

I am currently working on a Vue project and I am attempting to create a row of images with an icon positioned at the top left corner of each image. Here is my code snippet: <div v-for="(image, index) in images" :key="index" class=&q ...

Add two cells below a header with a colspan of one

I've been having trouble grouping the cells with the values { Lazy, Dog, Then, It } under a single header (with a colspan of 1) So far, I've attempted using div tags within the cells, creating multiple cells, and trying various combinations of w ...

Angular 8 fails to retain data upon page refresh

I have a property called "isAdmin" which is a boolean. It determines whether the user is logged in as an admin or a regular user. I'm using .net core 2.2 for the backend and Postgre for the database. Everything works fine, but when I refresh the page, ...

When you click on the image link, watch as the page gracefully transitions to a new one determined by the URL

I need assistance with a functionality where an image inside a div has a link to the next page. When this image is clicked, it should smoothly transition to the new page specified by its link. Any help with implementing this would be greatly appreciated. ...

Persistent footer overlaps lower body content

Issue with Sticky Footer in Safari I recently added a sticky footer to my website, but I'm facing problems with it covering the body content on Safari. It works fine on Chrome and Firefox after adding a bottom margin to the body to adjust for the hei ...

Internet Explorer 7 does not display icons

When I look at my icon, I'm calling it like this: <td> <i id="sthAdd" class="icn_plus"></i> </td> and the class looks like this: .icn_plus { background: url(../images/icons/plus-14x14.png) no-repeat; width: 20px; heig ...

Bootstrap button groups do not support the checking functionality of radio buttons

My bootstrap modal contains a form with two radio style buttons created using the example for normal button styled radio options found here: <form id="changePlanForm" action="/change_plan_submit.php" method="post"> & ...

Identifying whether a class exists on the same level using a selector

Snippet display: $( document ).ready(function() { //$(".tree-node").parents('.tree-child-folders').css("color", "red"); //$(".tree-collapsed:has(.tree-child-folders)").css("color", "red"); $(".tree-node > .tree-collapsed:has(.tree-child ...

Resolving the issue: "Unable to destructure the 'Title' property of 'fac' as it is undefined" in React JS

I'm facing an issue with react-bootstrap mapping in my component whereby I encounter the following error: Cannot destructure property 'Title' of 'fac' as it is undefined. It seems like there's an error when trying to de ...

Guide to crafting a flawless pixel-perfect separator with CSS borders

I have a menu with a black background and a submenu with a grey background. Now, I want to add a pixel-perfect divider between them. However, I'm unsure of which border color to use in order to achieve this perfection. Can anyone provide some guidan ...

How can I move a child element off-center within a parent div in CSS without affecting the position of the other child

Below is an example of code that demonstrates what I'm trying to accomplish: <nav className="flex flex-row justify-between items-center w-full h-[100px]"> <div className="flex flex-row items-center sm:pl-0 ml-auto"> ...

Hiding my valuable content with a sneaky CSS nav bar

My current issue involves the nav bar overlaying my content Here is a link to the problem for reference I am seeking advice on how to resolve this issue, as I'm unsure of what steps to take The nav bar functions properly in responsive/mobile mode, ...

Flexbox transforms Safari's Horizontal Nav into a Vertical layout

Issue with Horizontal Navigation Bar Turning Vertical in Safari I've been using flexbox to adjust the spacing and size of the li elements as the browser window size changes. Everything works perfectly fine in Chrome and Firefox, but for some reason, i ...

Is there a way to display the full text of a lengthy select option?

I'm facing a challenge with long <option>s in my <select> box - I don't want the box to be too wide, but when I style it smaller, the full text gets cut off. One idea is to hover a duplicate of the text exactly over the option the mou ...

I'm struggling to convert this vertical navigation bar into a horizontal layout using CSS

<nav class="navbar navbar-dark bg-dark sticky-top"> <a class="navbar-brand" href="#">I/H</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item active"> ...

Use PHP to transform an array into JSON format, then access and retrieve the JSON data using either jQuery or JavaScript

I have successfully generated JSON data using PHP: $arr = []; foreach($userinfo as $record) { $arr[] = array( 'BAid' => $record->getBAid(), 'BCid' => $record->getBCid(), 'BA ...