Resizing the screen does not trigger media queries to take effect

I am facing an issue with media queries. I am trying to set the width property of a container to 100% of the page, but it is not working as expected. I am using Bootstrap as my CSS framework. Here's a snippet from the inspector:https://i.sstatic.net/PaUzk.png

Code:

<div class="container-flex" id="blog-posts">
    <div class="post-left">
        <div>
            <img class="img-responsive" src="images/content/blog/post-image.jpg">
        </div>
    </div>
    <div class="divider">
        <div class="divider-dot"></div>
        <div class="divider-line"></div>
    </div>
    <div class="post-right">
        <div>
            <time>10 April 2014</time>
            <h3>Typi non habent claritatem insitam</h3>
            <p>Duis autem vel eum iriure dolor in hendreit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui [...]</p>
        </div>
    </div>
</div>

CSS:

@media only screen and (max-width : 767px) {
    #blog-posts {
        display: flex;
        flex-direction: column;
        width: 100%;
        border: 1px solid blue;

        .divider {
            display: none;
        }

        .post-left {
            ;
            width: 100%;

            div {
                //width: 100%;
                border: 1px solid;
            }
        }

        .post-right {
            width: 100%;

            div {
                width: 100%;
                border: 1px solid;
            }
        }
    }
}

...

#blog-posts {
    display: flex;

    .post-left {
        width: 47.95%;
        //border: 1px solid blue;
        div {
            width: 50%;
            float: right;

            img {
                float: right;
            }
        }
    }

    .divider {
        //border: 1px solid;
        width: 4.1%;

        .divider-dot {
            border-radius: 50%;
            background-color: rgb(235, 235, 235);
            width: 17px;
            height: 17px;
            margin: 0 auto;
        }

        .divider-line {
            width: 1px;
            height: 100%;
            background-color: rgb(235, 235, 235);
            margin: 0 auto;
        }
    }

    .post-right {
        //border: 1px solid green;
        width: 47.95%;

        div {
            width: 50%;

            time {
                font-size: 16px;
                font-family: "Raleway";
                color: rgb(123, 108, 99);
                line-height: 1.875;
                text-align: left;
            }

            h3 {
                font-size: 24px;
                font-family: "Raleway";
                color: rgb(33, 33, 33);
                line-height: 1.25;
                text-align: left;
            }

            p {
                font-size: 16px;
                font-family: "Raleway";
                color: rgb(148, 148, 148);
                line-height: 1.875;
                text-align: left;
            }
        }
    }
}

Answer №1

In accordance with the advice from previous comments, it is important to place media queries after your default styles so that they can override them if necessary.

Here is how your code should be structured:

#blog-posts{
  // styles...
}

@media only screen and (max-width : 767px) {
  #blog-posts{
    // styles...
  }
}

To illustrate this concept further, consider overwriting a class:

div {
  height: 100px;
  width: 100px;
}
.color {
  background: red;
}
.color {
  background: green;
}
<div class="color"></div>

As demonstrated, the block appears green instead of red.

The same principle applies to media queries, with the distinction that they only take effect when the viewport exceeds the specified size.

Answer №2

Your media query style is being overridden by a different rule lower in your scss file.

Try relocating that media query block to the bottom of your style.scss file.

Edit

The reason for this issue is that you initially define the rule for your media query and then have another conflicting rule later in your code, causing it to overwrite the previous one.

For example:

@media (max-width: 768px) {
    .my-class {
        color: red;
    }
}

.my-class {
    color: blue;
}

In this scenario, any element with the class my-class will display the color blue, regardless of screen size. The last rule specified takes precedence over the prior one. To rectify this, consider altering the order of these rules:

.my-class {
    color: blue;
}

@media (max-width: 768px) {
    .my-class {
        color: red;
    }
}

Following this adjustment, elements with the class my-class will appear in blue if the screen width is at least 768px. If the screen width falls below 768px, the media query rule will supersede the initial rule, assigning a color of red.

Please note that there are alternate solutions available as well. One alternative would be to include !important within your media query, though it is advisable to minimize its usage if other resolutions exist.

Another approach involves enclosing your class within an additional media query, where the order does not impact the outcome:

@media (max-width: 768px) {
    .my-class {
        color: red;
    }
}

@media (min-width: 768px) {
    .my-class {
        color: blue;
    }
}

Answer №3

Remember to always include !important at the end of the width:100% declaration

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

How to position the figcaption within a card?

My card design features a masonry-style layout similar to what you can see here https://codepen.io/daiaiai/pen/VPXGZx I'm trying to add some additional information on top of the images using figcaptions, but my attempts with position:relative haven&a ...

Space left unattended at the lower edge of the sheet

I'm in the final stages of creating the layout for my forums and have been struggling with a persistent gap at the bottom of the page, just below the footer. Despite trying various solutions recommended by others with similar issues, I still can' ...

