How to remove hardcoded widths and offsets from vertical drop-down menu - css

After following a couple of tutorials I have managed to build up a CSS-only vertical drop-down menu. However, the widths and absolute offsets are hardcoded and I cannot get them to adjust automatically according to their contents. I wish to avoid hardcoding these because I wish to integrate it into a CMS where I don't know the actual lengths of the menu items.
I have created a JSFiddle here showing the menu working: http://jsfiddle.net/nhfHw/2/
The top level items are currently hardcoded to a width of 100px (I wish to make this adjust according to the longest item at that level.) When I tried to remove that, it just expanded the next sub-level all over the screen.
#navigation
{
font-family: Helvetica, Arial, sans-serif;
font-size: 14px;
color: #707070;
line-height: 20px;
width: 100px; /* I wish to remove this */
margin-top: 30px;
}
The x offset of the sub-levels is also hard-coded. I wish them to just adjust according to their parent's width. Their width is also hardcoded to 200px.
li:hover .sub-level
{
background: #D0D0D0;
display: block;
position: absolute;
left: 100px; /* I wish to remove this */
top: 0px;
}
li:hover .sub-level .sub-level
{
left: 210px; /* I wish to remove this */
top: 0px;
}
ul.sub-level li
{
border: none;
float:left;
width: 200px; /* I wish to remove this */
}
I wish to avoid Javascript if possible.

A combination of display: inline-block and white-space: nowrap I believe gets you what you're looking for: http://jsfiddle.net/nhfHw/14/
#nav {
display: inline-block;
font-family: Helvetica, Arial, sans-serif;
font-size: 14px;
color: #707070;
line-height: 20px;
margin-top: 30px;
}
#nav ul li {
padding: 1px 5px;
list-style: none;
white-space: nowrap;
display: block;
position: relative;
}
#nav ul li:hover {
background: #E0E0E0;
}
#nav ul li a {
text-decoration: none;
color: #707070;
display: block;
}
#nav ul ul {
display: none;
border-left: 1px solid #ccc;
position: absolute;
left: 100%;
top: 0px;
}
#nav ul li:hover > ul {
display: block;
}

Here just what the answer above me did, but without bugs...
http://jsfiddle.net/techsin/UBgZf/2/ Updated
*{padding:0;margin:0;}
.main{position:relative;}
ul ul{
display:none;
position:absolute;
left:100%;
margin-top:-30px;
}
.main>li>ul{
background-color:#BDBDBD;
}
ul {
white-space: nowrap;
color:white;
background-color:gray;
float:left;
}
li{
height:20px;
list-style-type:none;
padding:10px;
clear:both;
}
li:hover>ul{
background-color:black;
display:block;
}
Tricks include:
White Space No wrap
float
absolute positioning so it always refer to first element that has been positioned either absolute or relative.

Related

z-index property doesn't work on hover menu links

