https://jsfiddle.net/ccaf8msu/1/
<nav class="navbar navbar-default">
<ul class="nav navbar-nav navbar-right">
<li class="first leaf">Frontpage</li>
<li class="leaf">Library</li>
<li class="expanded active-trail active">About the website
<ul class="nav navbar-nav navbar-right">
<li class="first leaf">About the book</li>
<li class="last leaf">The authors</li>
</ul>
</li>
<li class="leaf">Categories</li>
<li class="last leaf webshop-link">Webshop</li>
</ul>
</nav>
I have this 2 levels menu, and want both levels be horizontal as shown. The issue is when second level is active, and menu item 'about the website' gets a lot of whitespace after the item.
What i need is to get rid of the whitespace, and positioned the 2.level menu items to the right
Add style to li position:relative and inner ul position:absolute
nav.navbar-default {
width: 100%;
}
ul {
list-style: none;
}
ul li {
float: left;
position: relative;
}
li ul{
position:absolute
}
li a {
display: block;
height: 55px;
margin: 0 20px;
}
There's a whitespace because your dropdown is 1) after the <li> in the DOM order but 2) still takes up space before it goes to the next <li>.
What you need to do is set your dropdowns to have position: absolute so that it doesn't move your other elements around.
Also, in 2016, it's probably best to avoid floats and use flexbox for your layouts as it is much more powerful.
Here's a working code without white space:
CSS:
nav.navbar-default {
width: 100%;
}
ul {
list-style: none;
display: flex;
}
ul li {
justify-content: flex-start;
position: relative;
}
.navbar-right {
position: absolute;
top: 100px;
left: 0;
}
li a {
display: block;
height: 55px;
margin: 0 20px;
}
And here's the Fiddle.
Related
I have a drop down menu button that turns grey when hovered over. However, the grey does not expand throughout the whole button, but stops at the where I set the padding.
HTML:
#dropdown {
list-style: none;
display: none;
position: absolute;
background-color: #fff;
padding: 10px;
z-index: 99999;
width: 230%;
}
#dropdown li:hover,
#dropdown li:active {
background-color: #e3e3e3;
}
#dropdown.active, #menu:target #dropdown {
display: block;
}
<ul class="main-nav js--main-nav">
<li id="menu">
Help▾
<ul id="dropdown">
<li>Give</li>
<li>Get Help</li>
<li>Get Involved</li>
</ul>
</li>
</ul>
What I want is for the grey to take up all of the drop-down menu when hovered over, as opposed to just stopping at the padding.
EDIT: I should have clarified, when I meant I want it to take up "all" of the drop down menu, I meant just the "li" when I hover over that.
Is this what you were looking for?
#dropdown {
list-style: none;
display: none;
position: absolute;
background-color: #fff;
padding: 10px;
z-index: 99999;
width: 230%;
}
#dropdown:hover {
background-color: #eee;
}
#dropdown li:hover,
#dropdown li:active {
background-color: #e3e3e3;
}
#dropdown.active, #menu:target #dropdown {
display: block;
}
<ul class="main-nav js--main-nav">
<li id="menu">
Help▾
<ul id="dropdown">
<li>Give</li>
<li>Get Help</li>
<li>Get Involved</li>
</ul>
</li>
</ul>
Having trouble getting the drop-down menus to be directly aligned with the parent ones - at the moment they always fall from the center of the top one, see image.
My code:
body {
background: url('body-bkg.jpg');
background-repeat: no-repeat;
background-position: center top;
}
#navMenu {
margin: 0;
padding: 0;
text-align: center;
}
/*controls top parent box in navigation bar*/
#navMenu ul {
margin: 0;
padding: 0;
line-height: 30px;
display: inline-block;
}
/*controls top parent box in navigation bar*/
#navMenu li {
margin: 0;
padding: 0;
list-style: none;
position: relative;
background-color: hsla(0, 9%, 202%, 0.7);
float: left;
}
#navMenu ul li {
position: relative;
}
/*controls link text parent and children boxes in navigation bar*/
#navMenu ul li a {
text-decoration: none;
height: 30px;
width: 150px;
display: block;
color: #2e1c1c;
border: 1px solid #000;
}
/*controls children boxes in navigation bar*/
#navMenu ul ul {
position: absolute;
visibility: hidden;
top: 31px;
}
/*controls children boxes in navigation bar when hovered on parent box*/
#navMenu ul li:hover ul {
visibility: visible;
}
/*controls parent box when hovered on children box*/
#navMenu li:hover {
background: #387cf7;
}
/*controls child box when hovered on child box*/
#navMenu ul li:hover ul li a:hover {
background: white;
}
<div id="navMenu">
<ul>
<li>Destinations
<ul>
<li>Asia </li>
<li>Africa </li>
<li>Europe </li>
<li>North America </li>
<li>South America </li>
<li>Antartica </li>
</ul>
</li>
<li>Holiday Types
<ul>
<li>Short Breaks </li>
<li>Beaches </li>
<li>Adventure </li>
<li>Walking </li>
<li>Continents </li>
<li>Safari </li>
<li>Cruise </li>
<li>Family </li>
<li>Ultimate </li>
</ul>
</li>
<li>When to go
<ul>
<li>Winter </li>
<li>Spring </li>
<li>Summer </li>
<li>Autumn </li>
</ul>
</li>
<li>Corporate </li>
<li>Special Offers </li>
<li>About Us </li>
<li>Blog </li>
<li>Contact </li>
</ul>
</div>
This solution may work for you:
Demo Fiddle
I see you're using visibility:hidden a lot, but In situations like this I find display: none to be easier to work with.
CSS:
#navMenu ul ul {
// current styles
left: 0px;
}
I created a menu and a submenu that should appear after I hover over the list elements of the main menu. The problem is that the widths of the main menu's list elements changes while hovering, and it looks puzzling. I tried it with a fixed width, but the space between the menu items is too big then.
I also tried to set the submenu to position absolute, but the problem was that the submenu was always located at the same position and not beneath the main menu's item which was active.
I created a codepen for this issue.
HTML:
<ul class="menubar">
<li class="menubar-li">Unternehmen
<ul class="menubar-sub">
<li class="menubar-sub-li">Profil
<li class="menubar-sub-li">Meilensteine
<li class="menubar-sub-li">Team
<li class="menubar-sub-li">News
<li class="menubar-sub-li">Jobs</li>
</ul>
</li>
<li class="menubar-li">Kompetenzen
<ul class="menubar-sub">
<li class="menubar-sub-li">Kreation
<ul class="menubar-subsub">
<li>Strategie
<li>Design
<li>Online
<li>Reinzeichnung</li>
</ul>
</li>
<li class="menubar-sub-li"> Prepress
<ul class="menubar-subsub">
<li>Seitenproduktion</li>
<li>Kreativretusche
<li>Colormanagement
<li>Proofen</li>
</ul>
</li>
<li class="menubar-sub-li">Druck
<ul class="menubar-subsub">
<li>Personalisiert</li>
<li>Web 2 Print</li>
</ul>
</li>
<li class="menubar-sub-li">Katalogmanagement</ul>
</li>
<li class="menubar-li">Portfolio</li>
<li class="menubar-li">Service
<ul class="menubar-sub">
<li class="menubar-sub-li">Mediapool</li>
<li class="menubar-sub-li">DUON</li>
<li class="menubar-sub-li">Datenupload</li>
<li class="menubar-sub-li">Downloads
</ul>
</li>
<li class="menubar-li">Kontakt
<ul class="menubar-sub">
<li class="menubar-sub-li">Ansprechpartner</li>
<li class="menubar-sub-li">Anfahrt</li>
<li class="menubar-sub-li">Impressum</li>
</ul>
</li>
</ul>
CSS:
.menubar {
list-style: none;
position: relative;
bottom: 16px;
}
.menubar a {
color: black;
text-decoration:none;
font-size: 13px;
position: relative;
}
.menubar-li {
float: left;
height:29px;
line-height:29px;
vertical-align: middle;
padding-left: 10px;
padding-right: 10px;
margin-right: 10px;
}
.menubar-li:hover {
background-color: #94ba1d;
cursor: pointer;
}
.menubar-li:hover .menubar-sub {
display: block;
}
.menubar-sub {
display: none;
list-style: none;
margin-top: 1px;
padding-bottom: 10px;
padding-right: 10px;
padding-left: 0px;
background-color: #94ba1d;
position: relative;
left: -10px;
}
.menubar-sub-li {
line-height: 14px;
padding-top: 5px;
padding-left: 10px;
}
.menubar-subsub {
display: none;
}
You can position the submenu below the main menu button by setting the main menu button to position: relative and placing the (absolute) submenu inside it. You then show/hide the submenu by toggling overflow: hidden and visible.
Here is a demo: http://codepen.io/seraphzz/pen/osGnh
I want a parent class (ul tag) to hold a bunch of li tags to that overlap each other, here is my code:
<ul class="overlap">
<li>
Overlap One
</li>
<li>
Overlap Two
</li>
<li>
Overlap Three
</li>
</ul>
I don't want them to be positioned absolutely, because I have a parent element that has a background border and color, but when I position the children absolutely, the parent doesn't stretch, here is the code in its entirety.
<blockquote>
<ul class="overlap">
<li>
Overlap One
</li>
<li>
Overlap Two
</li>
<li>
Overlap Three
</li>
</ul>
</blockquote>
The blockquote tag has a background color on to it, that is why the children cannot be positioned absolutely.
Thanks in advance for the help!
Like this? http://jsfiddle.net/2N8Jz/
<blockquote class="clearfix">
<ul class="overlap">
<li>Overlap One</li>
<li>Overlap Two</li>
<li>Overlap Three</li>
</ul>
</blockquote>
blockquote {
border: 1px solid black;
padding: 10px;
}
.overlap {
padding-left: 10px;
}
.overlap li {
border: 1px solid grey;
float: left;
margin: 0 0 0 -10px;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
Giving the <li>'s a negative margin should do the trick.
here's an example: http://jsfiddle.net/wvEms/1/
basically i have div with width of 960px and it has navigation on right hand, the navigation works fine, just the last sub navigation shows content extended to 960px, so i was wondering if somehow i can push it to left side, without pushing top navigation with it, i been trying but it dont work. following is the example and css code
navigation, the last black line is the
end of 960px;
screenshot for navigation question two
(question from comment)
css code example
#topNav {
float: right;
position: relative;
}
#topNav li {
position: relative;
float: left;
line-height:1.5em;
padding: 0 .5em;
}
#topNav a span {
clear: both;
}
#topNav ul ul {
display:none;
position: absolute;
top: 20px;
right: 0;
}
#topNav ul ul li {
float:none;
white-space:nowrap;
position: relative;
margin:0;
}
#topNav li.active {
height: 50px;
background-color:#FFF;
border-top:#666 1px solid;
border-right:#666 1px solid;
border-left:#666 1px solid;
border-bottom:#FFF 1px solid;
z-index: 2;
}
#topNav ul ul.active {
display: block;
background-color:#F8F8F8;
border:#666 1px solid;
margin-top: -1px;
margin-right: -1px;
z-index: 1;
}
html 5 code
<nav id="topNav">
<ul>
<li class="active">Home</li>
<li>Everyone</li>
<li>Profile</li>
<li><a href="#" title="Account" >Account</a>
<ul>
<li>Edit Friends</li>
<li>Account Settings</li>
<li>Privacy Settings</li>
<li>Help Center</li>
<li>Logout</li>
</ul>
</li>
<li>Layouts
<ul class="active">
<li>Default Layout</li>
<li>Default Elements</li>
<li>Default Form</li>
<li>Media Detail</li>
</ul>
</li>
</ul>
</nav>
Try adding the following:
#topNav ul ul {
right: 0;
}
Rather than left: 0;. This should make it line up with the right hand side of the top menu item (Layouts in this case), rather than the left hand side.