I am creating a menu with hover-over submenus (just practicing my css) and I've come across a situation I don't understand. I'd like for the submenus to be positioned just below their parent menu item, and have no line breaks within them. The first two submenus behave just as I'd like, but as I move to the right two menu items, their submenus start to get line-breaks, because their width is constrained on the right side by the width of their parent's parent (div.menu-bar). How do I make it so that the submenu's widths are not constrained like this?
Here is a screencast to explain the situation: http://screencast.com/t/0fMjpA0MD
body {background: #77c4d3; padding:1%; }
div.menu-bar{position: relative; width: 700px;}
/*Styles for both menu and submenus: */
div.menu-bar ul { list-style-type: none; margin: 0; padding:20px; background: gray;}
div.menu-bar li { background:white; text-align:center; display:inline-block; padding:10px;}
/*Menu-specific styles: */
div.menu-bar > ul {width: 100%; text-align: center;}
div.menu-bar > ul > li {width:25%; border:0; margin: 0; padding-top: 10px; padding-bottom: 10px; box-sizing: border-box;}
/* Submenu-specific styles */
div.menu-bar ul ul {background-color: blue; padding-left: 10px; padding-right: 10px;}
/*Hide any unodered lists that are descendents of a list element by default*/
div.menu-bar li ul {
display: none;
}
/*Select any unordered lists that are children of list elements that are being hovered on.*/
/* The <ul> is display:block, but the individual <li> elements are inline-block, which is what matters.*/
div.menu-bar li:hover > ul {
display: block;
position: absolute;
}
<div class="menu-bar">
<ul>
<li>
Home
<ul>
<li>Beach House</li>
<li>Ski Cabin</li>
<li>Country Cottage</li>
</ul>
</li
><li>
News
<ul>
<li>World News</li>
<li>National News</li>
<li>Local News</li>
</ul>
</li
><li>
Contact
<ul>
<li>Email Address</li>
<li>Phone Number</li>
<li>Postal Address</li>
</ul>
</li
><li>
About
<ul>
<li>About Me</li>
<li>About You</li>
<li>About Us</li>
</ul>
</li>
</ul>
</div>
#Thiyagesh's code is correct, but his explanation doesn't quite make sense to me. In my solution (which is essentially the same as Thiyagesh's), my relative/absolute positioning relationships are the same. The only difference is that I needed to add a width to the submenus. Apparently when you don't specify a width of an absolute-positioned element, its width is defaulted to stay inside the bounds of its first relative-positioned ancestor?
In any case, the solution was just to add "width:375px;" to "div.menu-bar li:hover > ul".
Each inner ul should be absolute to the respective li. Hence the li should be positioned relative for the inner ul.
body {
background: #77c4d3; padding:1%;
}
div.menu-bar{
position: relative; width: 700px;
}
div.menu-bar ul {
list-style-type: none;
margin: 0;
padding:20px;
background: gray;
}
div.menu-bar li {
background:white;
text-align:center;
display:inline-block;
padding:10px;
}
div.menu-bar > ul {
width: 100%;
text-align: center;
}
div.menu-bar > ul > li {
width:24%;
border:0;
margin: 0;
padding-top: 10px;
padding-bottom: 10px;
box-sizing: border-box;
}
div.menu-bar ul ul {
background-color: blue;
padding-left: 10px;
padding-right: 10px;
}
div.menu-bar li ul {
display: none;
width: 375px;
position: relative;
}
div.menu-bar li:hover > ul {
display: block;
position: absolute;
}
<div class="menu-bar">
<ul>
<li>
Home
<ul>
<li>Beach House</li>
<li>Ski Cabin</li>
<li>Country Cottage</li>
</ul>
</li>
<li>
News
<ul>
<li>World News</li>
<li>National News</li>
<li>Local News</li>
</ul>
</li>
<li>
Contact
<ul>
<li>Email Address</li>
<li>Phone Number</li>
<li>Postal Address</li>
</ul>
</li>
<li>
About
<ul>
<li>About Me</li>
<li>About You</li>
<li>About Us</li>
</ul>
</li>
</ul>
</div>
Related
I am having trouble stacking up the elements inside the nav bar next to each other.
Here's how it looks:
I want the nav elements to be side by side of each other.
Here's HTML code
HTML:
<div id="navbar">
<ul>
<li>COMPANY
<ul>
<li>OVERVIEW</li>
<li>CEO MESSAGE</li>
<li>LEADERSHIP</li>
<li>INVESTORS</li>
<li>AFFILIATION</li>
<li>CONTACT</li>
</ul>
</li>
<li>PRODUCTS
<ul>
<li>OVERVIEW</li>
<li>PRODUCTS</li>
<li>APPLICATION</li>
<li>USAGE</li>
</ul>
</li>
<li>PARTNER PROGRAM
<ul>
<li>PARTNER PROGRAM</li>
<li>BECOME A PARTNER</li>
</ul>
</li>
<li>LEARN ABOUT WGIG
<ul>
<li>OVERVIEW</li>
<li>VIDEOS</li>
<li>INDUSTRY ARTICLES</li>
<li>WHITE PAPERS</li>
</ul>
</li>
<li>MEDIA CENTER
<ul>
<li>LATEST NEWS</li>
<li>PRESS RELEASES</li>
<li>MEDIA COVERAGE</li>
<li>EVENTS</li>
<li>AFFILIATION</li>
<li>MEDIA KIT</li>
</ul>
</li>
</ul>
</div>
Here's the CSS
CSS:
#navbar {
position: relative;
margin-left: 25%;
border: 5px solid green ;
width: 55%;
height: 100%;
}
#navbar ul {
display: inline-block;
border-radius: 10px;
position: absolute;
}
#navbar li {
list-style: none;
position: relative;
display: inline-block;
float: left;
}
#navbar ul li a {
text-decoration: none;
text-align: center;
color:lime;
height:30px;
width:40px;
display: block;
}
#navbar ul ul {
display:none;
position:absolute;
}
#navbar ul li:hover ul {
display:block;
}
The first <ul> should be relatively positioned. Setting your <a> at a fixed 40px wide isn't such a great idea (nor is it necessary) as most of the text within is more than 40px. Also: your menu items will wrap (stack) unless you set a min-width on their container.
See here: http://jsfiddle.net/RKRWQ/1/
I am having trouble getting my drop down navigation menu to work. The main issue is when I zoom out in Google Chrome, the menu starts wrapping and dropping to a 2nd line, causing all other divs on the page to wrap too.
I have searched across the internet to find a solution but nothing seems to work. Thanks for your advice.
Html:
<div id="nav">
<ul>
<li>Home</li>
<li>About us</li>
<li>Quality</li>
<li>Processes</li>
<li>Markets</li>
</ul>
<ul>
<li>Products
<ul>
<li>Product information</li>
<li>Product number one submenu</li>
<li>Product number two submenu</li>
<li>Product 3</li>
<li>Product 4</li>
<li>Product num 5</li>
<li>Products 6, more & products</li>
<li>Prod numb Seven</li>
<li>More products eight</li>
<li>More prods, prods and products</li>
<li>prod Stock List</li>
</ul>
</li>
</ul>
<ul>
<li>News
<ul>
<li>Latest news</li>
<li>Case Studies</li>
<li>Testimonials</li>
</ul>
</li>
</ul>
<ul>
<li>Contact
<ul>
<li>Contact us</li>
<li>Directions</li>
</ul>
</li>
</ul>
</div>
CSS:
#nav {
background-color:#14c64a;
height:37px;
width:960px;
}
#nav ul {
font-family: Arial, Verdana;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
white-space:nowrap;
}
#nav ul li {
display: block;
position: relative;
float: left;
}
#nav li ul {
display: none;
}
#nav ul li a {
display: block;
text-decoration: none;
color: #ffffff;
padding: 5px 15px 5px 15px;
background: #14c64a;
margin-left: 1px;
white-space: nowrap;
line-height:27px;
}
#nav ul li a:hover {
background: #666;
}
#nav li:hover ul {
display: block;
position: absolute;
}
#nav li:hover li {
float: none;
font-size: 11px;
}
#nav li:hover a { background: #14c64a; }
#nav li:hover li a:hover {
background: #1e7c9a;
}
Chrome doesn't want to shrink its font size below the minimum font size set by the user, even when zooming. That causes the texts to be wider than the 960px container at some point while zooming out.
(You can test this by making the minimum font size equal to the default font size in the settings. Then Chrome won't shrink the text at all when zooming out, not even a little.)
By the way, here is a fiddle of the code in the question, so people can test instantly.
I want to make my navigation bar with dropdown, but there is always distance on left side of the div dropdown. How do I remove that distance?
Here is the code of the navigation bar:
.navbar {
background-color: #6b6b6b;
margin:10px;
}
.navbar ul {
display: inline;
color: white;
font-family: "Agency FB";
font-weight: bold;
text-transform: uppercase;
list-style-type: none;
font-size: 24px;
}
.navbar ul li {
display: inline-block;
padding-right: 10px;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
}
.navbar ul li:hover {
background-color: #cccccc;
}
.navbar ul li ul {
display: none;
position: absolute;
margin-left:-10px;
margin-top:5px;
background-color:white;
color:#6b6b6b;
padding-left:-20px;
font-size:20px;
}
.navbar ul li ul li {
display: block;
}
.navbar ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
And here is the code of html navigation bar (without link):
<div class="navbar">
<ul>
<li>
Home
</li>
<li>
Category
<ul>
<li>Batik</li>
<li>Party</li>
<li>Office</li>
<li>Casual</li>
<li>Sport</li>
</ul>
</li>
<li>
Information
<ul>
<li>
About Us
</li>
<li>
Cara Belanja
</li>
<li>
Our Location
</li>
</ul>
</li>
<li>
Contact Us
</li>
</ul>
</div>
Thanks before :)
I remade it for you kinda, I'm sure you'll get the jist of it
<div id="nav">
<ul>
<li>Home</li>
<li>Category
<ul>
<li>Blah</li>
<li>Blah</li>
<li>Blah</li>
</ul>
</li>
<li>Information
<ul>
<li>Blah</li>
<li>Blah</li>
<li>Blah</li>
</ul>
</li>
<li>Contact Us</li>
</ul>
</div>
http://jsfiddle.net/un64F/3/ for css
I'm not sure I understand what you are asking for exactly but does adding padding-left: 0; to the .navbar ul style have the effect you want?
I have a drop down nav menu that I need to be centered within a div but text-align: center isn't working for me.
The site is at http://www.joekellywebdesign.com/churchsample1/index.html
HTML
<div id="navmenudiv">
<ul id="navmenu">
<li>Home</li>
<li>
About Us
<ul class="sub1">
<li>Introduction</li>
<li>Who We Are</li>
<li>Staff</li>
</ul>
</li>
<li>
Services
<ul class="sub1">
<li>Sunday Morning</li>
<li>Sunday Evening</li>
<li>Wednesday Evening</li>
</ul>
</li>
<li>Resources</li>
<li>Contact Us</li>
<li>News and Events</li>
</ul>
</div>
CSS
navmenudiv {
z-index:60;
margin: -30px 0;
height:50px;
background-color:#5340BF;
top:40;position:
relative;
text-align:center;
}
Here is an easy solution:
ul#navmenu li {
float:left; /* REMOVE */
display: inline-block; /* ADD */
}
Currently, the li are floating. By changing them to inline-block elements, they will center - given that you are already using text-align:center on the parent.
Updated CSS:
ul#navmenu li {
margin-right: 4px;
text-align: center;
width: 125px;
display: inline-block;
position: relative;
}
ul#navmenu li {
width: 125px;
text-align: center;
position: relative;
margin-right: 4px;
display: inline-block;
margin: 0 10px;
}
I've been able to create a horizontal menu using (display:inline) and I now have a drop menu thanks to sousMenu. My problem is that all the submenus, regardless of element I hovered over, appear in the same place. How do I work around this?
My menu code thus far:
<ul>
<li>Home</li>
<li class="sousMenu">About Us
<ul>
<li>Board of Directors</li>
</br>
<li>Student Profiles</li>
</br>
<li>Projects</li>
</ul>
</li>
<li class="sousMenu">Get Involved
<ul>
<li>Donations</li>
</br>
<li>Job Board</li>
</br>
<li>Join</li>
</ul>
</li>
<li class="sousMenu">Resources
<ul>
<li>Connections</li>
</br>
<li>Gallery</li>
</br>
<li>Tours</li>
</ul>
</li>
CSS:
#navcontainer ul {
/*margin: 0;*/
margin-left: auto;
margin-right: auto;
padding: 0;
top:180;
right:20;
width:800px;
list-style-type: none;
text-align: center;
position: absolute;
color: #fff;
background-color: #003300;
padding: .2em 1em;
}
#navcontainer ul li {
display: inline;
padding-left:2cm;
}
#navcontainer ul li a {
text-decoration: none;
color: #fff;
background-color: #030;
}
#navcontainer ul li a:hover {
color: #fff;
background-color: #000;
}
.sousMenu:hover ul {
display: block;
}
.sousMenu ul {
text-align: center;
display: none;
list-style-type: none;
}
Try setting the parent list item to position: relative and the child ul to position: absolute for starters. I made some other slight modifications to your code to achieve the desired effect.
Here's the CSS:
* {
margin: 0;
padding: 0;
vertical-align: baseline;
}
li {
list-style-type: none;
}
ul.main li {
position: relative;
display: inline-block;
}
.main li:hover > ul {
display: block;
}
ul.sub {
position: absolute;
display: none;
top: 100%;
left: 0;
}
ul.sub li {
display: block;
}
I also cleaned up the HTML a bit. You were missing a closing </ul> tag as well:
<ul class="main">
<li>Home</li>
<li class="sousMenu">About Us
<ul class="sub about">
<li>Board of Directors</li>
<li>Student Profiles</li>
<li>Projects</li>
</ul>
</li>
<li class="sousMenu">Get Involved
<ul class="sub get-involved">
<li>Donations</li>
<li>Job Board</li>
<li>Join</li>
</ul>
</li>
<li class="sousMenu">Resources
<ul class="sub resources">
<li>Connections</li>
<li>Gallery</li>
<li>Tours</li>
</ul>
</li>
</ul>
Here's the fiddle: http://jsfiddle.net/vXhZb/2/