I made a menu bar, and custom effect on hover. I wanted the link text to be on top layer. Problem is that when I hover the link in menu, the triangle overlays the text as shown in example, even though I set the z-index of a link to 999.
<div id="menu">
<ul>
<li>yyyyyy</li>
<li>ppppppp</li>
<li>ggggggg</li>
<li>jjjjjjjj</li>
</ul>
Jsfiddle:
https://jsfiddle.net/z5zLL1kn/
#menu{
height:50px;
background-color:#fff8dc;
border-bottom:1px #ff8888;}
ul {
list-style-type: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
height:50px;
background-color: #fff8dc;
width:450px;
}
li { float: left; }
li a {
font-family: 'Quicksand', sans-serif;
font-size: 16px;
font-weight: 400;
line-height: 44px;
display: block;
color: #ff1636;
text-align: center;
border-bottom:1px #ff8888;
text-decoration: none;
height:49px;
position: relative;
padding-left:15px;
padding-right:15px;
z-index:100;
}
li a:hover:after {
content: "";
display: block;
border: 10px solid #fff8dc;
border-bottom-color: #ff8888;
position: absolute;
bottom: 0px;
left: 50%;
margin-left: -10px;
margin-bottom:1px;
}
Your problem is that the ::after pseudo element is considered a part of the a element, and so changing the z-index of the anchor, will also apply to the pseudo element.
A quick solution would be to move the arrow pseudo element to the list item instead of the link.
Working jsFiddle
li {
position: relative;
}
li a {
position: relative;
z-index:2;
}
li:hover::after {
position: absolute;
}
Another solution:
As Roko C. Buljan mentioned in the comments, a more straight-forward solution can be to build the arrow pseudo element properly (the second border color needs to be transparent, instead of the background's color:
li a:hover:after {
border: 10px solid transparent;
border-bottom-color: #ff8888;
}
Working jsFiddle

Nav inside Header, Position Fixed?

I have an header with 100% width, and a nav inside header with 980px
Now i want to give the position as fixed for both header and nav.
I have tried with the following code but could'nt get what i wanted to
Please help me,
my header.css
width:100%;
height:60px;
background: #ffffff;
position:fixed;
z-index:999;`
and my nav.css
background: #ffffff;
height: 60px;
text-align:center;
position:fixed;
z-index:99;
.nav ul
margin:0;
padding:0;
.nav li
display: inline-block;
list-style-type: none;
vertical-align:middle;`
.nav li a
font-size: 16px;
color: black;
display: block;
line-height: 60px;
padding: 0 10px;
text-decoration: none;
If the nav is inside the header you don't need position:fixed in your nav.css, you should also remove the z-index. A clearer description of the problem and the html you're using would be helpful if that doesn't help.
#Fastnto, it's something like this that you want?
http://jsfiddle.net/alexandrecanijo/NBp8F/
I've changed some parts of your original CSS in order to show the header (#ccccccc) and nav (#000000) and added the .content with enough lorem ipsum so that you are able to see the nav.
But, the CSS might be cleaned and refactored in some parts... Didn't had a change to do this...
Hope this helps.
html,body, p {
margin: 0;
padding: 0;
border: 0;
font: 14px arial;
}
.header {
width: 100%;
height: 80px;
background: #cccccc;
position: fixed;
z-index:999;
margin: 0;
clear: both;
top: 0;
}
.nav {
background: #000000;
height: 60px;
text-align:center;
z-index:99;
}
.nav ul {
margin:0;
padding:0;
}
.nav li {
float: left;
list-style-type: none;
}
.nav li a {
font-size: 16px;
color: #fe6700;
display: block;
line-height: 60px;
padding: 0 10px;
text-decoration: none;
}
.nav li a:hover {
color: #000000;
background: #fe6700;
}
.content {
margin-top: 80px;
}

Stop CSS navbar from scaling with position:relative

I'm using a CSS navbar that has drop down menus. I had a problem with scaling on my site, but some helpful users here helped me fix it. The solution they gave me was to change the position attribute to absolute.
This works fine for text boxes and images, but changing the navbar code to absolute breaks it and makes some of the buttons go to a second line.
Is there a way to stop this from happening and also stop the navbar from bugging out when the page is resized? Sorry if this is hard to understand. This is my navbar CSS, the HTML is just a list:
Jsfiddle: http://jsfiddle.net/qN8sm/embedded/result/
ul {
font-family: 'Open Sans', Times;
font-size: 14px;
margin: 0;
padding: 0;
list-style: none;
position: relative;
z-index: 150;
}
ul li {
display: block;
position: relative;
float: right;
}
li ul { display: none; }
ul li a {
display: block;
text-decoration: none;
color: #ffffff;
border-top: 7px solid #CC4D4D;
padding: 25px 30px 30px 30px;
background: #333333;
margin-left: 0px;
white-space: nowrap;
}
ul li a:hover { background: #757575; }
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #757575; }
li:hover li a:hover { background: #757575; }
What you could try is set min-width:(amount of pixels)px. This sets the minimum width for an element, but if unless you set a max-width, it can upscale.

How do I center the navigation bar in my website?

I'm using Weebly to create a website, and I've been trying to modify the navigation bar so that it appears centered rather than left justified. I don't really know any code at all, but I found the navigation code, and I was wondering what I could change to center the bar. Thanks!
#nav-wrap .nav {
float:left;
}
#nav-wrap .container {
clear: both;
overflow: hidden;
position: relative;
background:url(saperator-h.png) repeat-x bottom;
padding-bottom:40px;
}
#nav-wrap .container ul {
list-style: none;
}
#nav-wrap .container ul li {
list-style: none;
float: left;
background:url(nav-saperator.png) no-repeat right center;
margin-right: 10px;
padding-right: 25px;
}
#nav-wrap .container ul > li:last-child, #nav-wrap .container ul span:last-child li {
background:none;
}
#nav-wrap .container ul li a {
display: block;
line-height:14px;
border: 0;
outline: 0;
list-style-type: none;
text-transform:uppercase;
padding:5px;
margin-bottom:4px;
}
#nav-wrap .container ul li#active a,
#nav-wrap .container ul li a:hover {
color:#000;
}
#nav-wrap {
position: relative;
width: /*set width, can't be 100% obivously if you want it centered */
margin: auto;
}
This is a guess, we don't see any HTML , apply this to whatever is the container for the entire navbar
It depends on the HTML structure, but this could work.
#nav-wrap .container ul {
display:block;
width:auto;
margin:auto;
}
#topnav a {
float: left;
display: block;
color: #fff;
font-size: 1.2em;
text-decoration: none;
padding: 11px 60px 60px 15px;
border: 0;
outline: 0;
text-transform: uppercase;
i had to mess with the padding but this centered my nav bar in weebly

