I've got a simple menu that should show a sub-menu vertically. However, i changed this menu to be in order to center it, and it now doesn't show vertically but horizontally.
Here is my codepen :
http://codepen.io/anon/pen/NGwmGq
.navitem{
height: 30px;
}
#menu ul {
margin:0;
padding:0;
list-style-type:none;
text-align:center;
}
#menu li {
background-color:black;
display: inline-block;
padding: 0;
vertical-align: middle;
}
#menu li a {
display:block;
width:125px;
color:white;
text-decoration:none;
line-height:30px
}
#menu li a:hover {
color:#FFD700;
}
#menu ul li ul {
display:none;
}
#menu ul li:hover ul {
display:block;
}
#menu li:hover ul li {
float:none;
}
#menu li ul {
position:absolute;
}
#menu {
height:30px;
margin-top: 30px;
}
.table {
display: table; /* Allow the centering to work */
margin: 0 auto;
}
/* Logo */
#logo{
height: 190px;
width: 266px;
background-color:black;
margin: 0 30px;
}
/* Fin MENU */
As you can see, the "portfolio" actually shows the sub-menu but this sub-menu should be vertically aligned.
You seem to have deleted the positioning context on the li
#menu li {
background-color: black;
display: inline-block;
padding: 0;
vertical-align: middle;
position: relative; /* add this */
}
For li you have given display as inline-block that,s why they are coming in one line. so for portfolio submenu Write this css in your css file
#menu ul li ul li {
display: block;
}
In your css, you're making all main menu items inline using #menu li selector, which is also applied for lis in sub menu.
So you've to explicitly specify display: block for sub menu lis
Change your code as given below.
#menu li:hover ul li {
float:none;
display:block;
}
Just Add display:block to your sub menu li.
#menu li:hover ul li {
float:none;
display:block; /* Add This */
}
UPDATED : EXPLANATION
1) display:block; property is a block property in HTML. So every element with such property takes a new line(Elements views Vertically).
2) display:inline-block; property is a block but inline property. So elements which such property appears on same single line(Elements views Horizontally).
Working : Fiddle
Related
I am trying to create 3 - tire drop down menu. I am positioning the third level sub menu to left of parent menu, but the top positions is no aligning properly.
below screen is the result i got.
css code:
/* the menu_new_new is the list, you don't need a wrapping div */
#menu_new{
Text-Align: Left;
/*width:100%;*/
background:#194eb1;
/* height:30px;*/
}
/* only mainmenu_new listitems */
#menu_new > li{
display:inline-block;
position:relative;
line-height:35px;
}
/* links in mainmenu_new and both submenu_news */
#menu_new a{
display:block;
text-decoration:none;
padding:0px 10px;
color:#fff;
text-align:Left;
}
/* submenu_news of both levels */
#menu_new li ul{
position: absolute;
top: 35px;
left:0;
min-width:150px;
background: #194eb1;
Text-Align: Left;
}
/* submenu text */
#menu_new li ul li{
Text-Align: Left;
line-height:20px;
List-style-type:none;
border: 1px solid white;
}
/* move the second level submenu_new to the right to don't overlap the parent submenu_new */
#menu_new ul ul{
position: absolute;
top:auto;
left:150px;
Z-index: 10;
}
/* hover effect for submenu_new links */
#menu_new li li a:hover{
background:rgba(255,255,255,0.5);
}
/* hide all submenu_news for default */
#menu_new li ul{
display: none;
}
/* show a submenu_new if the direct parent listitem is hovered */
#menu_new li:hover > ul{
display:block;
}
How to align the top position exactly to parent menu position?
Please refer this for sample code: http://jsfiddle.net/asovbLqd/?
Please Chk URL enter link description here
#menu_new ul ul{
top:0px;
}
#menu_new li ul li{
position:relative;
}
EDIT: Also need to add position: relative to #menu_new li ul li.
For #menu_new ul ul, instead of top: auto, use top: 0.
To fix the Top for all sub menu, you need to provide "postion:relative to its parent element"
And to fix the Left extra space you need is to remove the default Padding from "<ul>" tag
Here is the Fiddle with both issues resolved "Top position and Left position":
http://jsfiddle.net/asovbLqd/14/
I have just changed the below:
Added position: relative; to the parent element of Sub Menu <ul>
#menu_new li ul li {
Text-Align: Left;
line-height:20px;
List-style-type:none;
border: 1px solid white;
position: relative;
}
And Removed Padding and margin from <ul> tags
#menu_new ul ul {
position: absolute;
top: 0;
left: 150px;
z-index: 10;
padding: 0;
margin: 0;
}
#menu_new li ul {
display: none;
margin: 0;
padding: 0;
}
Hope this will help!
http://jsfiddle.net/KsR5K/
#nav {
position: absolute;
font-family: digital_techregular;
font-size: 24px;
}
#nav ul ul {
display: none;
}
#nav ul li:hover > ul {
display:block
}
#nav ul {
background: #000;
list-style: none;
position: relative;
padding: 0px;
}
#nav ul:after {
content:"";
clear:both;
display:block
}
#nav ul li {
float:left
}
#nav ul li:hover {
background: #757575;
}
#nav ul li:hover a {
color:#fff
}
#nav ul li a {
display:block;
padding-top: 3px;
padding-left: 20px;
padding-right:20px;
color:#fff;
text-decoration:none
}
#nav ul ul {
background: #000;
padding:0;
position:absolute;
top:100%
}
#nav ul ul li {
float:none;
position:relative
}
#nav ul ul ul {
position:absolute;
left:100%;
top:0;
}
#nav ul ul li a {
padding-left: 20px;
padding-right: 20px;
color:#fff
}
#nav ul ul li a:hover {
background:#757575;
}
Right now, I have two drop down menus. One under Browse Models, and another under Tutorials.
If you look at the sub menus, the items' widths adjust accordingly depending on what is typed. If you look at tutorials, under the Printers sub menu, you will see Test, and Test 2. Only on Test 2, the 2 is placed underneath the word Test.
Basically, the sub sub menus aren't properly adjusting based on the width of the items in the same way that the sub menus are. If you were to type "Model Finishing" where Test 2 is, it would stack the words. But the "Model Finishing" item in the sub menu is all on one line.
I'm still pretty new at this. This is the first time I've tried to create a nested menu like this, I'm pretty much just going off of tutorials, so any advice would be appreciated.
Thanks for your time.
Edit: Changed the JSfiddle and code to reflect what I'm currently working with.
http://jsfiddle.net/S5cYJ/6/
#nav ul ul ul {
/* add this */
width:100%;
}
also remove width:100% from below
#nav ul ul li a {
padding: 15px 40px;
color: #fff;
display:block;
}
you should look into bootstrap. It has a nice menu already built so that you can concentrate and doing other things http://getbootstrap.com/components/#navbar
Fixed this by adding:
white-space: nowrap;
to #nav ul.
I have a verticle navigation bar that has options that appear after you hover over the first option. When I have margins to the left, you see a circle bullet point appear before the first bullet. Anyone have a fix for this? I can't seem to fix it.
.nav
{
margin-left: 30px;
}
.nav li {
list-style:none !important;
color:black;
font-size:30px;
text-decoration:none;
}
.nav ul {
padding:0;
margin:0;
background-color: white;
}
.nav ul li {
list-style: none !important;
float:left;
}
li > a
{
background-image: url("media/pics/triangle.png");
background-repeat: no-repeat;
background-size: 30px 30px;
background-position: right -10px;
}
li > a:only-child
{
background-image: none;
list-style-type: none;
}
.nav ul li a {
color:black;
font-size:30px;
text-decoration:none;
}
.nav li a:hover {
}
.nav ul li ul { display:none; } /* Makes the Dropdown menu Hidden by default */
.nav ul li:hover ul { /* Displayes the Dropdown menu when Hovered over the specific navigation item */
z-index:99999;
display:list-item !important;
position:absolute;
}
.nav ul li:hover ul li {
float:none;
list-style:none !important;
}
http://jsfiddle.net/zsPE9/
It's because of display:list-item !important added on :hover of .nav ul li.
You would use display:block, or something other than list-item.
.nav ul li:hover ul {
z-index:99999;
display:block; /* Changed from display:list-item !important */
position:absolute;
}
UPDATED EXAMPLE
Also, you should avoid usage of !important when possible. It essentially removes the cascade nature from CSS.
You have display set on list-item. That is probably what is causing your problem. Instead, maybe use block or inline. See other options here: http://www.w3schools.com/cssref/pr_class_display.asp. Also, next time please just post the code that is causing the issue :).
I have been trying quite a few guides on getting sub-menus to work. Some which include JS. Now I'm trying a just css approach but I'm kinda stuck on getting the submenu to work for me.
My code is on fiddle here: http://jsfiddle.net/PLb5K/
To do a basic test I have done
#nav ul li ul {
display: none; }
#nav ul li:hover ul {
display: block;
position: absolute; }
UPDATE
#nav ul:hover .sub {
display: block;
position:absolute;
}
fixes the "not working on hover" issue but any list item will show the sub menu. Please could anyone help on how to make it so only the single parent will show the submenu.
here is a basic html/css example for you:
the fiddle
css
ul {
margin: 0;
padding: 0px 100px 0px 0px;
list-style: none;
line-height: normal;
text-align: center;
}
ul li {
background-color:grey;
display: inline-block;
*display: inline;
padding:4px 8px;
margin:0;
zoom: 1;
}
ul li a{
color: white;
text-decoration:none;
}
ul li ul.sub{
display:none;
position:absolute;
margin-top:4px;
margin-left:-8px;
}
ul li:hover ul.sub{
display:block;
}
I am trying to create a simple sub menu which appears when u hover over the main menu.
Can anyone see where i am going wrong?
I have created this fiddle for example.
Try this: http://jsfiddle.net/KSeph/11/
It is not layouted but it works.
This css basically provides a very simple way to realize dropdowns:
.subMenu {
float: right;
}
.subMenu ul li {
float: left;
}
.subMenu ul ul {
display: none;
position: absolute;
}
.subMenu ul li:hover ul {
display: block;
}
.subMenu ul ul li {
float: none;
}
Hey now add two style in your css as like this
.subMenu li{
position:relative;}
.subsubmenu {
position:absolute;
top:22px;
left:0;
}
Live demo
on .subsubmenu add left: 0; top: 0; and remove float: right !important;
on .subMenu ul li add position: relative;