CSS Repsonsive How do I make sub-menus a dropdown? - css

If you view twoguysplayingzelda.com on a mobile device, you will notice that my sub-menus are displayed. This makes the menu way too long. I would like to hide these and make them a dropdown (with a "+" symbol showing there is a dropdown). So you would click on games, then click on which game you wanted, and then see the options for each game. I just can't figure this one out. My CSS is below. Thanks for your help!
#media (max-width: 1000px) {
/* navigation */
.main-menu { display: none; }
.search-toggle { width: 24px; }
.nav-toggle {
display: block;
padding: 25px 0;
}
.nav-toggle .bar {
display: block;
width: 26px;
height: 3px;
margin-top: 5px;
background: #8E8E8E;
border-radius: 1px;
}
.nav-toggle .bar:first-child { margin-top: 0; }
.nav-toggle:hover { cursor: pointer; }
.nav-toggle.active .bar { background: #fff; }
.mobile-menu li { border-top: 1px solid rgba(255,255,255, 0.1); }
.mobile-menu > li:first-child { border-top: none; }
.mobile-menu a {
display: block;
padding: 25px 5%;
font-size: 0.9em;
text-transform: uppercase;
color: #999;
letter-spacing: 1px;
}
.mobile-menu a:hover { color: #fff; }
.mobile-menu ul a { padding-left: 10%; }
.mobile-menu ul ul a { padding-left: 15%; }
.mobile-menu ul ul ul a { padding-left: 20%; }
.mobile-menu ul ul ul ul a { padding-left: 25%; }
.mobile-menu ul ul ul ul ul a { padding-left: 30%; }
}

You could try jQuery mobile collapsible menus: http://demos.jquerymobile.com/1.4.0/collapsible/
I've also found a css-based solution here that might work for you: Pure CSS collapse/expand div.
Otherwise, if you want to go with the jquery mobile route, make sure the <head> tag in your html file looks something like this:
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1">
<link rel = "stylesheet" href = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src = "https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src = "https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
These urls in script and link will allow you to pull the jQuery mobile code automatically without you needing to download any files/code and add them to your project explicitly. Then, within your body tag, add a div to wrap around the entire page, like this:
<div data-role = "page">
...
</div>
Then, you can add collapsible lists to your page, within that data-role = "page" div, like so:
<div data-role="collapsible">
<h4>Heading</h4>
<ul data-role="listview">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
</div>
Great tutorials you can look at for jQuery mobile:
https://www.tutorialspoint.com/jquery_mobile/jquery_mobile_setup.htm
http://demos.jquerymobile.com/1.4.0/collapsible/#&ui-state=dialog

Related

Make menu items and submenu items display vertically without covering each other up

As the first step in making my menu responsive, I want to add a media query in css to change the way the menu displays so that each list item is displayed vertically below the previous item, with it's own submenu items displayed below it before the next list item is displayed. Hope that makes sense. Here are the HTML and CSS that make the menu work in the desktop version of the site:
HTML
<nav>
<img id="logo" src="#logoUrl">
<ul>
<li class="#(CurrentPage.Url == "/" ? "current" : null)">Home</li>
#foreach (var item in menuItems)
{
<li class="#(CurrentPage.Id == item.Id ? "current" : null)">
#item.Name
#if (item.Children.Where("Visible").Any())
{
var subMenuItems = item.Children.Where("Visible");
<ul>
#foreach (var sub in subMenuItems)
{
<li>#sub.Name</li>
}
</ul>
}
</li>
}
</ul>
<br class="clear">
</nav>
(This is on Umbraco, so forgive all the Razor bits)
CSS
#logo {
float: left;
margin-right: 25px;
}
nav {
width: 100%;
height: auto;
background-color: #354a49;
}
nav > ul > li {
display: block;
position: relative;
width: auto;
height: 50px;
float: left;
font-size: 1.1em;
margin: 0px 20px 0px 20px;
padding: 15px 8px 13px 8px;
text-align: center;
}
nav ul li a {
color: #fefce9;
margin-left: auto;
margin-right: auto;
}
nav ul li a:hover {
font-style: italic;
}
nav ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
z-index: 99;
width: 200px;
}
nav ul li:hover {
border-bottom: 2px solid #fefce9;
background-color: #a1b0af;
}
nav ul li:hover > ul {
display: block;
margin-top: 2px;
}
nav ul li ul li {
display: block;
float: none;
padding: 20px 3px;
background-color: #a1b0af;
border-bottom: 2px solid #fefce9;
}
nav ul li ul li a {
color: #fefce9;
}
nav li.current {
background-color: #a1b0af;
border-bottom: 2px solid #fefce9;
}
nav li.current > a {
color: #fefce9;
font-style: italic;
}
And here is the CSS I have in my media query at the moment:
#logo {
margin-right: -50px;
}
nav > ul > li {
float: none;
margin: 0px;
}
nav ul ul {
width: 100%;
}
nav li.current {
background-color: inherit;
}
That displays the main menu items one below the other OK, but when I try to change things so that the submenu items appear between the menu items I just end up with the submenu items appearing over the top of the menu items and each other.
EDIT
Here's the rendered HTML as requested:
</nav>
<img id="logo" src="/media/1042/wshalogo.png">
<ul>
<li class="current">Home</li>
<li>
About us
<ul>
<li>Our People</li>
<li>Who we were and are</li>
<li>Our Houses</li>
<li>Annual Reports</li>
</ul>
</li>
<li>
Being a Tenant
<ul>
<li>Asbestos</li>
<li>Being Safe & Secure</li>
</ul>
</li>
<li>
News
<ul>
<li>Community Garden</li>
<li>Football Team</li>
<li>Health Centre</li>
</ul>
</li>
</ul>
<br class="clear">
</nav>
Your second level ul is position: absolute; which means it's taken out of the normal document flow and won't take up space in relation to any other elements. Try changing absolute to relative. That should keep the items correctly positioned in the menu.
nav ul ul {
display: none;
position: absolute; /* <--- Try changing this to relative. */
top: 100%;
left: 0;
z-index: 99;
width: 200px;
}
Also, the fixed height on your top-level li doesn't let the element grow past 50px. Try setting that instead to a min-height:
nav > ul > li {
display: block;
position: relative;
width: auto;
height: 50px; /* <-- min-height: 50px */
float: left;
font-size: 1.1em;
margin: 0px 20px 0px 20px;
padding: 15px 8px 13px 8px;
text-align: center;
}
That worked in this fiddle but led to awkward jumping when the sub-menu was hovered and then un-hovered.
Also, consider your use-case - if you're doing this to support tablet/mobile devices the :hover state won't work the same way it doesn't when you're using a mouse. Users would have to know to press to the side of the "About Us" link text to see the dropdown, otherwise they'll get taken directly to the "About Us" page without seeing the :hover state. It might be necessary to either show all the items in a tree structure or use JavaScript to add additional functionality for the submenus.
Here's a decent solution to a responsive sub-menu without JavaScript, but it also doesn't use links for top-level menu items that have sub-items.

