CSS dropdown menu jumping - html

I'm stuck with a drop down menu. The problem is that the "parent" link is jumping.
HTML:
<ul id="nav">
<li><span>Page 1</span>
<ul>
<li><a>Extralong Page 1.1</a></li>
<li><a>Page 1.2</a></li>
<li><a>Page 1.3</a></li>
</ul>
</li>
<li>Page 2</li>
<li><span>Page 3</span>
<ul>
<li><a>Page 3.1</a></li>
<li><a>Long description for page 3.2</a></li>
<li><a>Page 3.3</a></li>
<li><a>Page 3.4</a></li>
</ul>
</li>
</ul>
CSS:
* {
margin: 0;
padding: 0;
}
#nav {
float: right;
}
#nav ul {
float: left;
}
#nav li {
float: left;
padding-top: 2px;
list-style: none;
background: #3451ff;
}
#nav li a,
#nav li span {
display: block;
text-decoration: none;
font-size: 12pt;
font-weight: bold;
padding: 13px 10px 9px 10px;
}
#nav li a:link, #nav li a:active, #nav li a:visited,
#nav li span {
color: #FFF;
}
#nav li a:hover, #nav li a.active,
#nav li span:hover {
color: #000;
}
#nav li li {
display: none;
}
#nav li:hover li {
display: block;
float: none;
background: #555;
}
#nav li li a {
font-size: 10pt;
margin: 1px;
width: 100%;
background: #3c6f3a;
padding: 5px 0;
}
Demo
How can I make this parent static such that it's width isn't changed on hover? I don't want to use js.

Set a width for the parent:
#nav li {
float: left;
padding-top: 2px;
list-style: none;
width: 100px; /* Add this bad boy */
background: #3451ff;
}
DEMO HERE

You can use position:absolute and make its parent li position:relative Demo
#nav ul {
position:absolute;
right:0;
}

Use position:relative for ul parent and give position:absolute for parent child..it will be working fine.
Here is the updated fiddle :http://jsfiddle.net/nikhilvkd/gmU55/5/
#nav li li {
display: none;
position:relative;
}
#nav li:hover li {
display: block;
float: none;
position:absolute;
background: #555;
}

Related

Navbar deconnexion button hover on two lines not wanted

