I developed a submenu with CSS & jQuery and my intention was that it was vertical submenu but I´m getting a horizontal menu.
I already tried to "play" with display in CSS but its not working, like #menu ul li ul li a {display: inline-block;}.
Anyone there knows the trick to put this submenu vertical?
My jsfiddle with full example:
http://jsfiddle.net/jcak/9EcnL/7/
My html:
<section id="menu-container">
<nav id="menu">
<ul>
<li>Home
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</li>
<li>Procuts
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</li>
<li>About</li>
<li>Contacts</li>
</ul>
</nav>
</section>
My css:
#menu ul li ul li {display: none;}
#menu-container
{
background:green;
width:100%;
height:46px;
float:left;
z-index:7;
}
#menu
{
width:960px;
height:auto;
margin:0 auto 0 auto;
}
#menu ul
{
list-style-type:none;
}
#menu ul li
{
display: inline-block;
float:left;
height:46px;
position: relative;
line-height:46px;
font-weight:300;
}
#menu ul li a
{
text-decoration:none;
color:#ccc;
display:block;
margin-right:5px;
height:46px;
line-height:46px;
padding:0 5px 0 5px;
font-size:20px;
}
#menu ul li a:hover
{
color:#fff;
}
#menu ul li ul {
position: absolute;
left: 0;
-webkit-padding-start: 0;
width: 300px;
}
#menu ul li ul li
{
color:#fff;
}
By default, a ul will be vertical.
The float:left is what is making this whole menu be horizontal instead.
Have a go at removing that and seeing how the menu behaves.
Working Fiddle
#menu ul li
{
display: inline-block;
height:46px;
position: relative;
line-height:46px;
font-weight:300;
}
Related
What do I need to include in my CSS to get my menu text to stay black when hovering over a submenu?
I've tried systematically going through my current :hover elements, including > when ever possible, and adapting the relevant parts from other codes into the CSS. I've also combed through similar questions in hopes to find applicable code but have not been successful.
#menu {
width: 100%;
position: fixed;
background: #333;
color: #fff;
}
#menu ul.left {
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul a {
display:block;
color:#fff;
text-decoration:none;
padding:0 15px;
}
#menu ul a:hover {
display:block;
color: #333;
text-decoration:none;
padding:0 15px;
}
#menu ul li {
list-style-type: none;
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul li:hover {
background:#f6f6f6;
color:#333;
}
#menu ul ul {
display:none;
position:absolute;
background: #f6f6f6;
color: #333;
}
#menu ul li:hover > ul {
display:block;
}
<nav id="menu">
<ul class="left">
<li>Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</ul>
</li>
</ul>
</nav>
Expected results: hovering from Menu 1 to Sub Menu x will keep the #333 colored text on Menu 1.
Actual results: hovering from Menu 1 to Sub Menu x will revert to #f6f6f6 colored text on Menu 1.
Added a element to the #menu ul li:hover in css.
#menu {
width: 100%;
position: fixed;
background: #333;
color: #fff;
}
#menu ul.left {
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul a {
display:block;
color:#fff;
text-decoration:none;
padding:0 15px;
}
#menu ul a:hover {
display:block;
color: #333;
text-decoration:none;
padding:0 15px;
}
#menu ul li {
list-style-type: none;
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul li:hover a{
background:#f6f6f6;
color:#333;
}
#menu ul ul {
display:none;
position:absolute;
background: #f6f6f6;
color: #333;
}
#menu ul li:hover > ul {
display:block;
}
<nav id="menu">
<ul class="left">
<li>Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</ul>
</li>
</ul>
</nav>
apologies for basic-ness of question, just creating a dropdown menu and having some problems with the margins and positioning.
Ive set the div of it as 85% but i want that centered in the page,
And then the list items centered in the middle of that so it's all neat but can't get it too.
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix">
<li>Home</li>
<li>Second Page</li>
<li>Third Page<span class="arrow">▼</span>
<ul class="sub-menu">
<li>Dropdown One</li>
<li>Dropdown Two</li>
</ul>
</li>
<li>Fourth Page</li>
<li>Fifth Page<span class="arrow">▼</span>
<ul class="sub-menu">
<li>Dropdown 1</li>
<li>Dropdown Item 2</li>
<li>Dropdown 3</li>
<li>Dropdown 4</li>
</ul>
</li>
</ul>
</nav>
CSS:
.clearfix:after {
display:block;
clear:both;
}
.menu-wrap ul {
margin:0px;
padding:0px;
}
.menu-wrap {
width:80%;
background: #85D2CD;
display: inline-block;
margin: auto;
}
.menu {
width:100%;
margin:0px auto;
}
.menu li {
margin:0px;
list-style:none;
}
.menu a {
transition:all linear 0.15s;
color:#FFF;
}
.menu > ul > li {
float:left;
display:inline-block;
position:relative;
font-size:19px;
}
.menu > ul > li > a {
padding:10px 30px;
display:inline-block;
text-shadow:0px 1px 0px rgba(0,0,0,0.4);
text-decoration:none;
}
.menu > ul > li:hover > a { /* Hover background color */
background:#5D9390;
}
.menu li:hover .sub-menu {
opacity:1;
}
.sub-menu {
width: 100%;
padding:5px 0px;
position:absolute; /* Need this so menu height doesnt extend to dropdown size*/
top:100%;
left:0px;
z-index:-1;
opacity:0;
transition:opacity linear 0.15s;
background:#6D9E9B;
}
.sub-menu li {
display:block;
font-size:16px;
}
.sub-menu li a {
padding:10px 30px;
display:block;
text-decoration:none;
}
.sub-menu li a:hover {
background:#5D9390;
}
I think that is the only relevant code needed, I've kinda lost my mind a bit and put margin auto in too many spots to try fix this but none of it is working
First of all change the
.menu-wrap {
width: 80%;
background: #85D2CD;
display: block;
margin: 0 auto;
overflow: hidden;
}
And to center your menu items, change the ul style to
ul.sub-menu {
text-align: center;
}
That should get the things sorted out and be aligned in middle.
If you know the width of the wrapping div, you can do this:
.menu-wrap{
left: 10%;
position: relative;
}
If you get rid of the float:left, you can add this to the menu, to center the menu:
.menu {
text-align: center;
}
JsFiddle: http://jsfiddle.net/d8sstx6t/2/
If you don't know the width and the height of the content you can use display: table;
.clearfix:after {
display:block;
clear:both;
}
.menu-wrap ul {
margin:0px;
padding:0px;
}
.menu-wrap {
width:80%;
background: #85D2CD;
display: table;
margin: 0 auto;
}
.menu {
display: table;
margin:0 auto;
}
.menu li {
margin:0px;
list-style:none;
}
.menu a {
transition:all linear 0.15s;
color:#FFF;
}
.menu > ul > li {
float:left;
display:inline-block;
position:relative;
font-size:19px;
}
.menu > ul > li > a {
padding:10px 30px;
display:inline-block;
text-shadow:0px 1px 0px rgba(0,0,0,0.4);
text-decoration:none;
}
<div class="menu-wrap">
<nav class="menu">
<ul class="clearfix">
<li>Home</li>
<li>Second Page</li>
<li>Third Page<span class="arrow">▼</span>
<ul class="sub-menu">
<li>Dropdown One</li>
<li>Dropdown Two</li>
</ul>
</li>
<li>Fourth Page</li>
<li>Fifth Page<span class="arrow">▼</span>
<ul class="sub-menu">
<li>Dropdown 1</li>
<li>Dropdown Item 2</li>
<li>Dropdown 3</li>
<li>Dropdown 4</li>
</ul>
</li>
</ul>
</nav>
I am trying to place a black border below the dropdown menu, then having the "selected" page which is currently the "Home" page being underlined using a orange border. I am trying to overlap the orange border on the black border.
http://jsfiddle.net/46z5su4u/
Edited
Some people seems to be confused, I want to put the orange line on top of the black line.
http://jsfiddle.net/46z5su4u/2/
Any advices?
HTML:
<h1>Drop Down Menu</h1>
<header>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
</ul>
</li>
<li>Menu 3
<ul>
<li class="dir">Sub Menu 1</li>
<li class="dir">Sub Menu 2 THIS IS SO LONG IT MIGHT CAUSE AN ISSEUE BUT MAYBE NOT?
</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 4</li>
<li>Menu 5</li>
<li>Menu 6</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
CSS:
header
{
border-bottom:4px solid #3a3a3a;
}
#primary_nav_wrap
{
margin-top:15px;
margin-bottom:-4px;
position:relative;
}
#primary_nav_wrap ul
{
list-style:none;
float:left;
margin:0;
padding:0;
}
#primary_nav_wrap ul a
{
display:block;
text-decoration:none;
line-height:32px;
padding:0 15px;
font-family:"Open Sans","Lucida Grande",Tahoma,"Trebuchet MS",Verdana,Arial,sans-serif;
font-size:1.1em;
}
#primary_nav_wrap ul li
{
position:relative;
float:left;
margin:0;
padding:0;
}
#primary_nav_wrap ul li.current-menu-item
{
border-bottom:4px solid #ff7500;
font-weight:700;
}
#primary_nav_wrap ul li.current-menu-item a
{
color:#333;
}
#primary_nav_wrap ul li a:hover
{
color: #ff7500;
}
#primary_nav_wrap ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#f8f8f8;
padding: 0 12px;
box-shadow: 0 2px 2px #ccc;
}
#primary_nav_wrap ul ul li
{
float:none;
width:150px;
border-bottom: solid 1px #f2f2f2;
border-top: solid 1px #fff;
}
#primary_nav_wrap ul ul li:after
{
content: "";
}
nav ul ul li:last-child a
{
border-bottom: none;
}
nav ul ul li:first-child a
{
border-top: none;
}
#primary_nav_wrap ul ul a
{
line-height:120%;
padding:10px 6px;
}
#primary_nav_wrap ul li:hover > ul
{
display:block;
}
you may need a lower boarder when you move?
#primary_nav_wrap ul li a:hover
{
color: #ff7500;
border-top: 4px solid #ff7500;/*add*/
}
Try this http://jsfiddle.net/46z5su4u/1/
this is all i have added
#primary_nav_wrap ul li ul li {
border-bottom:1px solid orange;
}
Fixed
http://codepen.io/anon/pen/bfgvo
Added to html
<div class="clearBoth"></div>
<div id="top_blackline"></div>
Added to css
.clearBoth
{
clear:both;
}
#top_blackline
{
margin-top:-4px;
width: 100%;
border-bottom:4px solid #3a3a3a;
}
Just set the dark border on the first-level <li>s instead of <header>.
So the updated css code will look like this:
header {
position: absolute; /* removed border */
}
/* add the following rule */
#primary_nav_wrap > ul > li {
border-bottom:4px solid #3a3a3a;
}
which sets the dark border on first-level <li>s only using the direct child selector >, more on this selector here.
a preview of the code above is here.
I have created this kind of menu.
The CSS classes used are as follows.
#main_nav {
padding: 3px 0px 3px 0px;
z-index: 300 !important;
}
#main_nav ul {
list-style:none;
position:relative;
float:left;
margin:0;
padding:0;
background: #888;
z-index: 300 !important;
min-width: 150px;
white-space: nowrap;
}
#main_nav ul a {
display:block;
text-decoration:none;
font-size:12px;
line-height:32px;
padding:0 15px;
color: #fff;
font-family: "Helvetica Neue", Helvetica, Arial, Verdana, sans-serif;
}
#main_nav ul li {
position:relative;
float:left;
margin:0;
padding:0;
}
#main_nav ul li.current-menu-item {
background:#ddd;
}
#main_nav ul li:hover {
background: #008000;
}
#main_nav ul ul {
display:none;
position:absolute;
top:100%;
left:0;
background:#888;
padding:0;
}
#main_nav ul ul li {
float:none;
white-space: nowrap;
width: 100%;
}
#main_nav ul ul a {
line-height:120%;
padding:10px 15px;
}
#main_nav ul ul ul {
top:0;
left:100%;
}
#main_nav ul li:hover > ul {
display:block;
}
#main_nav > ul > li {
display: inline-block;
float: none;
margin: 0 -3px 0 0;
}
.menu li:last-of-type { border-right: none; }
.menu li > a:after {float: right; margin-left: 5px; content: '\25BA'; }
.menu > li > a:after {float: right; margin-left: 5px; content: '\25BC'; }
.menu li > a.only-child:after { margin-left: 0; content: ''; }
The HTML code :
$(document).ready(function() {
$("a:only-child").addClass("only-child");
});
<div id="main_nav">
<ul class="menu">
<li class="current-menu-item">Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
<li>Category 4</li>
<li>Category 5</li>
</ul>
</li>
<li>Deep Menu 2</li>
</ul>
</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 2</li>
<li>Menu 3</li>
</ul>
</div>
This works fine except float: right in the following CSS classes.
.menu li > a:after {float: right; margin-left: 5px; content: '\25BA'; }
.menu > li > a:after {float: right; margin-left: 5px; content: '\25BC'; }
float: right; is meant to float a corresponding arrow to the right of the menu. It however, does not allow the menu text and the arrow to inline properly.
The discrepancy is clearly visible on FireFox.
The picture has been taken from FireFox (31.0). The arrows are not lined up properly with the text. The menu looks good on Google Chrome (36.0.1985.143 m) and Internet Explorer (8).
How can arrows be aligned right and lined up properly?
It looks like instead of setting float:right you needed to use
position: absolute;
right: 0px;
As shown here
i have a menu that when i hover has a drop down i follow this
and this but unfortunately im getting different result than expected..im getting list but it is displayed as line horizontally and not vertically like the example here is the code i added to make the drop down
css
ul # menu li ul .submenu{
display:none;
}
ul # menu li : hover ul .submenu{
display:block;
}
here is the html
<div id="menu">
<ul id="menu-list">
<li id="">Home</li>
<li id="">
Menu
<ul class="sub-menu">
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
</ul>
</li>
<li id="">Menu</li>
<li id="">Menu</li>
<li id="">Menu</li>
</ul>
</div>
Here you go:
Updated Fiddle
Just the small change. Made UL id = "menu"
<ul id="menu">
<li id="home">Home</li>
<li id="townoffice">
Menu
<ul class="sub-menu">
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
</ul>
</li>
<li id="busines">Menu</li>
<li id="residents">Menu</li>
<li id="tourists">Menu</li>
</ul>
HTML
<ul id="nav" class="sixteen columns">
<li>Home
</li>
<li>Portfolio
<ul>
<li>Asia
<ul>
<li>Korea</li>
<li>China</li>
<li>Japan</li>
</ul>
</li>
<li>Europe
<ul>
<li>France</li>
<li>Germany</li>
<li>Italy</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
#nav {
width:800px;
margin:30px 50px;
padding: 0;
float:left;
}
#nav li {
list-style: none;
float: left;
padding:0 10px;
background-color:#367FB3;
color:white;
}
#nav li a:hover {
background-color:#52baff;
color:#fff;
}
//daf adf
/*--temp--*/
#nav ul ul li {
clear:left;
}
#nav ul ul {
position:absolute;
left:14em;
top:0;
}
#nav ul ul li a {
display:block;
padding: 3px 15px;
color: #242424;
text-decoration: none;
font-size:13px;
font-family:"Lato" !important;
}
/*--end temp--*/
#nav li a {
display: block;
padding: 3px 15px;
color: #242424;
text-decoration: none;
font-size:13px;
font-family:"Lato" !important;
}
#nav a:hover {
color:#367FB3;
}
#nav a:active {
color:#367FB3;
}
#nav li ul {
display: none;
width: 14em;
/* Width to help Opera out */
background-color:transparent;
z-index:666;
}
#nav li:hover ul, #nav li.hover ul {
display: block;
position: absolute;
margin:0px -10px;
padding:0px;
}
#nav li:hover ul ul {
display:none;
}
#nav li ul li:hover ul {
display:block
}
#nav li:hover li, #nav li.hover li {
float: none;
line-height:30px;
}
#nav li:hover li a, #nav li.hover li a {
background-color:#367FB3;
color:#fff;
font-size:13px;
font-family:"Lato" !important;
}
#nav li li a:hover {
background-color:#52baff;
color:#fff;
}
FIDDLE