CSS How to Make Horizontal Menu Bar stretch across full page width

I am making a website for a friend and am struggling with the CSS for the menu bar. I've deisgned it as he asked etc., but am having a couple of problems.
1) I can't get it to stretch across the full width of the page. The menu itself should stretch across the whole width of the page, with the width of the page split into 6, and the text in each menu item to be centralised and stay the same size and just add extra black background to accommodate the page width. (Most users who'll be looking at it will be using 1920 x 1080 apparently)
Like so (Ignore the boxes at the top - they were just colour tests):
http://i58.tinypic.com/1z2zkf8.png
2) When I mouseover the menuitems for the submenu, the main menu readjusts itself. How can I stop it doing this, so it stays at a static width for the menu buttons? (i.e. 1/6th of the page width)
3) How can I make it so clicking the main menu will show the relevant submenu and keep it up until there is a click elsewhere on the page (i.e. so you don't have to hold your mouseover to select the submenu)
HTML:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Navigation</title>
<link href="navigation.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="navbar">
<ul>
<li>Home</li>
<li>Clan
<ul class="subnav">
<li>Really? You made a website for this?</li>
<li>Member Roster</li>
<li>One of us...</li>
</ul>
</li>
<li>Games
<ul class="subnav">
<li>Current Games Rotation</li>
<li>Games You Really Need To Buy</li>
<li>Clan Steam Account</li>
<li>Wargame Decks</li>
<li>Leon's Wheel o' Games</li>
</ul>
</li>
<li>Events
<ul class="subnav">
<li>Thursday Game Night</li>
<li>ILAN</li>
</ul>
</li>
<li>Donate
<ul class="subnav">
<li>Help Us Not Be Poor</li>
<li>Help Us Even If You're Poor</li>
</ul>
</li>
<li>Other Shit
<ul class="subnav">
<li>Links to Shit</li>
<li>Cheap as Shit Games</li>
<li>Stats 'n' Shit</li>
<li>Downloadable Shit</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
CSS:
#import url("AEnigma_Scrawl/stylesheet.css");
.navbar {
font-family: "AEnigma Scrawl";
font-size: 32px;
text-align: left;
height: 80px;
width: 100%;
font-weight: normal;
text-shadow: 1px 1px #000000;
}
.navbar ul {
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-left: 0px;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
list-style-type: none;
width: 100%;
}
.navbar ul li {
float: left;
}
.navbar ul li a {
text-decoration: none;
color: #FFFFFF;
padding-top: 5px;
padding-right: 5px;
padding-bottom: 5px;
padding-left: 5px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-left: 0px;
background-image: url(bg_navbar.png);
border-top: 1px solid #FFFFFF;
border-bottom: 1px solid #FFFFFF;
}
.navbar ul li a:hover , .navbar ul li a:active, .navbar ul li a:focus{
background-image: url(bg_navbar_hover.png);
}
.navbar ul li a.current {
background-image: url(bg_navbar_current.png);
}
.navbar ul li .subnav {
display: none;
}
.navbar ul li .subnav li {
float: none;
}
.navbar ul li .subnav li a {
font-size: 18px;
}
.navbar ul li:hover ul.subnav {
display: block;
position: static;
}
.navbar ul li:hover ul.subnav a {
display: block;
width: 100%;
}
The reason why your menubar isn't stretching across the page is probably because browsers usually automatically add a margin to the html/body. Try setting this css:
html,
body
{
margin:0;
padding:0;
}
Also the reason why your menu bar is shifting is because you're positioning the .subnav ul as static when it should be positioned as absolute. Like so:
.navbar ul li .subnav
{
position:absolute;
}
Positioning an element as absolute means it takes up no space on page so this means it won't push other elements away.
Try adding min-width: 100% on the navbar.
Also change the menu background from image to color.

Float:left prevents click on <a href> tag

I'm trying to get a website up and I'm using CSS to get it designed but I am unfamiliar with navbars and getting stuff side by side. I used a "float: left" for my image (logo) and well the text aligned but, it seems like I cannot fully click on the image anymore since it's also something that can be clicked using the tag. So, any ideas? My code:
CSS:
#navbar {
width: 100%;
height: 40px;
background-color: black;
opacity: 0.7;
display: fixed;
}
#navbar img {
opacity: 1;
float: left;
}
#navbar img:hover {
opacity: 0.7;
}
#navbar img a
{
margin: 0;
padding: 0;
}
#navbar ul
{
position: relative;
list-style-type:none;
margin:0;
padding: 8px 0 0 0;
color: white;
font-size: 1.5em;
}
#navbar ul a
{
display:block;
width:60px;
}
#navbar li
{
display:inline;
}
HTML:
<div id="navbar">
<img src="logo.png"/>
<ul> <li>Hello</li> <li>World</li>
</ul>
</div>
Hi Please see the below html and css
HTML:
<div id="navbar">
<ul>
<li><img src="logo.png"/></li>
<li>Hello World</li>
</ul>
</div>
CSS:
#navbar {
width: 100%;
height: 40px;
background-color: black;
opacity: 0.7;
}
#navbar img {
opacity: 1;
}
#navbar img:hover {
opacity: 0.7;
}
#navbar img a
{
margin: 0;
padding: 0;
}
#navbar ul li
{
list-style-type:none;
margin:0;
padding: 8px;
color: white;
font-size: 1.5em;
}
#navbar ul li
{
display:inline-block;
}
Please check the jsfiddle: http://jsfiddle.net/Luncqwn3/3/
I have cleaned up your html and css
I have placed the logo ,as well as the text in the ul li and given
display:inline-block which will make them appear next to each other
in a single line.
Please try using hexadecimal codes for color like #000 or #fff
instead of specifying white,black,green etc.
There is no such thing as display:fixed, please see w3schools
site for better understandong.
I removed float:left for the img tag , because i achieved
bringing the logo and text next to each pther using
display:inline-block for the ul li.

