Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Understanding Media Query

Featured Replies

Hi everyone,

 

I can't seem to understand media queries. I understand it sends a question on your browser or screens settings. Then runs conditions based on that query.

 

But I cannot seem to understand how mine is working (no I didn't make it, a very helpful member on here did it for me; thanks :santa_kiss: ).

 

Could anyone shed some light on helping me grasp it?

 

The way I see it; Media Query changes when larger screen it gives me 4 list items (25%). BUT WHAT'S HAPPENING WHEN IT'S GOING FOR A SMALL SCREEN; I WOULD EXPECT TO SEE UL#NAVBAR LI {

WIDTH: 100%

}

 

BUT I DO NOT SEE THIS, HOW IS THE BROWSER BEING TOLD TO MAKE THE WIDTH DIFFERENT ON SMALLER SCREENS?

 

(NOT SHOUTING WITH CAPS, JUST TRYING TO CLEARLY SHOW MY QUESTION AS IT'S A LONG POST).

 

Grateful for any input.

 

My navbar css media query is:

 

@media all and (min-width: 480px) {
    /* SHOW NAV ACROSS FULL WIDTH OF SCREEN, GIVE 25% WIDTH TO EACH LIST ITEM (THERE's 4 IN TOTAL, SO DIVIDE 100% BY NUMBER OF LIST ITEMS  */
    #navbar li {
        width:25%;
        float:left;
    }
    
    /* TRADITIONAL STAIRS AND COMPLEX STAIRS */
    #navbar li ul li {       
        position:relative; 
        width:100%;         
    }
    
    /* SUB LEVEL OF TRADITIONAL STAIRS AND COMPLEX STAIRS */
    #navbar li ul li ul  {        
        position:absolute;
        left:100%;        
        width:100%;
        top:0;    
    }  
}

CSS before media query:

 

/* HIDE SUB-MENU AND SUB-SUB MENU */
#navbar {
width: 100%;	
z-index: 8000;
position: absolute;
}

ul#navbar {
font-size: 2em;
}

ul#navbar ul {
font-size: 0.9em;	
}

ul#navbar ul ul {
font-size: 0.5em;	
}

#navbar ul, #navbar ul li ul li { display:none; }

/* GENERAL LIST STYLES */
#navbar li {  
    background:#1A1A1A; /* BLACK LIST BACKGROUND */
    text-align:center;    
    border-bottom:1px white solid; /* ADD BORDER UNDERNEATH EACH LIST ITEM */
}

#navbar li a {
    text-decoration:none; /* REMOVE UNDERLINE FROM LINKS */
    display:block;
    color:white; /* TEXT COLOR */
    padding:5px 0; /* ADD SOME SPACE TOP AND BOTTOM OF LI ITEMS */
}

/* SUB LIST STYLES */
#navbar ul li a {
    color:black;
    background: #eee;   /* LIGHT GREY */
    padding:5px 0;   
}


/* REMOVE BORDER FROM LAST SUB-LIST ELEMENT, <li class="last">... */
#navbar ul li.last {
border-bottom:none;
}

/* SHOW SUB-MENU WHEN ABOUT IS HOVERED */
#navbar li:hover ul  { 
display:block; 
}

/* SHOW SUB-SUB MENU WHEN 'This will show another sub-menu' is hovered */
#navbar li ul li:hover ul li  {
display:block;
}

/* SUB-SUB MENU STYLE e.g. sub menu of Complex Stairs */
#navbar li ul li ul li a {
background:lightblue;
}

/* HOVER OF ABOVE */
#navbar li ul li ul li a:hover {
background: #eeeeee;
}

 

The 25% applies to 25% of the screen the Media Query is configured for. In this case, you could set the link to 100% either in your base stylesheet (mobile-first) or in a media query that may resemble something like:

 

@media only screen and (max-width: 480px) {
      Navbar li {
            width: 100%;}
}

For the record, I don't think your post was too long so no need to italicise and type in uppercase in future - just keep your paragraphs in order ;)

  • Author

Thanks for your reply 4's. What I don't understand is that when I resize my screen to under 480px the width of the <li> items becomes 100%. Is that because the browser automatically assumes it to be 100% width from it's parent's div?:

#navbar {
width: 100%;	
z-index: 8000;
position: absolute;
}

I was thinking there would have to be another section like you said with:

 

@media only screen and (max-width: 480px) {
      Navbar li {
            width: 100%;}
}

but I can't see anything like that unless I am missing something? The page is at http://concretestairslondon.co.uk/concrete-stairs/helical/spiral-stairs.php#

I have to go out for a bit - I'll have a look at your site when I come back and get a chance. Are you designing Mobile-First or are you doing (the natural way!) desktop then sizing down? If you are starting with the mobile, then the nav will be responding to your base styles. Have a look through if stylesheets. Depending on what text editor you use, do a search for the classes and IDs. I use Sublime Text 2 and you can do a search through all your files for specific text. You might find you're overiding something. Have a look in Chrome Developer Tools or Firebug as well but I'll try have a look when I'm back.

  • Author

Thanks 4's, I only have one stylesheet (others for third party like lightbox etc) but I designed our site so it would be well compatible for mobile devices, then if it's really small the navbar becomes mobile friendly (well I didn't do that but someone helped me). But I can't see how it does that.

 

Trying to learn it as it's so cool, would love to know how it works and use again for future sites and other things.

If your media queries aren;t working perhaps you need the meta viewport tag?

 

 

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

Thanks for your reply 4's. What I don't understand is that when I resize my screen to under 480px the width of the <li> items becomes 100%. Is that because the browser automatically assumes it to be 100% width from it's parent's div?:

#navbar {
width: 100%;	
z-index: 8000;
position: absolute;
}

I was thinking there would have to be another section like you said with:

 

@media only screen and (max-width: 480px) {
      Navbar li {
            width: 100%;}
}

but I can't see anything like that unless I am missing something? The page is at http://concretestairslondon.co.uk/concrete-stairs/helical/spiral-stairs.php#

 

In the larger screen sizes float and width is used to get the <li>'s to sit next to each other this isn't applied to the smaller screen size thats why they sit on top of each other

In the larger screen sizes float and width is used to get the <li>'s to sit next to each other this isn't applied to the smaller screen size thats why they sit on top of each other

 

Awesome :) I was just working on this and you beat me to it ;)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.