CSS drop up menu not displayed correctly

I'm having some trouble with changing a CSS drop down menu to a drop up menu. I think I'm almost there, but for some reason some of the buttons are not displayed correctly (the text is moving downwards, but the menu is moving upwards). See crisislab.nl for the code in action.
Any help would be much appreciated!
#navigation {
width: 980px;
height: 38px;
}
#navigation li {
float: left;
position: relative;
top: 220px;
}
#navigation li:hover {
background: transparent url(gfx/navigation_hover.png) repeat;
}
#navigation li a {
text-transform: uppercase;
color: white;
padding: 13px 33px;
line-height: 38px;
font-size: 11px;
}
#navigation li a:hover {
text-decoration: none;
}
#navigation li ul {
position: absolute;
background: transparent url(gfx/navigation_hover.png) left top repeat;
z-index: 1000;
min-width: 100%;
display:none;
left:-1px;
}
#navigation li:hover ul {
bottom: 38px;
display:block;
}
#navigation li ul li {
background: none;
width: 100%;
}
#navigation li ul li:hover {
background: none;
background-color: #2a51b5;
}
#navigation li ul li a {
text-transform: uppercase;
color: white;
padding-left: 8px 10px;
line-height: 28px;
width: 100%;
display:block;
}
When having a hard time finding bugs, always replicate and break the code into smallest simplest chunks.
Hope this would help: http://jsfiddle.net/ccS7q/
But you wouldn't be able to achieve drop up menu with the sublists listing upwards unless you use jquery or javascript with it. The fiddle above can't do listing upwards, you could though adjust manually the ul li.menu-item ul top value as the listing lengthens. Though its a lot of work. I would advise you to use jquery instead.
Remove that bottom: 38px; from below code:
#navigation li:hover ul {
bottom: 38px;/*Just Remove This*/
display: block;
}
Add top:0 to #navigation ul li ul li
#navigation li ul li {
background: none repeat scroll 0 0 transparent;
top: 0;/*Add This*/
width: 100%;
}
I think this will help you.

Resources