CSS Drop Down Menus won't display in proper area using IE (all versions)

I have a CSS drop down menu implemented into a website I am working on. It works flawlessly in any browser except for IE.
With IE, the drop downs will appear in a completely different area of the page, making it impossible to click on the links before the hover disappears. I included the code from my .css and .html (changed nav structure to basic for here) files below. This nav is placed into my site's layout via a PHP include, so the area my nav bar is displayed is centered and 230px from the top of the main page.
When I use the DOCTYPE strict mode it will work as expected, however I have other issues then which I can't change, so I'd rather not use a DOCTYPE at all (else I'll be forced to recode the ENTIRE site)
I'm sure there has to be some type of work around for this... even if it doesn't display exactly the same in IE, I just need it to be usable!
Any help is greatly appreciated!! I've spent weeks trying to sort this headache...
CSS FILE:
#catnav {
padding: 0;
clear: both;
height: 32px;
width: 980px;
background: #000;
position: absolute;
top: 230px;
z-index: 100;
}
#nav {
list-style: none;
margin: 0;
padding: 0;
position: relative;
z-index: 100;
}
#nav ul {
margin: 0;
padding: 0;
}
#nav li {
float: left;
margin: 0;
padding: 0;
list-style: none;
position: relative;
}
#nav a {
display: block;
line-height: 32px;
margin: 0 0px;
padding: 0;
font-size: 12px;
color: #fff;
font-weight: bold;
}
#nav li a:hover {
color: #fff;
text-decoration: underline;
display: block;
}
#nav li ul {
list-style: none;
position: absolute;
width: 0px;
left: -999em;
}
#nav li:hover ul, #nav li.sfhover ul {
left: auto;
}
#nav li li {
float: left;
margin: 0;
padding: 0;
width: 120px;
}
#nav li li a {
width: 129px;
height: 24px;
line-height: 24px;
color: #000;
border-bottom: 1px solid #000;
background: #c5c5c5;
margin: 0;
padding: 5px 20px 5px 15px;
}
#nav li li a:hover {
border-top: 1px solid #131f27;
background: #8f7c58;
padding: 5px 20px 5px 15px;
}
#nav li:hover, #nav li.sfhover { /* prevents IE7 drop-down menu bug (focus on a page element prevents nested menus from disappearing) */
position: static;
}
HTML FILE:
<script type="text/javascript"><!--//--><![CDATA[//><!--
sfHover = function() {
if (!document.getElementsByTagName) return false;
var sfEls = document.getElementById("nav").getElementsByTagName("li");
for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=" sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
//--><!]]></script>
<link rel="stylesheet" href="css.css">
<div id="catnav">
<div id="nav">
<ul>
<li>Title Link 1</li>
</ul>
<li>
<ul>
<li>Title Link2 </li>
</ul>
</li>
<li>Title Link 3
<ul>
<li>Sub Link 1</li>
</ul>
</li>
<li>Title 4
<ul>
<li>Sub Link 1</li>
</ul>
</li>
<li>Title Link 5</li>
<li>Title Link 6</li>
<li>Title Link 7</li>
</ul>
</div>
</div>
How can I make it so that IE will at least display these drops downs under their titles rather than 230px below and to the right of them?
Sounds like a box model problem, there is a great many threads and questions pertaining to IE's implementation of how elements are rendered on pages. I played with your snippets briefly and changed the following CSS section:
#nav li:hover ul, #nav li.sfhover ul {
left: auto;
_margin-top: -230px;
_margin-left: -72px;
}
There is still an issue with the sub-links not showing up directly under the parent link and I don't have further time to respond to your question but this should give you a starting point.
This is a similar problem from 2009 and there is a good discussion among the replies.