I would like a deconnexion button to appear when hovering on the menu "Bonjour Toi".
But it's showed on 2 lines instead of 1.
As "Toi" can be changed according to user name, when the name is longer the menu deconnexion is correctly showed on 1 line.
Here is what i have now:
Here is my html code:
.nav-top {
background-color: #475162;
height: 70px;
width: 100%;
text-align:center;
padding:10px;
}
.nav-top nav .logo{
float:left;
}
.nav-top nav .logo{
margin-left:20px;
}
.nav-top nav ul {
padding: 0px;
margin: 0px;
float: right;
}
.nav-top nav ul {
margin-right: 50px;
}
.nav-top nav li {
display: inline;
}
.nav-top nav li a {
color: white;
font-size: 1em;
line-height: 70px;
padding: 5px 15px;
cursor: pointer;
font-family: Raleway, arial;
}
.nav-top ul li a.active {
border: solid 1px #FF307E;
}
.nav-top ul li a:hover:not(.active) {
border: solid 1px #FF307E;
}
/* BUTTON DECONNEXION */
.nav-top nav ul li{
display:inline-block;
position:relative;
}
.nav-top nav ul li ul{
position:absolute;
z-index: 1000;
max-height:0;
left: 0;
right: 0;
overflow:hidden;
}
.nav-top nav ul li:hover ul{
max-height:15em;
}
.nav-top nav ul li ul a{
padding:8px 5px;
text-decoration: none;
}
.nav-top nav ul li ul a img{
vertical-align:middle;
}
.nav-top nav ul li:hover li a{
background-color: #FF307E;
color:white;
text-transform:inherit;
}
<div class="nav-top">
<nav>
<div>
<div>
<a routerLink='./'><img class="logo" src="./assets/img/logo.png" height="70px"></a>
<ul>
<li><a>BLOG</a></li>
<li><a>CONTACT</a></li>
<li><a>SUPPORT</a></li>
<li ><a class="active ">Bonjour {{nameUserConnected}}</a>
<ul>
<li><a (click)="confirmLogout()"><img src="./assets/img/logout.png" width="17px" /> Déconnexion</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
How can I get the deconnexion + icon on the same line ?
You have a few problems here. The ul that is positioned absolute will have the width of the element it's relative to. Which is the li containing Bonjour Toi . That's why when it's longer, it will fit. If the text is smaller , the ul won't fit. You also set overflow:hidden on it , you need to remove that
I changed a bit your code ( the image i've set it like a background-image and padding-left of a is equal to the width of image, change it as you like )
All new/changed code is at the top of the CSS styles
see below
.nav-top nav ul li ul {
position: absolute;
z-index: 1000;
max-height: 0;
left: 0;
top: 100%;
display: none;
}
.nav-top nav ul li ul li a {
padding-left: 30px;
background: url('https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico') no-repeat scroll left center #FF307E;
}
.nav-top nav ul li ul {
width: 100%;
display: none;
}
.nav-top nav ul li:hover > ul {
display: block;
overflow: visible;
}
.nav-top {
background-color: #475162;
height: 70px;
width: 100%;
text-align: center;
padding: 10px;
}
.nav-top nav .logo {
float: left;
}
.nav-top nav .logo {
margin-left: 20px;
}
.nav-top nav ul {
padding: 0px;
margin: 0px;
float: right;
}
.nav-top nav ul {
margin-right: 50px;
}
.nav-top nav li {
display: inline;
}
.nav-top nav li a {
color: white;
font-size: 1em;
line-height: 70px;
padding: 5px 15px;
cursor: pointer;
font-family: Raleway, arial;
}
.nav-top ul li a.active {
border: solid 1px #FF307E;
}
.nav-top ul li a:hover:not(.active) {
border: solid 1px #FF307E;
}
/* BUTTON DECONNEXION */
.nav-top nav ul li {
display: inline-block;
position: relative;
}
.nav-top nav ul li:hover ul {
max-height: 15em;
}
.nav-top nav ul li ul a {
padding: 8px 5px;
text-decoration: none;
}
.nav-top nav ul li ul a img {
vertical-align: middle;
}
.nav-top nav ul li:hover li a {
background-color: #FF307E;
color: white;
text-transform: inherit;
}
<div class="nav-top">
<nav>
<div>
<a routerLink='./'><img class="logo" src="./assets/img/logo.png" height="70px"></a>
<ul>
<li><a>BLOG</a></li>
<li><a>CONTACT</a></li>
<li><a>SUPPORT</a></li>
<li><a class="active ">Bonjour Toi</a>
<ul>
<li><a (click)="confirmLogout()">Déconnexion</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>

Changing colour of link in list without altering hover CSS

This menu bar works as it should, until hyperlinks are involved, as the browser inserts its own text formatting.
I tried using the pseudo selectors (a:link a:visited) to counteract this, but that prevents the styling I have already created from showing, (as I want the text to change from grey to white upon hover). I also tried #menubar ul li a:link{} but didn't work. How do I prevent the links from changing colour when they are in lists?
Fiddle here: http://jsfiddle.net/CWB9C/1/
HTML:
<div id="menubar">
<ul>
<li> Home
</li>
<li>Facebook
<ul>
<li>One
</li>
<li>Two
</li>
<li>Three
</li>
</ul>
</li>
<li>Google.com
<ul>
<li>One
</li>
<li>Two
</li>
<li>Three
</li>
</ul>
</li>
<li>Search</li>
</ul>
</div>
CSS:
body {
text-align: center;
}
#menubar ul{
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
list-style: none;
}
#menubar ul li{
font: 18px;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #fff;
color:#666;
text-decoration:none;
}
#menubar ul li{
font: 18px;
font-family: latolight;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #fff;
}
#menubar ul li:hover {
background: #A03C3A;
color: #D6D6D6;
}
#menubar ul li ul{
padding: 0;
position: absolute;
top: 43px;
left: 0;
width: 150px;
box-shadow: none;
display: none;
opacity: 0;
visibility: hidden;
color:#666;
text-decoration:none;
}
#menubar ul li ul {
padding: 0;
position: absolute;
top: 43px;
left: 0;
width: 150px;
display: none;
opacity: 0;
visibility: hidden;
}
#menubar ul li ul li {
background:#A03C3A;
display: block;
color: #FFF;
}
#menubar ul li ul li {
background:#A03C3A;
display: block;
color: #FFF;
text-shadow: 0 -1px 0 #000;
z-index:10;
color:#666;
text-decoration:none;
}
#menubar ul li ul li:hover {
background:#4F529F; z-index:10;
}
#menubar ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
z-index:10;
}
Style the a's not li's, or just set to all the a's
a { color:inherit; text-decoration:none; }
Just create a style that will apply to both li and li > a:
#menubar ul li, #menubar ul li a {
color:#666;
font: 18px;
font-family: latolight;
text-decoration: none;
/* Add whatever additional style you want */
}
jsFiddle
From what I'm reading, I think this is what you want
#menubar a {
color: #whatevershadeofgrayhere;
text-decoration: none;
}
#menubar a:hover {
color: #whatevershadeofwhitehere;
}
#menubar ul li:hover a{
color:#fff;
}
You can use the inherit value for color.
#menubar ul li a {
color: inherit;
}
Then it will inherit from the closest parent with a color style. You can then do something like this for the colors.
#menubar ul li ul li {
color: black;
}
fiddle
(nice menu by the way)