Employ the Google Charting library for creating a GeoChart that is currently not displaying

Hello everyone. I'm having a bit of an issue with my web page development. I've been trying to add a GeoChart, but no matter how many times I copy-paste the code from the Google developer's website, the map just won't show up. I must be ...

Utilize a unique CSS selector and content property to insert a space character between two inline-block elements

I have a group of connected span elements with the class tag from bootstrap v4. Because they are generated from a react.js array of JSX elements, they sit adjacent to each other without any spacing between them: <span class="tag tag-primary">text< ...

Range slider in Bootstrap showing values as labels

After reviewing the Bootstrap 4 documentation and various responses on Stack Overflow, I am still unable to locate a method for the range slider to show the value as the track is adjusted. https://i.sstatic.net/Iu0ug.png I have come across open-source pr ...

To see website changes, refreshing with Ctrl + F5 is necessary in the browser

I have a simple website that uses only HTML and CSS. Whenever I upload new files to the site, they do not appear in the browser until I perform a hard refresh by pressing CTRL + F5. Does anyone have any suggestions on how to fix this issue? ...

The core-icons icon doesn't appear as intended in version 1.2.1 of Polymer

Currently, I am working with Polymer version 1.2.1 and have included core-icons in my elements.html file using the following import statement: <link rel="import" href="/bower_components/core-icons/social-icons.html"> Within a custom Polymer element ...

What is the reason for Angular Material using pixels to measure fonts?

We are in the process of developing an Angular Material application, and our designer has requested that we convert all CSS styles to be measured in em units. He believes that using px will require multiple styles for different media query breakpoints. Cur ...

Align the label vertically with the corresponding input field

I am currently in the process of revamping an old HTML form to eliminate the use of tables. I found a solution on this site to give the label a fixed width, and here is how it looks in the current code: HTML <div id="form"> <label for="field ...

Tips for deleting an HTML row and removing an entry from a MySQL database through the use of AJAX

Utilizing a mix of HTML, PHP, Javascript, and AJAX, I am creating an HTML table populated from a MySQL database. The goal is to implement a functionality where a row can be removed from both the table and the database using a delete button. Here's wha ...

Guide to updating the value of a CSS seconds dynamically

let timerSeconds = 10; // This value will be retrieved from a database function StartCountDownTimer() { let timerStart = new Date(); let css_seconds = `${timerSeconds}s`; const css_root = document.querySelector(':root'); let cssVa ...

Choosing pseudo-elements with CSS styling rules

I have been utilizing the Brave browser to block online ads. However, certain websites have found a way to insert ads into their HTML on the server-side, bypassing my ad-blocking efforts in Brave. Currently, Brave only offers the ability to block elements ...

Use Angular2 to showcase the selected image as the main one when the user clicks on the

I'm working on creating a product thumbnail gallery, and I'd like the main image to be displayed when the user clicks on a thumbnail. I am using Angular for this project, although I am still learning my way around the framework. product.html &l ...

A bar is seen above the directive within the partial

My goal is to design a user interface using Angular on the front end. I have a directive called navbar. Despite using ngRoute, I incorporated this directive in my partial for /home. Everything seems to be functioning properly, but the navbar doesn't a ...

A 4-image panel grid featuring a central blank space, perfect for an email template design

Attempting to replicate this 4-image layout like the one shown below, but facing challenges due to email template constraints that prevent the use of negative margins. I tried floating the images to the left and using: position: relative; top: -px; but i ...

The class is failing to be applied to the parent div that holds an id starting with the letter x

I have been attempting to assign a class to the parent container when the child element has the 'hidden' class. If not, then a different class should be added. function tagMissions() { if ($('span[id^="mission_participant_new"]').h ...

Display 3 buttons only on screens smaller than 576 pixels, and hide them on wider screens

Struggling to get these buttons to show up, but nothing seems to work! Just stumbled upon a code that should make them visible under 576 px: However, only the navbar icon is appearing... https://i.sstatic.net/sDMJv.png I even tried consulting two AI ...

Does the webpack style loader load only application-specific CSS, or is it designed to handle all CSS files?

I am in the process of improving the modularity of my front-end design by delving into webpack. This tool provides a style loader, which enables you to import a css file and inject it into the document like this: require("style/url!file!./file.css"); // = ...

How to Customize Background Colors for Blogspot Posts?

Recently, I started my own blog using Google Blogger and I've run into a minor issue. Despite applying a background image for the entire blog, all of my posts have a white background instead. I haven't used any background-color in the HTML code o ...

Utilizing the Aladin Lite application within a React application, despite not being specifically designed for React

I am interested in incorporating the Aladin Lite app into my React application. Typically, embedding the app in a div is straightforward when developing a website without React: <!-- include Aladin Lite CSS file in the head section of your page --> ...