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*/
}
Related
I want to display listed items (List 1) inside listed items (List 2) and I want the <li> elements of the first list to be displayed inline and the <li> elements inside each first <li> to be displayed vertically on hover. The problem that when hovering over the first main <li>, then the second main <li> will be displayed at the end of the first main <li> which is not expected:
The following is a live display of the problem:
https://codepen.io/alafawzi/pen/PaVYyB
ul {
list-style: none;
padding: 0;
margin: 0;
height: 100%;
}
ul li {
display: inline-block;
padding: 15px;
}
ul li ul li{
margin: 0;
padding: 0;
display: none;
}
ul li:hover ul li{
display: block;
}
<body>
<header>
Html5 begins
</header>
<nav>
<ul>
<li>div
<ul>
<li>Link1.1</li>
<li>Link1.2</li>
<li>Link1.3</li>
</ul>
</li>
<li>head
<ul>
<li>Link2.1</li>
<li>Link2.2</li>
<li>Link2.3</li>
</ul>
</ul>
</nav>
</body>
You need to add absolute position in sub ul.
ul {
list-style: none;
padding: 0;
margin: 0;
height: 100%;
}
ul li {
display: inline-block;
padding: 15px;
position: relative;
}
ul li ul{
margin: 0;
padding: 0;
display: none;
position: absolute;
}
ul li:hover ul{
display: block;
}
<body>
<header>
Html5 begins
</header>
<nav>
<ul>
<li>div
<ul>
<li>Link1.1</li>
<li>Link1.2</li>
<li>Link1.3</li>
</ul>
</li>
<li>head
<ul>
<li>Link2.1</li>
<li>Link2.2</li>
<li>Link2.3</li>
</ul>
</ul>
</nav>
</body>
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>
I have a navigation working fine in my website, but, we decided to put the logo in the middle of it and now I can't vertical align it, I tried using line-height but it did not make the trick.
I put the code in the snippet, can someone give me a hand?
nav > ul > li > a > img {
width: 60px;
}
nav > ul {
list-style: none;
}
nav > ul > li {
display: inline-block;
}
nav > ul > li > a {
text-decoration: none;
color: black;
}
<nav>
<ul>
<li>Shop</li>
<li>Shop</li>
<li><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Wi-Fi_Logo.svg/1280px-Wi-Fi_Logo.svg.png" alt="logo"/></li>
<li>Shop</li>
<li>Shop</li>
</ul>
</nav>
inline-block by default is vertical-align:baseline so set it to middle,
same rule to img, so if you don't want to apply to li you can apply to img instead
nav > ul > li > a > img {
width: 60px;
/*vertical-align:middle - this would work here by itself too */
}
nav > ul {
list-style: none;
}
nav > ul > li {
display: inline-block;
vertical-align: middle
}
nav > ul > li > a {
text-decoration: none;
color: black;
}
<nav>
<ul>
<li>Shop
</li>
<li>Shop
</li>
<li>
<a href="#">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Wi-Fi_Logo.svg/1280px-Wi-Fi_Logo.svg.png" alt="logo" />
</a>
</li>
<li>Shop
</li>
<li>Shop
</li>
</ul>
</nav>
Just vertically align the image, using the vertical-align property.
The value you want is most likely middle.
nav > ul > li > a > img {
width: 60px;
vertical-align:middle;
}
nav > ul {
list-style: none;
}
nav > ul > li {
display: inline-block;
}
nav > ul > li > a {
text-decoration: none;
color: black;
}
<nav>
<ul>
<li>Shop</li>
<li>Shop</li>
<li><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Wi-Fi_Logo.svg/1280px-Wi-Fi_Logo.svg.png" alt="logo"/></li>
<li>Shop</li>
<li>Shop</li>
</ul>
</nav>
I have a menu like that and i have arrows if any item has child items,I use css content property to put arrows but i cant allign arrows to the end of the line.
<ul id="menu-bar">
<li><a>homepage</a></li>
<li><a>about us</a></li>
<li><a>services</a>
<ul>
<li><a>service 1</a>
<ul>
<li><a>service 1A</a></li>
<li><a>service 1B</a></li>
</ul>
</li>
</ul>
</li>
</ul>
this is my css
#menu-bar:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
#menu-bar li ul li > a:after {
content: '▶';
}
If you are trying to align it to the right try this http://jsfiddle.net/omcuL0vw/
#menu-bar li ul li > a:after {
float:right;
content: '▶';
}
I am trying to create a horizontal navigation that when you rollover the root items the sub pages and their sub pages are shown below but in a 3 or 4 column layout. I have experimented with the css "column count" but it is not giving me consistent results. I am wondering if anyone has come across this before or could point me in the right direction.
<ul id="nav">
<li class="nonActive rootNav" id="rootNav1">
for Residents
<ul>
<li><a href="/for-residents/history-of-smithville/">History of
Smithville</a></li>
<li>Mission and Vision</li>
<li>Alerts</li>
<li>FAQs</li>
</ul>
</li>
<li class="nonActive rootNav" id="rootNav2">
for Business
<ul>
<li>Film Commission</li>
<li>Comprehensive Plan</li>
<li>Chamber of Commerce</li>
</ul>
</li>
<li class="nonActive rootNav" id="rootNav3">
our Community
<ul>
<li>Calendar</li>
<li>News</li>
<li><a href="/our-community/memorial-park-project/">Memorial Park
Project</a></li>
<li>City Maps</li>
<li>Airport</li>
</ul>
</li>
<li class="nonActive rootNav" id="rootNav4">
city Departments
<ul>
<li>Police Department</li>
<li>Fire Department</li>
<li>Parks and Rec</li>
<li>Public Library</li>
<li>
Utilities
<ul>
<li>Pay online</li>
</ul>
</li>
<li>Public Works</li>
</ul>
</li>
<li class="nonActive rootNav" id="rootNav5">
city Government
<ul>
<li>
City Council
<ul>
<li><a href=
"/city-government/city-council/city-council-meeting-minutes/">City Council
meeting minutes</a></li>
</ul>
</li>
<li>City Manager</li>
<li>City Staff</li>
<li>
Municipal Court
<ul>
<li><a href="/city-government/municipal-court/municipal-judges/">Municipal
Judges</a></li>
<li><a href="/city-government/municipal-court/open-warrants/">Open
Warrants</a></li>
</ul>
</li>
</ul>
</li>
</ul>
body {
margin: 0px;
}
ul#nav {
margin: 0px;
padding: 0px;
}
ul#nav li {
list-style-type: none;
display: inline;
position: relative;
float: left;
}
ul#nav li a {
display: block;
padding: 10px;
background-color: #EAEAEA;
border: 1px solid #000000;
}
ul#nav li ul {
display: none;
position: absolute;
width: 750px;
margin: 0px 0px 0px -40px;
clear: both;
columns:200px 3;
-webkit-columns:200px 3; /* Safari and Chrome */
-moz-columns:200px 3; /* Firefox */
}
ul#nav li:hover ul {
display: block;
}
ul#nav li ul li {
clear: left;
display: block;
float: none;
}
ul#nav li ul li ul {
margin: 0px;
padding: 0px 0px 0px 10px;
position: relative;
}
ul#nav li ul li ul li {
clear: both;
display: block;
}
Here is my fiddle
fiddle
Here are a few examples of what I am trying to achieve.
Hm, based on your JSFiddle, I'm assuming the problem you're facing right now is that all of the submenus are lined up with the menu item that causes them to appear, and you want them aligned to only the left. You could achieve this through removing the relative positioning on <li> elements, and using left:0 on the submenus to put them where you want them.
So, your CSS adjustments would look like:
ul#nav li {
/* position:relative; */
}
ul#nav li ul {
left:0;
}
Here's a JSFiddle to show you how that look. Hope this helps! Let me know if you have any questions.
I'm sure there are plenty of ways to tackle this with a ton of different options - but here's one approach I worked with
#nav {
position: relative;
float: left;
}
#nav > li {
width: 20%;
}
ul, li {
margin: 0px;
padding: 0px;
list-style-type: none;
width: 100%;
float: left;
}
ul li a {
display: block;
padding: 10px;
background-color: #EAEAEA;
border: 1px solid #000000;
text-decoration: none;
}
ul li ul {
position: absolute;
top: 40px;
left: 0;
display: none;
}
ul li:hover > ul {
display: block;
}
/* The Rest for example purposes */
ul li ul li {
width: 25%;
}
ul li ul li a {
background: #ddd;
border: none;
}
With skipping the relative positioning on the first li children, the second level of ul's can inherit the width of the top level ul.
# http://jsfiddle.net/PqhEs/
Organized it a little differently - It all depends on how you'd want to group items in your sub sub pages, right now in the fiddle they just inherit the sub navigation's styles, but you could remove the float and adjust the width to have them list like the example.