Targeting an element by hovering another element in CSS

How can I target one element on hover and then effect an other element?
Here's the HTML:
<header>
<h1 style="float: left; margin-top: 2%;"> Naughty Mama.</h1>
<nav>
<ul>
<li>My Profile</li>
<li>Inbox</li>
<li>Notifications</li>
</ul>
</nav>
</header>
Here's the CSS:
#CHARSET "ISO-8859-1";
header {
background-color: #ddd;
}
nav {
float: right;
margin-right: 5%;
margin-top: 2%;
}
nav ul li{
display: inline;
padding: 15px;
background-color: #eee;
}
nav ul li a {
color: #fff;
text-decoration: none;
}
nav ul li a:hover {
color: #000;
}
I want to effect the nav ul li when I hover on nav ul li a.
Hope I am clear.
You may style <a> so it covers <li>. apply hover effect as background to <a> :)
http://codepen.io/anon/pen/zyDLA
header {
background-color: #ddd;
}
nav {
float: right;
margin-right: 5%;
margin-top: 2%;
}
nav ul li{
display: inline-block;
background-color: #eee;
vertical-align:top;
}
nav ul li a {
color: #fff;
text-decoration: none;
display:block;
padding:15px;
}
nav ul li a:hover {
color: #000;
background:#48a
}

Sublevels in Drop Down Menu Stack on top of One Another Instead of Displaying Verticly

