What are some tips for getting media queries to function properly?

I need help with my homepage banner image as I am trying to make it responsive by adding media queries. The goal is for the image to adapt to 3 different sizes based on the screen being used, but currently only the medium-sized version appears. Could someone please review my code and let me know what I'm doing wrong? Thank you.

Below is the code in question:


      /*---------------------------------------------
         Section#Slider [Banner Image]
      -----------------------------------------------*/

      /* NEXUS 5 Size 412px, XS (less than 786px no media query since this is default in Bootstrap) */
      /* IPAD SIZE 768px and up, S */

      @media screen and (min-width: 550px) {
         #slider {
            background: url("../img/smartphoneweb_640x285.jpeg") no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: 10% 0%;
            padding: 200px 0 280px 0;
            position: relative;
         }
      }

      /* MD, desktops, 992px and up */

      @media (min-width: 950px) {
         #slider {
            background: url("../img/smartphoneweb_1280x570.jpeg") no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: 10% 0%;
            padding: 200px 0 280px 0;
            position: relative;
         }
      }

      /* LG, large desktops, 1200px and up */

      @media (min-width: 1200px) {
         #slider {
            background: url("../img/smartphoneweb_1920x855.jpeg") no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: 10% 0%;
            padding: 200px 0 280px 0;
            position: relative;
         }
      }
    

Answer №1

The error that occurred was the omission of the screen attribute in the 992px media query. I've implemented the necessary changes to rectify this issue.

/*---------------------------------------------
      Section#Slider [Banner Image]
-----------------------------------------------*/
/* NEXUS 5 Size 412px, XS
(less than 786px no media query since this is
default in Bootstrap)
*/

/* IPAD SIZE 768px and up, S*/

@media screen and (min-width:0px) and (max-width:550px) {
    #slider {
        background: url("../img/smartphoneweb_640x285.jpeg") no-
        repeat;
        background-size: cover;
        background-attachment: fixed;
        background-position: 10% 0%;
        padding: 200px 0 280px 0;
        position: relative;
    }
 }

/* MD, desktops, 992px and up */

@media screen and (min-width:551px) and (max-width:992px){
    #slider {
        background: url("../img/smartphoneweb_1280x570.jpeg") no-
        repeat;
        background-size: cover;
        background-attachment: fixed;
        background-position: 10% 0%;
        padding: 200px 0 280px 0;
        position: relative;
    }
}

/* LG, large desktops, 1200px and up */

Answer №2

If you want to adjust the style for a specific screen size, you can utilize the min and max media query.

    @media screen and (min-width: 550px) and (max-width: 950px){
        #slider {
            background: url("../img/smartphoneweb_640x285.jpeg") no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: 10% 0%;
            padding: 200px 0 280px 0;
            position: relative;
        }
     }

    /* MD, desktops, 992px and up */

    @media (min-width: 950px)  and (max-width: 1200px){
        #slider {
            background: url("../img/smartphoneweb_1280x570.jpeg") no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: 10% 0%;
            padding: 200px 0 280px 0;
            position: relative;
        }
    }  
    @media (min-width: 1200px){
        #slider {
            background: url("../img/smartphoneweb_1920x855.jpeg") no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: 10% 0%;
            padding: 200px 0 280px 0;
            position: relative;
        }

    }

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

Customize the appearance of the submit button by using CSS to replace

Currently, I am working on a CSS userstyle project. The issue that I am facing is the inability to make modifications to images in buttons coded within the html of a web page: <button type="submit" class="btn btn-default"><img alt="Ico-plus" src= ...

Adjusting the layout of a webpage using CSS

I am facing a challenge where I need to adjust the layout of a webpage using CSS for mobile view. Currently, the desktop version has a sidebar floated to the left and a textbox floated to the right. The HTML structure is as follows: <div id="container" ...

What is the reason that my "width: auto" property is not properly adjusting to the content's width?

My css code, width: auto is causing issues with the content width I am a beginner in HTML, CSS, and JavaScript, seeking help with my website. I have multiple divs within a section element, but the section's width is not adjusting to fit the divs prop ...

What is the best way to create a flexible grid that automatically adjusts to either two columns or one column depending on the width of the

