Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:56:53+00:00 2026-06-11T13:56:53+00:00

I have a menu which looks like this: |Home|Options|Settings|Tools|Preferences|Edit| That’s fine when a phone

  • 0

I have a menu which looks like this:

|Home|Options|Settings|Tools|Preferences|Edit|

That’s fine when a phone has lots of horizontal space, but when a device with a narrow viewport accesses the page, I want the menu to look like

|Home|Options|Settings|+MORE+|

Where clicking the “MORE” menu displays the other items in a vertical drop down.

I don’t want to set manual breakpoints, because I have no idea how wide the individual menu items will be when displayed.

My menu is currently just a set of <li> in a <ul>

The CSS for horizontal layout is

#menu ul, #menu li { margin: 0; padding: 0; list-style: none; }
#menu ul { overflow: auto; }
#menu li { float: left; }
#menu  a { display: block; padding: 0.5em; text-decoration: none; border-right: 1px solid #fff; font-size: 110%; }

I’m reluctant to use something like jQuery – even when minimized, it’s still a significant overhead for older mobile browsers. Media Queries are also problematic for some phones, so I would like to avoid relying on those.

Any thoughts on CSS and (simple) JavaScript to automatically hide elements depending on the browser’s width?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:56:54+00:00Added an answer on June 11, 2026 at 1:56 pm

    Actually, you can do it with no JavaScript at all, just with media queries (which really have excellent support + this solution I am presenting is a mobile first one) and :nth-last-child (which again is even supported by Opera Mini).

    demo

    (resize to see how it works)

    You’ll need to have a structure like this:

    <nav id='menu'>
        <ul>
            <li><a href='#'>Home</a></li>
            <li><a href='#'>Options</a></li>
            <li><a href='#'>Settings</a></li>
            <li><a href='#'>Tools</a></li>
            <li><a href='#'>Preferences</a></li>
            <li><a href='#'>Edit</a></li>
            <li><a href='#'>+ MORE +</a></li>
        </ul>
    </nav>
    

    Then you’ll need to select the Tools, Preferences and Edit and set their display to none:

    #menu li:nth-last-child(-n+4):not(:last-child) { display: none; }
    

    li:nth-last-child(-n+4) selects only the first four list items from the end. You add the :not(:last-child) condition to that because you want the + MORE + list item to be shown.

    In order to better understand structural pseudo-classes, you can play around with this tool.

    Finally, you’ll need to use a media query to change the display settings for larger screens:

    @media (min-width: 30em) {
        #menu li:nth-last-child(-n+4):not(:last-child) { display: block; }
        #menu li:last-child { display: none; }
    }
    

    I am using an em based media query and not a px based one for two reasons:

    • one, this article;
    • two, my own site looks like crap on zoom because I used px based media queries on it a year ago;

    EDIT: In order to make the menu expand on click and to make the number of menu elements shown vary with screen width, I have changed the structure a bit more:

    <nav id='menu'>
        <a tabindex=1 class='ctrl' href='#'>+ MORE +</a>
        <ul>
            <li><a href='#' class='menu-link'>Home</a></li>
            <li><a href='#' class='menu-link'>Options</a></li>
            <li><a href='#' class='menu-link'>Settings</a></li>
            <li><a href='#' class='menu-link'>Tools</a></li>
            <li><a href='#' class='menu-link'>Preferences</a></li>
            <li><a href='#' class='menu-link'>Edit</a></li>
        </ul>
    </nav>
    

    And also changed the CSS a bit:

    #menu .ctrl { float: right; }
    #menu ul, #menu li { margin: 0; padding: 0; list-style: none; }
    #menu ul { overflow: auto; }
    #menu li { float: left; }
    #menu li:nth-last-child(-n+5) { display: none; }
    #menu a {
        padding: 0.5em;
        text-decoration: none;
        border-right: 1px solid #fff;
        font-size: 110%;
    }
    #menu li a { display: block; }
    #menu li:first-child a { border-left: 1px solid #fff; }
    #menu .ctrl:focus, #menu .ctrl:active { display: none; outline: 0; }
    #menu .ctrl:focus ~ ul li, #menu .ctrl:active ~ ul li { display: block; }
    
    @media (min-width: 15em) {
        #menu li:nth-child(2) { display: block; }
    }
    @media (min-width: 20em) {
        #menu li:nth-child(3) { display: block; }
    }
    @media (min-width: 25em) {
        #menu li:nth-child(4) { display: block; }
    }
    @media (min-width: 30em) {
        #menu .ctrl ~ ul li { display: block; }
        #menu .ctrl { display: none; }
    }
    

    demo

    (I’ve also added a background with vertical lines at every 5em just to make it clear how wide the screen is when resizing the browser window)

    This method should work without JavaScript – tested that in desktop browsers, Opera Mobile, Android browser and iOS Safari. I don’t know about Opera Mini though – I’ll have to test that.

    EDIT#2: No, it doesn’t work in Opera Mini for me (the menu is collapsed, but clicking the + MORE + link does not expand it). Tried to make it work with JavaScript (no library), but that also doesn’t work in Opera Mini (though it works on desktop browsers).

    EDIT#3: Also tried to do the same thing using jQuery. This time it also works in Opera Mini. Really slow (at least for me), but it works. This is what I used:

    $('.ctrl').click(function() {
        $(this).css({'display': 'none'}).next().children().css({'display': 'block'});
    });
    

    EDIT#4: Now tried the :target method – demo (also CSS-only). Works fine on my laptop using Chrome (haven’t tested in another desktop browser), doesn’t work in Opera Mini (menu is collapsed, clicking the + MORE + link does not expand it). Works in Opera Mobile though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a menu which looks like this: When the user clicks on Recruitment,
So I have a menu in a php file that looks like this (This
I have a navigation menu which looks like this: I have to split it
I have a menu which looks like this , where on hovering over tanfa
I have a jQuery menu which is build up by like this: <h3 id='300'
I have a row of evenly-spaced navigation items, which looks like this: The currently
I have a script, which basically looks like this (the part I need help
Hi Have a menu which look like this <div id=products_menu class=span-9 last> <ul> <li><a
Suppose we have a menu which presents the user with some options: Welcome: 1)
I have an extensive menu in which I would like to search for and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.