Recently a few months ago I had to add sublevel functionality into a drop down menu on one of our sites. The tactic I took before worked well for the one column in the navigation, but I was asked to add a sublevel to the column before it which didn't work because I was using relative positioning (see the example below):
<style type="text/css">
#div#mycontent { overflow: visible; }
#nav ul { font-family: Arial, Verdana; font-size: 10px; margin: 0; padding: 0; list-style: none; font-weight: bold; }
#nav ul li { display: block; float: left; margin: 0;}
#nav li ul { display: none; }
#nav ul li a { display: block; text-decoration: none; color: #3c1c4e; border-top: 1px solid #ffffff; padding: 5px 15px 5px 15px; background: #f0e8d8; margin-left: 1px; white-space: nowrap; }
#nav ul li a:hover { background: #f0e8d8; }
#nav li:hover ul { display: block; position: absolute; }
#nav li:hover li { float: none; font-size: 11px; }
#nav li:hover a { background: #f0e8d8; }
#nav li:hover li a:hover { background: #fff7e7; }
/* This is for sublevels in the drop down */
#nav li:hover ul li ul {display: none}
#nav li ul li:hover ul { display: block; }
#nav li ul li ul li { position: relative; left: 188px; bottom:25px ;padding-left:1px }
So I modified the sublevels in the drop down menu to use relative positioning used an overlap approach (due to the way to previous coder originally designed the drop down). The new code looks like the one below:
#nav li ul li ul li { position: absolute; left: 125px; bottom: 0px; border-style: solid; border-width: 1px; border-color:purple; z-index: 1; }
However as the title indicates the LI under the unordered list are now stacking on top of one another. Instead of displaying vertically one after the other. I believe it requires me to clear the float, but it looks like it was done up above. So I'm unsure if I need to redefine the float then clear it in order to make sure the links in the sub list will display vertically.
Edit:
A good thought to add the HTML to show how I'm trying to execute this:
<html>
<head>
<style type="text/css">
#div#mycontent { overflow: visible; }
#nav ul { font-family: Arial, Verdana; font-size: 10px; margin: 0; padding: 0; list-style: none; font-weight: bold; }
#nav ul li { display: block; float: left; margin: 0;}
#nav li ul { display: none; }
#nav ul li a { display: block; text-decoration: none; color: #3c1c4e; border-top: 1px solid #ffffff; padding: 5px 15px 5px 15px; background: #f0e8d8; margin-left: 1px; white-space: nowrap; }
#nav ul li a:hover { background: #f0e8d8; }
#nav li:hover ul { display: block; position: absolute; z-index: 0;}
#nav li:hover li { float: none; font-size: 11px; }
#nav li:hover a { background: #f0e8d8; }
#nav li:hover li a:hover { background: #fff7e7; }
/* This is for sublevels in the drop down */
#nav li:hover ul li ul {display: none}
#nav li ul li:hover ul { display: block; }
#nav li ul li ul li { position: absolute; left: 125px; bottom: 0px; border-style: solid; border-width: 1px; border-color:purple; z-index: 1; }
</style>
</head>
<body>
<div id="nav">
<ul id="menu">
<li>Column 1
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</li>
<li> Column 2
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</li>
<li>Column 3<li>
</ul>
</div>
</body>
</html>
Try these CSS rules for your sublevels in the drop down:
/* This is for sublevels in the drop down */
#nav li:hover ul li ul {
display: none
}
#nav li ul li:hover ul {
display: block;
position:absolute;
top:0;
left:100%;
}
#nav li ul li ul li {
position:relative;
display: block;
float: left;
border: 1px solid purple;
z-index: 1;
}

CSS Menu hover effect

I'm trying to create a hover effect in my drop down menu but I can't figure out how to do it.
What I want to do is this:
instead of this:
My Code - You can also see it here, (updated version)
HTML:
<nav>
<ul>
<li>One
<ul>
<li>1.1</li>
<li>1.2
</ul>
</li>
<li>Two
<ul>
<li>2.1</li>
<li>2.2</li>
<li>2.3</li>
</ul>
</li>
<li>Three
<ul>
<li>3.1</li>
<li>3.2</li>
</ul>
</li>
<li>Four</li>
</ul>
</nav>
CSS:
nav {
float: left;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content:"";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
}
nav ul li:hover {
background-color: #000;
}
nav ul li a:hover {
color: #fff;
}
nav ul li a {
display: block;
padding: 25px 15px;
color: #6F0;
text-decoration: none;
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #000;
}
Any help will be appreciated.
Working fiddle:
Change the styles for the below two:
nav ul li:hover {
border-bottom: 1px solid #000;
color: #000;
}
nav ul li a:hover {
color: #000;
}
And add:
nav ul li:hover li:hover{
background: #000;
}
In order to style the sub-menus.
The first (li:hover) you want to set a bottom border - you can change the width of this border from 1px to something more thick, say, 3px