I have a menu bar on a hover it should display a submenu. I am displaying the sub menu but it moving entire main menu to side. I want the submenu out of the main menu, next to the main menu.
my HTML code
<div class="MenuBar">
<ul>
<li><img src="#"><br>text1</li>
<div id="submenu">
this is a test div
</div>
<li><img src="#"><br>text2</li>
<li><img src="#"><br>text3</li>
</ul>
CSS
#submenu {
display: none;
}
.MenuBar ul li a:hover #submenu {
display: block;
position: relative;
top: 20px;
}
I think this is what you are after. I have added classes to the lists, changed your id submenu to class and added submenu items to all lists.
.MenuBar ul li .submenu {
display: none;
}
.MenuBar ul li.men1:hover .submenu {
display: inline-block;
position: relative;
top: 20px;
}
.MenuBar ul li.men2:hover .submenu {
display: inline-block;
position: relative;
top: 20px;
}
.MenuBar ul li.men3:hover .submenu {
display: inline-block;
position: relative;
top: 20px;
}
<div class="MenuBar">
<ul>
<li class="men1">text1
<div class="submenu">
hovered 1st
</div></li>
<li class="men2">text2
<div class="submenu">
hovered 2nd
</div></li>
<li class="men3">text3<div class="submenu">
hovered 3rd
</div></li>
</ul>
Let me know if this was what you were after.
you can use following CSS for this:
if you want to show submenu in bottom of parent menu
.MenuBar ul li {
position: relative;
}
.MenuBar ul li:hover #submenu {
display: block;
position: absolute;
top: 100%;
left: 0px;
}
or if you want to show submenu in right of parent menu
.MenuBar ul li {
position: relative;
}
.MenuBar ul li:hover #submenu {
display: block;
position: absolute;
top: 0;
left: 100%;
}
Place the submenu div outside your lists. If you throw that inside a bunch of list item and play with display property, it will affect the list structure. Place that div outside the list item, position the submenu div with css so it always alignment to the respective menu item and them add your hover effect.
Change Html Like this:
<li>
<img src="#"><br>text1
<div id="submenu"><-----------------submenu must be child li
this is a test div
</div>
</li>
And Css Like This :
.MenuBar ul li a:hover + #submenu {<--------------use + selector
display: block;
position: relative;<-----Remove This
top: 20px;<------Remove This
margin-top: 20px;<----------Add This
margin-bottom: 10px;<-------Add This
}
Full Code:
#submenu {
display: none;
}
img {
width: 20px;
}
ul {
list-style: none;
}
.MenuBar ul li a:hover + #submenu {
display: block;
margin-top: 20px;
margin-bottom: 10px;
}
<div class="MenuBar">
<ul>
<li>
<a href="#">
<img src="https://www.seeklogo.net/wp-content/uploads/2012/12/apple-logo-eps-logo-vector-400x400.png"><br>text1
</a>
<div id="submenu">
this is a test div
</div>
</li>
<li>
<a href="#">
<img src="https://www.seeklogo.net/wp-content/uploads/2012/12/apple-logo-eps-logo-vector-400x400.png"><br>text2
</a>
</li>
<li>
<a href="#">
<img src="https://www.seeklogo.net/wp-content/uploads/2012/12/apple-logo-eps-logo-vector-400x400.png"><br>text3
</a>
</li>
</ul>
</div>
Related
I have a simple menu made in html/css and the problem I encountered is that if I put my mouse pointer over menu item (test2) to expand submenu then other items from menu section (test1) change their positions: https://jsfiddle.net/dsb87pxz/
<nav>
<ul>
<li>test1</li>
<li>
test2
<ul>
<li>test2.1</li>
<li>test2.2</li>
</ul>
</li>
</ul>
</nav>
nav > ul > li {
display: inline-block;
}
nav > ul > li > ul {
display: none;
}
nav > ul > li:hover > ul {
display: block;
}
nav > ul > li > ul > li {
display: block;
}
Can you suggest a solution for this problem?
With vertical-align: top
nav>ul>li {
display: inline-block;
vertical-align: top;
}
nav>ul>li>ul {
display: none;
}
nav>ul>li:hover>ul {
display: block;
}
nav>ul>li>ul>li {
display: block;
}
<nav>
<ul>
<li>test1</li>
<li>
test2
<ul>
<li>test2.1</li>
<li>test2.2</li>
</ul>
</li>
<li>
test2
<ul>
<li>test2.1</li>
<li>test2.2</li>
</ul>
</li>
</ul>
</nav>
When you hover over a list item in the first level, it affects the list item on the right, because of display: inline-block.
Therefore one can use float: left and display: relative for <li> in the first level and display: absolute for the <ul> inside of the <li>.
Example
ul {
list-style: none;
padding: 0;
}
li {
padding: 2px 5px;
}
nav>ul>li {
float: left;
position: relative;
}
nav>ul>li>ul {
position: absolute;
left: 0;
display: none;
}
nav>ul>li:hover>ul {
display: block;
}
<nav>
<ul>
<li>test1</li>
<li>
test2
<ul>
<li>test2.1</li>
<li>test2.2</li>
</ul>
</li>
<li>
test2
<ul>
<li>test2.1</li>
<li>test2.2</li>
</ul>
</li>
</ul>
</nav>
You could add position:absolute to your hover element like so
nav > ul > li:hover > ul {
display: block;
position: absolute;
}
You could add position:absolute to your hover element like so
nav > ul > li:hover > ul {
display: block;
position: absolute;
}
That's true you just need to add vertical-align as top to your inline-block elements which are li tags here.
display:inline-block by default aligns their block text to the baseline and that's why when user hover in above codes it aligns the text to the baseline i.e. vertical-align:baseline so change that to vertical-align:top.
nav > ul > li{
display: inline-block;
vertical-align:top; /*Just add this as already suggested*/
}
I have custom navbar, not any bootstrap or something else.
I tryed one tab is left and dropdowns are middle and one tab is right side.
But right side tab isnt inside navbar.
I dont understand why it is going out of navbar if it is in ul tag. Maybe someone can explain that.
HTML code:
<div id="nav">
<div id="nav_wrapper">
<ul>
<div id="left">
<li>
item #1
</li>
</div>
<div id="mid">
<li> dropdown #1
<ul>
<li>
dropdown #1 item #1
</li>
<li>
dropdown #1 item #2
</li>
<li>
dropdown #1 item #3
</li>
</ul>
</li>
<li> dropdown #2
<ul>
<li>
dropdown #2 item #1
</li>
<li>
dropdown #2 item #2
</li>
<li>
dropdown #2 item #3
</li>
</ul>
</li>
<li> dropdown #3
<ul>
<li>
dropdown #3 item #1
</li>
<li>
dropdown #3 item #2
</li>
<li>
dropdown #3 item #3
</li>
</ul>
</li>
</div>
<div id="right">
<li>
item #2
</li>
</div>
</ul>
</div>
<!-- Nav wrapper end -->
Style code:
body {
padding: 0;
margin: 0;
font-family: Arial;
font-size: 17px;
}
#nav {
background-color: #222;
}
#left{
float: left;
}
#mid{
margin-left: auto;
margin-right: auto;
text-align: center
}
#right{
float: right;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
min-width: 200px;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #333;
}
#nav ul li a, visited {
color: #CCC;
display: block;
padding: 15px;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-top: 0;
margin-left: -5px;
z-index: 1;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:hover {
color: #699;
}
Added html and style code here.
Problem is right side isnt in navbar.
I think in divs is problem.
Pls try
#nav_wrapper ul{
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display:flex;
}
and now you DON'T have to give float:left and float:right to the #left and #right divs; and your right div will be aligned on the right end.
OR
Add the following codes to your existing styles, to align your third item on the right end.
#nav{
background-color: #222;
overflow:hidden;
}
#mid{
float:left;
}
I asked previously a question about how to make a dropdown menu by css. Now I've got it to work beautifully. But now I need to make it react to on click. The menu I'm trying to create will have 2 sets of nested list items. I'm using pseudo selecter :focus on the first level list items. But of course I lose the focus part when I'm trying to display the nested list item. Is it possible in some way to keep the first level list item to stay focused while the user goes deeper in the menu?
My html code is like this
<header class="Header">
<!-- Head navigation-->
<div>
<img src="images/cart_logo_webb_design.svg" alt="cart">
<img src="images/search_logo_webb_design.svg" alt="search glass">
</div>
<div> <img src="images/k_logo_webb_design.svg" alt="CompanyLogo"> </div>
<div>
<nav id="MainNavigation">
<img src="images/menu_logo_webb_design.svg" alt="Menu icon">
<ul id="dropDownMenu">
<li>
<a class="Sub_Menu_Link" href="#" title="Woman">Woman
</a>
<li>
<a class="Sub_Menu_Link" href="#" title="Womanplus">+
</a>
<ul>
<li>1 </li>
<li>2 </li>
<li>3 </li>
<li>4 </li>
<li>5 </li>
</ul>
</li>
</li>
<li> <a class="Sub_Menu_Link" href="#" title="Man">Man</a>
<li>
<a class="Sub_Menu_Link" href="#" title="Manplus">+
</a>
<ul>
<li>1 </li>
<li>2 </li>
<li>3 </li>
<li>4 </li>
<li>5 </li>
</ul>
</li>
</li>
<li><a class="Sub_Menu_Link" href="#" title="Sale">Sale</a></li>
</ul>
</nav>
</div>
</header>
header {
position: fixed;
display: block;
background: white;
z-index: 1;
width: 100%;
top: 0;
}
.Header img {
width: 36px;
height: 40px;
}
header div {
float: right;
width: 33.33%;
margin: 0;
}
header div:first-of-type a {
padding: 0%;
}
header div:after {
content: "";
visibility: hidden;
display: block;
clear: both;
}
nav {
position: relative;
left: 0;
}
nav > ul {
margin: 0;
padding: 0 0px;;
float: left;
line-height: 40px;
}
nav a {
positon: absolute;
left: 0;
}
nav ul ul {
padding: 20px;
}
nav ul a {
list-style: none;
text-decoration: none;
}
nav ul ul li a {
display: inline-block;
}
nav > ul:after {
content: "";
visibility: hidden;
display: block;
clear: both;
}
.Sub_Menu_Link {
display: inline-block;
padding: 10px 0px;
line-height: 40px;
}
.Sub_Menu_Link:hover {
color: yellow;
}
nav ul {
background: #E9E9E9;
position: relative;
width: 100%;
}
nav ul ul li a:hover {
color: yellow;
}
nav ul ul {
display: none;
}
nav ul a:focus {
background: red;
}
nav ul a:focus ~ ul {
display: block;
}
nav > a:hover {
background-color: red;
}
nav > a:focus {
background-color: green;
}
nav ul {
display: none;
}
nav > a:focus ~ ul {
display: block;
}
It is not possible to do it simply with the :focus selector. In CSS, you cannot select the parent of an element, so as to keep the visibility with focus on a child. As an example:
element:focus -parent { /* this doesn't exist */ }
That simply does not exist as of right now. And you also cannot have focus on multiple elements, which adds on to the issue.
There are two ways that I can think of solving your problem:
Using the JavaScript event onclick instead of a CSS-only approach;
Maintaining the CSS-only approach and using input:checked instead of :focus for your triggers. It's known as The Checkbox Hack.
I don't know how to really explain this in a simple question for the title, but I have an example page to show you what I mean at http://www.yenrac.net/test
I am having problems with dropdown menus, as you can see. Whenever I hover over to the next category in the dropdown 'accordion' it jumps back and screws with desktop cursors if they have their browser in a small window. Another thing is that mobile users cannot just click the box link again to close the dropdown, which is something else I would like to fix. Can someone please take a look at this and help me figure out what I am doing wrong?
Here is my HTML: http://pastebin.com/RkFs97wH
<nav>
<ul id="navigation">
<li><a id="current" href="index.html">Home</a></li>
<li>FAQ</li>
<li><a class="dropdown" href="#">Dropdown</a>
<ul>
<li>
<a class="pullout" href="#">Pullout Tab</a>
<ul>
<li>Tier 3 Tab</li>
</ul>
</li>
<li>
<a class="pullout" href="#">Pullout Tab 2</a>
<ul>
<li>Tier 3 Tab</li>
</ul>
</li>
</ul>
</li>
<li>Servers</li>
<li id="last">Contact Us</li>
</ul>
</nav>
Here is my CSS: http://pastebin.com/ttYh0Qz6
/* Set to very large range just for convenience of testing */
#media screen and (max-width: 10000px) {
#postdatemeta {
visibility: visible;
}
.postdate {
visibility: hidden;
}
nav ul {
list-style-type: none;
margin: 0 auto;;
padding: 0;
width: 98%;
}
nav li a {
display: block;
color: #fff;
padding: 1em 0;
margin: 0.3em auto;;
text-decoration: none;
text-align: center;
background: #000;
}
nav ul ul {
position: absolute;
visibility:hidden;
}
nav ul li:hover > ul {
visibility: visible;
position: relative;
}
nav ul ul li a {
background: #999;
}
.pullout:after {
content: "\000020\0025BE";
}
I appreciate any help and feedback you are able to provide me!
You are adding a margin to the a element, but the parent li item ignores these margins currently, so when you mouse over the margins, it recognizes you as moving the mouse off of the li, so the :hover state goes away resulting in a collapsed menu.
Try add the following:
nav li {
width: 100%;
display: inline-block;
}
This allows the li element to wrap the a element including the margins, so when they are moused over, the :hover state remains.
#media screen and (max-width: 10000px) {
nav ul {
list-style-type: none;
margin: 0 auto;
;
padding: 0;
width: 98%;
}
nav li {
width: 100%;
display: inline-block;
}
nav li a {
display: block;
color: #fff;
padding: 1em 0;
margin: 0.3em auto;
;
text-decoration: none;
text-align: center;
background: #000;
}
nav ul ul {
position: absolute;
visibility: hidden;
}
nav ul li:hover > ul {
visibility: visible;
position: relative;
}
nav ul ul li a {
background: #999;
}
.pullout:after {
content: "\000020\0025BE";
}
}
<nav>
<ul id="navigation">
<li><a id="current" href="index.html">Home</a>
</li>
<li>FAQ
</li>
<li><a class="dropdown" href="#">Dropdown</a>
<ul>
<li>
<a class="pullout" href="#">Pullout Tab</a>
<ul>
<li>Tier 3 Tab
</li>
</ul>
</li>
<li>
<a class="pullout" href="#">Pullout Tab 2</a>
<ul>
<li>Tier 3 Tab
</li>
</ul>
</li>
</ul>
</li>
<li>Servers
</li>
<li id="last">Contact Us
</li>
</ul>
</nav>
I have made a navigation menu bar and also two dropdown menus with 3 items each. My problem is that, below this menu, I have a slider, so when I hover my dropdown, two of the three items are hidden behind the slider.
How can I solve this?
My HTML:
<div id="nav">
<div id="nav_wrapper">
<ul>
<li>item #1
</li>
<li> item #2
</li>
<li> dropdown #1
<ul>
<li>dropdown #1 item #1
</li>
<li>dropdown #1 item #2
</li>
<li>dropdown #1 item #3
</li>
</ul>
</li>
<li> dropdown #2
<ul>
<li>dropdown #2 item #1
</li>
<li>dropdown #2 item #2
</li>
<li>dropdown #2 item #3
</li>
</ul>
</li>
<li> item #3
</li>
</ul>
</div>
</div>
My CSS:
#nav {
background-color: #222;
}
#nav_wrapper {
width: 960px;
margin: 0 auto;
text-align: left;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
position: relative;
min-width: 200px;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #333;
}
#nav ul li a, visited {
color: #CCC;
display: block;
padding: 15px;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #333;
border: 5px solid #222;
border-top: 0;
margin-left: -5px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:hover {
color: #699;
}
I think the problem is on your z-index properties.
You need to set on #nav and #slider elements position and z-index properties.
#nav {
position: relative;
z-index: 2;
}
#slider{
position: relative;
z-index: 1;
}
That means the #nav menu will be front slider when you set a z-index property large than z-index on #slider. Have you attention that z-index doesn't work if you not set element position property like relative or absolute.
You can look this link for example - http://css-tricks.com/almanac/properties/z/z-index/