Dropdown menu, when resizing the browser

I am doing an horizontal dropdown menu. It looks like this :
[menu1][menu2][menu3][menu4]
But when I resize (less wide) my browser, the menu appears like :
[menu1][menu2]
[menu3][menu4]
I want it to remain in line all the time!
EDIT: my CSS file
/* General */
#cssdropdown, #cssdropdown ul {
list-style: none;
position: relative;
visibility: visible;
z-index: 1;
overflow: hidden;
}
#cssdropdown, #cssdropdown * { padding: 0; margin: 0; }
/* Head links */
#cssdropdown li.headlink {
width: 11.911em;
float: left;
margin-left: -1px;
border: 1px black solid;
background-color: #e9e9e9;
text-align: center;
}
#cssdropdown li.headlink a { display: block; padding: 10px; }
/* Child lists and links */
#cssdropdown li.headlink ul { display: none; border-top: 1px black solid; text-align: center; }
#cssdropdown li.headlink:hover ul { display: block; }
#cssdropdown li.headlink ul li a { padding: 5px; height: 17px;}
#cssdropdown li.headlink ul li a:hover { background-color: #FF9; }
/* Pretty styling */
body {
font-family: verdana, arial, sans-serif;
font-size: 0.7em;
position: static;
}
#cssdropdown a { color: black; font-weight: bold; font-size:10px } #cssdropdown ul li a:hover { text-decoration: none; }
#cssdropdown li.headlink { background-color: #FFF50A; }
#cssdropdown li.headlink ul { background-position: bottom; padding-bottom: 10px; }
/*headermenu*/
#headerMenu {
position: relative;
float: left;
color: #DDD;
z-index: 1;
height: 34px;
right: 10px;
width: auto;
}
<div align="left" class="thrColElsHdr" id="headerMenu">
<ul id="cssdropdown" name="cssdropdown">
<li class="headlink"> Ecole
<ul>
<li>Histoire</li>
<li>Philosophie</li>
<li>Méthode</li>
<li>Equipe</li>
<li>Qualité</li>
<li>Services</li>
<li>Emplois</li>
</ul>
</li>
<li class="headlink"> Cours
<ul>
<li>Individuel</li>
<li>Semi-privé</li>
<li>Mini-groupe</li>
<li>Intensif</li>
<li>Entreprises</li>
<li>A distance</li>
<li>Par téléphone</li>
<li>Coaching</li>
<li>Soutien scolaire</li>
<li>Diplômes officiels</li>
</ul>
</li>
<li class="headlink"> Inscription
<ul>
<li>Auto-évaluation</li>
<li>Conditions</li>
<li>Tarifs</li>
<li>Formulaires</li>
</ul>
</li>
<li class="headlink"> Contact
<ul>
<li>Ecole</li>
<li>Lien externe</li>
</ul>
</li>
</ul>
</div><br/>
You should set min-width on the element containing the menu.
you want to use the css
white-space:nowrap;
this should be applied to the parent of your menus
if you provide some of the actual html, I can be more specific
for example
<div class='menuContainer'>
<span>menu1</span>
<span>menu2</span>
<span>menu3</span>
<span>menu4</span>
</div>
and css like
.menuContainer {
white-space:nowrap;
}
see http://www.w3schools.com/css/pr_text_white-space.asp
Edit in response to op question modifications
I assume #cssdropdown is the id your container around all the menus. please let me know the html for this if it's not correct.
Anyways, in this case, you should add to your css
#cssdropdown {
white-space:nowrap;
}
One other note, I see the width of your mens is set to 11.911em. When I see that I can only assume that you set it to be exactly the right width for whatever font you have. keep in mind your users may have slightly different fonts and suddenly your pixel perfect sizing is meaningless. design with a little more flexibility in mind.
Sounds like your width property isn't being set in either the HTML or the CSS.
Can you provide some sample code?

Resources