My challenge lies in arranging items into two columns or one, with a specific layout for mobile devices. The order of the items changes between the two column and single column layouts. The number and size of items vary dynamically. For example, in a tw ...

Reposition and resize an image. Creating a grid layout

I'm having trouble with positioning and cropping an image correctly on my website. I want it to look like this (hero-section): The entire project is based on a grid layout. I need a circular image that is larger than the container, positioned to the ...

How can I achieve a similar layout in my .cshtml file?

https://i.sstatic.net/f1C0G.png Hey there, I'm looking to achieve a layout similar to the one shown in the image. Specifically, I want the left side panel to remain unchanged when expanding the Accordion control on the right side. Can someone guide ...

Can a HTML div be created with an animation and a hover-scale effect added to it?

I experimented with using divs as clickable buttons that direct to another page on my website. I added animations to make them rotate into view when the site is loaded. Now, I'm trying to implement a hover effect that will scale the buttons when hover ...

Issue with jQuery incorrectly calculating height post-refresh

I am currently utilizing jQuery to vertically center various elements on a webpage. Due to lack of support in older versions of IE, I cannot use the table-cell CSS statement. Therefore, I am using jQuery to calculate half of the height and then position it ...

Are the links drifting to the left?

Some of the links on my website seem to be misbehaving and not following the CSS rules, floating to the left unexpectedly. Strangely, it's only the links that are affected; the rest of the text appears fine. For example, you can observe this issue at ...

Ionic version 4 ion-img eagerly preloads images instead of implementing lazy-loading

I recently created a horizontal scroller using FlexLayoutModule. It allows me to display a horizontally scrollable list where each item consists of an image and text column. Here's the relevant snippet from my scroller.component.html: <div class=" ...

Responsive Design: Concealing a Sidebar with CSS

Seeking assistance with optimizing the layout of my app, . It currently displays a menu on the left and a login form in the center, with a graph shown to users upon logging in. Is there a way to configure it so that when accessed on iOS: 1) the left menu ...

How can I specify a CSS class pair within a CSS module in React?

<NavLink to={rootPath + path} activeClassName="active" className={scss.navlink} > <ListItem button key={name}> <ListItemIcon> <Icon htmlColor="#E1F ...

Using data-image as the source for Bootstrap Modal

I am currently working on an image gallery that utilizes the Paver jQuery plugin. The gallery is functional, but I am facing an issue where it displays the same image in the modal instead of showing the respective data-image for each image. My goal is to ...

Setting a const value (true or false) based on the size of the window - a step-by-step guide

While studying, I encountered a challenge: In the code below, I need to dynamically set useState(false) based on the screen size. If the screen is larger than 947px, for instance, it should automatically be changed to true. The issue arises because sett ...

Issue with div in simple HTML code

I've been facing challenges with this line of HTML as I keep encountering errors and cannot pinpoint the source. The error highlight appears at the end of the last div, but once removed, another error emerges at the end of the body tag. Check out the ...

After refreshing the page, users are redirected back to the login page

On my website, I created a main page and an index.html page that redirects to the main page through a login panel. The issue I am encountering is that when I log in successfully on the main page and then refresh the page, it takes me back to the login pag ...

How to efficiently center a jQuery Mobile Dialog on the screen using custom dimensions

I designed an app using jQuery Mobile and I'm looking to display a Dialog box when a button is clicked. The Dialog box should be centered on the screen with specified width and height. For reference, you can view the current version on jsfiddle. The ...

What is the best way to center a form element and an image link vertically within a div?

I'm having trouble aligning a search form and an image link within a div. I've attempted using 'display:inline', which did place them on the same line, but not in the way I wanted. I also experimented with adjusting width, float, and pa ...

How to disable scrolling for React components with CSS styling

When incorporating a React component into my HTML, I follow this pattern: <html> <body> <div id=app>${appHtml}</div> <script src="/bundle.js"></script> </body> </html> Within my application, th ...

Fully responsive header designed for optimal experience at any screen height

I am facing issues with the header and cannot seem to find a solution. My goal is to make the header span 100% of the window screen height. I need a responsive header that adjusts to 100% height, and when resizing for a smaller viewport, nothing should sho ...