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?
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:
Then you’ll need to select the Tools, Preferences and Edit and set their
displaytonone: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:
I am using an
embased media query and not apxbased one for two reasons:pxbased 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:
And also changed the CSS a bit:
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:
EDIT#4: Now tried the
:targetmethod – 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.