when I set the "width" attribute of the .topMenu li, the .subMenu width is too narrow, even though I set it's position to absolute. It also is horizontal instead of vertical. Why?
the html
<div id="navigation">
<ul id="navBlock">
<li>HOME</li>
<li class="topMenu">
ABOUT US
<ul class="subMenu">
<li>WELCOME</li>
<li>HISTORY</li>
</ul>
</li>
</ul>
</div>
the css
#navBlock{
margin: 0;
padding: 0;
list-style-type: none;
}
#navBlock li{
display: inline-block;
border:1pt solid #ccc;
width:20%;
text-align:center;
background:#fff;
position:relative;
}
#navBlock li:hover{
cursor:pointer;
background:orange;
}
.topMenu:hover .subMenu{
display:block;
position:absolute;
}
.subMenu{
display:none;
margin: 0;
padding: 0;
}
.subMenu li{
border:1pt solid #aaa;
background:#fff;
width:200px;
display: inline;
position:absolute;
}
.subMenu li:hover{
float:none;
}
http://jsfiddle.net/L28Lf3se/
The problem is one of specificity. Your CSS for .subMenu li is being overridden by the #navMenu li because it thinks it's more specific. That whole block isn't being used. Re-label the .subMenu li to #navBlock li ul li and you'll be good to go.
Change the width in px than %
#navBlock li{
display: inline-block;
border:1pt solid #ccc;
width:150px;
text-align:center;
background:#fff;
position:relative;
}
Everything will work smooth
#navBlock li(0-1-0-1) more specific than .subMenu li(0-0-1-1) that's why the width you set for .subMenu li are get overrided.
You shouldn't touch your #navBlock li width.
Change #navBlock li to #navBlock>li and magic will happen.
This is best you can do to solve your problem.
Related
I'm putting up a site on Tumblr for my online store. Thing is, I can't get the dropdown menu to show up when I put "overflow:hidden" on the #topmenu part. When I set it to "overflow:visible", I get a huge chunk of white space to the right of the webpage. The white space isn't obvious when I'm using my laptop but it is when I use my phone.
I've tried changing the value of the z-index but it still won't work. I want to be able to have "overflow:visible" while not getting the white space at the right side of the page.
Please help me figure out what's wrong with the code and find a solution. I think there's something wrong with the width of the menu. But then when I change the width, all the links are pushed to the left side of the page.
Thank you in advance!
#topmenu {
font-family: 'Montserrat', sans-serif;
float:left;
width:100%;
background:transparent;
overflow:visible;
z-index:99999;
position:relative;
}
#topmenu ul {
clear:left;
float:left;
list-style:none;
margin:0;
padding:0;
position:relative;
left:50%;
text-align:center;
}
#topmenu ul li {
display:block;
float:left;
list-style:none;
margin:0;
padding:3pt;
position:relative;
right:50%;
}
#topmenu ul li a {
display:block;
margin:0 0 0 1px;
padding:4px 11px;
background: transparent;
color: #222222;
font-size:10px;
text-decoration:none;
line-height:2em;
letter-spacing: 3px;
}
#topmenu ul li a:hover {
background: none;
color: #b492a8;
}
#topmenu ul li ul.dropdown{
min-width: 125px;
max-width: 125px;
background: #ffffff;
display: none;
position: absolute;
z-index: 999;
left: 0;
}
#topmenu ul li:hover ul.dropdown{
display: block; /* Display the dropdown */
padding:0px 0px 0px 50px;
}
#topmenu ul li ul.dropdown li{
display: block;
}
<div id="topmenu">
<ul>
<li>HOME</li>
<li>SHOP</li>
<li>FAQ</li>
<li>ORDER FORM</li>
<li>CUSTOMERS
<ul class="dropdown">
<li>OOTD</li>
<li>Feedbacks</li>
</ul>
</li>
<li>ABOUT</li>
</ul>
</div>
EDIT:
I tried using #gopalraju's code and it eliminated the white space while showing the dropdown menu. (Thank you gopalraju!)
How do I push the whole menu to the center of the page?
And how do I put the dropdown menu right under the word "Customers"?
#topmenu {
font-family: 'Montserrat', sans-serif;
float:left;
width:100%;
background:transparent;
overflow:visible;
z-index:99999;
position:relative;
}
#topmenu ul {
clear:left;
list-style:none;
margin:0;
padding:0;
position:relative;
display:table;
margin:0 auto;
text-align:center;
}
#topmenu ul li {
display:block;
float:left;
list-style:none;
margin:0;
padding:3pt;
position:relative;
}
#topmenu ul li a {
display:block;
margin:0 0 0 1px;
padding:4px 11px;
background: transparent;
color: #222222;
font-size:10px;
text-decoration:none;
line-height:2em;
letter-spacing: 3px;
}
#topmenu ul li a:hover {
background: none;
color: #b492a8;
}
#topmenu ul li ul.dropdown{
min-width: 125px;
max-width: 125px;
background: #ffffff;
display: none;
position: absolute;
z-index: 999;
left: 0;
}
#topmenu ul li:hover ul.dropdown{
display: block; /* Display the dropdown */
}
#topmenu ul li ul.dropdown li{
display: block;
}
<div id="topmenu">
<ul>
<li>HOME</li>
<li>SHOP</li>
<li>FAQ</li>
<li>ORDER FORM</li>
<li>CUSTOMERS
<ul class="dropdown">
<li>OOTD</li>
<li>Feedbacks</li>
</ul>
</li>
<li>ABOUT</li>
</ul>
</div>
I want to build a simple CSS dropdown menu. It works if I use absolute positioning, but starts to push down the content if I use relative positioning. What is the reason for this? Using absolute positioning for the DIVs menu and content is not an option for me unfortunately.
HTML:
<div id="menu">
<ul>
<li class="topmenu">
Top 1
<ul>
<li class="submenu">Sub 1</li>
<li class="submenu">Sub 2</li>
</ul>
</li>
</ul>
</div>
<div id="content">
Test
</div>
CSS:
#menu {
position:relative;
}
#menu ul {
list-style-type:none;
list-style-image:none;
margin:0px;
padding:0px;
}
#menu li.topmenu {
float:left;
}
.topmenu a {
float:left;
width:110px;
}
.topmenu ul{
display:none;
}
.submenu a{
width:110px;
clear:both;
}
.topmenu:hover ul {
display:block;
z-index:10;
}
#content {
display: block;
clear: both;
z-index: 1;
}
I've simplified the code, removed the horrible floats and substituted with display: inline-block and fixed the positioning issue.
(Demo)
#menu ul {
list-style:none;
margin:0px;
padding:0px;
font-size: 0px;
}
#menu li {
font-size: 1rem;
}
#menu > ul > li {
display: inline-block;
}
#menu ul ul {
display: none;
}
#menu ul li:hover ul {
display: block;
position: absolute;
}
#menu a {
display: inline-block;
width:110px;
}
How can it be that there are so many answers on this topic and I still can't figure this out? I've been fiddling with the CSS on jsfiddle for hours and I still don't understand why my navigation bar won't center without going into a vertical list.
The html:
<div class='nav'>
<ul class='menu' id='menu'>
<li><a expr:href='data:blog.homepageUrl'>home</a></li>
<li><a class='drop-ctg' href='a'>MAKEUP</a>
<ul>
<li><a href='a'>EYES</a></li>
<li><a href='a'>LIPS</a></li>
<li><a href='a'>FACE</a></li>
</ul>
</li>
<li><a href='a'>SKINCARE</a></li>
<li><a href='a'>LIFESTYLE</a></li>
<li><a href='a'>DIY</a></li>
<li><a href='a'>CONTACT</a></li>
</ul>
</div>
and the CSS, I think:
*{
margin:0;
padding:0;
outline:0;
}
.nav {
width:950px;
height:auto;
border-bottom:1px solid #eee;
margin:10px auto 5px;
display:inline-block;
}
.menu {
width:auto;
list-style:none;
font:$pagenavifont;
text-align:center;
margin:0 auto;
}
.menu a {
float:left;
color:#999;
text-decoration:none;
text-transform:uppercase;
width:auto;
line-height:36px;
padding:0 20px;
}
.menu a:hover,li.menuhover a{
color:#111;
}
.menu li {
position:relative;
float:left;
width:auto;
}
.menu li:last-child {
background:none;
}
.menu ul{
display:none;
position:absolute;
top:36px;
left:0;
background:#fbfbfb;
display:none;
list-style:none;
}
.menu ul li{
float:none;
border-top:1px solid #e3e3e3;
border-right:1px solid #e3e3e3;
border-left:1px solid #e3e3e3;
width:auto;
background:none;
}
.menu ul li:last-child {
border-bottom:1px solid #e3e3e3
}
.menu ul li a{
float:none;
display:block;
background:none;
line-height:36px;
min-width:137px;
width:auto;
text-align:left;
padding-left:10px;
color:#444;
}
.menu ul li a:hover{
background:#fdfdfd;
color:#777;
}
I just started my blog today, and so far I've learned that getting rid of floats and putting inline-block might help, but there are so many that I really don't get which code applies to what. Any help is appreciated!
Here's the fiddle link: http://jsfiddle.net/vFDrV/9/
Here's the link to my blog: http://theprettyfoxes.blogspot.com/
if I understand correctly your question, its quite simple.
Add the follow code to your menu css class.
.menu { /* applying to a ul element */
/* ... your code ... */
display: inline-block;
}
You can read more about this at the Mozilla Docs
https://developer.mozilla.org/en-US/docs/Web/CSS/display
What it's going on when we add "inline-block" is this:
The element generates a block element box that will be flowed with
surrounding content as if it were a single inline box (behaving much
like a replaced element would)
Thats all!
remove float from following:
.menu a {
/*float: left;*/
color: #999;
text-decoration: none;
text-transform: uppercase;
width: auto;
line-height: 36px;
padding: 0px 20px;
}
.menu li {
position: relative;
/*float: left;*/
width: auto;
display: inline; /* <- add this */
}
need some help. I'm just learning HTML and have been looking at how to do menus today, whilst doing so I have ran into a problem.
I can't seem to figure out how to center the menu on screen.
This is what I have so far;
<div id="navMenu">
<ul>
<li>Home</li>
<li>WWW.
<ul>
<li>Through the years</li>
</ul></li>
<li>Browsers
<ul>
<li>IE</li>
<li>Firefox</li>
<li>Chrome</li>
</ul></li>
<li>CSS
<ul>
<li>CSS 2.1</li>
<li>CSS 3</li>
</ul></li>
<li>Scripting
<ul>
<li>JavaScripts</li>
<li>jQuery</li>
</ul></li>
</ul>
</div>
Current CSS;
#navMenu{
margin:0;
padding:0;
}
#navMenu ul{
margin:0;
padding:0;
line-height:30px;
}
#navMenu li{
margin:0;
padding:0;
list-style:none;
float: left;
position: relative;
background-color: #999;
border-radius: 5px;
}
#navMenu ul li a{
text-align: center;
text-decoration:none;
height:30px;
width:150px;
display:block;
color: #FFF;
border: 1px solid #FFF;
text-shadow: 1px 1px 1px #000;
}
#navMenu ul ul{
position:absolute;
visibility:hidden;
top:32px;
}
#navMenu ul li:hover ul{
visibility:visible;
}
#navMenu li:hover{
background-color: #09F;
}
#navMenu ul li:hover ul li a:hover{
background-color: #CCC;
color: #000;
}
#navMenu a:hover{
color:#000;
}
The above produces this .... http://gyazo.com/55cf62d121ef16eb1248e5246d0accae
Anyway to get the menu bar in the center of the screen.
SIDE NOTE:: Any spoilers tags on this site?
Try using display: inline-block and text-align: center.
Set display: inline-block for the <ul> and text-align: center for the container #navMenu. Set text-align: left for #navMenu li to fix the submenus.
Also
Example
#navMenu{
margin:0;
padding:0;
text-align: center;
}
#navMenu ul{
margin:0;
padding:0;
line-height:30px;
display: inline-block;
}
#navMenu li{
margin:0;
padding:0;
text-align: left;
float: left;
list-style:none;
position: relative;
background-color: #999;
border-radius: 5px;
}
Add a width to your menu wrapper and give the left and right an automatic margin.
You can adjust the width depending on where in the center you want to have your nav positioned.
#navMenu {
width: 960px;
margin:0 auto;
padding:0;
text-align: center;
}
The margin and paddings your have in the ul and li are not needed.
#navMenu ul{
list-style: none;
line-height:30px;
display: inline-block;
text-align: center;
}
#navMenu li{
display: inline;
}
Padding should go here to keep all your links even.
#navMenu li a {
display: block;
padding: 10px;
}
I am pretty new on css and I want to learn use css effectively but I have now couple of problems/questions, I want to make grey border under my menu but when I zoom in and out border behave so strangely? Sorry my code is not very clean. And how can I retouch my code for better standing?
HTML
<html>
<head>
<link href="Style.css" rel="stylesheet" type="text/css">
<style type="text/css">
span{
color:#d00000;
text-decoration:underline;
}
#home{
background-color:#0000CC;
}
span{
font-style:italic;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="header">
<ul>
<li><span>Home</span></li>
<li>link2</li>
<li>link3
<ul>
<li>sub</li>
<li>sub2</li>
</ul>
</li><!---End of DropDown Videos menu--->
<li>link4</li>
<li>link5</li>
</ul><!---End of nav elements--->
<br class="clearFloat">
</div><!---End of header--->
</div><!---End of wrapper--->
<div id="grey">
</body>
</html>
CSS
#wrapper{
float:center;
}
#grey{
border-bottom:250px solid #a0a0a0;
padding:0px;
margin-left:203px;
margin-right:387px;
}
#header{
margin:auto;
padding:0px;
clear:both;
}
#header ul{
margin:0px;
padding:0px;
line-height:30px;
}
#header li{
margin:0px;
padding:0px;
list-style: none;
float:left;
position:relative;
background-color:#000099;
border:1 solid #0000CC;
top:px;
right:-15%;
}
#header ul li a{
text-align:center;
font-family:fantasy;
text-decoration:none;
display:block;
height:30px;
width:150px;
color:black;
}
#header ul ul li a{
color:#0000cc;
}
#header ul ul li{
right:0%;
}
#header ul ul{
margin:0px;
padding:0px;
position:absolute;
opacity: 0;
visibility: hidden;
transition: all .1s ease-in;
}
#header ul li:hover ul {
visibility:visible;
opacity:1 ;
}
#header ul li:hover {
background-color:blue;
}
#header ul li:hover a {
color:black;
}
#header ul ul li:hover a {
color:#d00000;
}
.clearfloat{
clear:both;
margin:0px;
padding:0px;
}
Without completely reworking your code, just think of added a bottom border per 'li'...
Remove your id='grey' and add your border-bottom to your #header li {}. Refresh but note how your 'sub' menus then each have a bottom border now. They inherited it, so you then need to turn those off...hence add border-bottom: none to the #header ul ul li declaration.
Now...if we did a quick rework of your code...
HTML
<div id="wrapper">
<div id="header">
<div id="navwrapper">
<ul id="nav">
<li><span>Home</span></li>
<li>link2</li>
<li>link3
<ul class="sub">
<li>sub</li>
<li>sub2</li>
</ul>
</li>
<li>link4</li>
<li>link5</li>
</ul>
</div>
</div>
CSS
html, body { margin:10px;padding:0; }
#wrapper, #header, #navwrapper { margin: 0 auto 0 auto; }
#nav {
padding:0;
margin: 0 auto 0 auto;
line-height:30px;
height:30px;
list-style:none }
#nav li {
border-bottom: solid #a0a0a0 10px;
margin:0;
padding:0;
float:left;
background-color:#000099;
}
#nav a {
text-align:center;
font-family:fantasy;
text-decoration:none;
display:block;
height:30px;
width:150px;
color:black;
}
#nav li:hover { background-color:blue; }
#nav li:hover .sub {
visibility:visible;
opacity: 1;
}
#nav .sub a { color:#00c; }
#nav .sub {
margin:0;
padding:0;
position:absolute;
opacity: 0;
visibility: hidden;
transition: all .1s ease-in;
list-style: none;
}
#nav .sub li { clear:both; border-bottom: none; }
Using more naming conventions with your sub menus is safer than just drilling down through the css. You also need to note that any top-level CSS declaration is inherited by all the children within. Such that if you declare #nav li as being black but want all the 'sub' list elements to be blue, that needs to be stated explicitly...AFTER the initial declaration in the CSS document.
Hope this helped...