Hover problems nav bar (HTML/CSS) - html

I've made a nav bar and I'd like to use the hover selector only on the pages that aren't active. So I used the selector a:not(.active):hover but it doesn't work. I'd really appreciate if someone could help me.
ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display:block;
position:absolute;
top:-2px;
left:0;
right: 0;
background-color: darkred;
}
li
{
float: left;
}
li a
{
display: block;
color: white;
text-align: center;
padding: 20px 23px;
text-decoration: none;
}
li a:not(.active):hover
{
background-color: #B22222;
}
.active {
background-color: #470005;
}
<ul>
<li class="active">Home</li>
<li>About me</li>
<li>Contacts</li>
<li>Help</li>
<li>Other Works</li>
<li>News</li>
</ul>

Your :not pseudo-class is on your link. However, the active class is on the li.
li:not(.active) a:hover should work
JSfiddle Example: https://jsfiddle.net/ubntkk46/

The class is linked to your li element, not the hyperlink
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display: block;
position: absolute;
top: -2px;
left: 0;
right: 0;
background-color: darkred;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 20px 23px;
text-decoration: none;
}
li:not(.active) a:hover {
background-color: #B22222;
}
.active {
background-color: #470005;
}
<ul>
<li class="active">Home</li>
<li>About me</li>
<li>Contacts</li>
<li>Help</li>
<li>Other Works</li>
<li>News</li>
</ul>

You could just apply the same background colour to the active a tag and the active a tags hover state, and a different background colour to the the non active a tags hover states. This would allow it to work in IE8, because :not isn't supported in older versions of IE.
ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display:block;
position:absolute;
top:-2px;
left:0;
right: 0;
background-color: darkred;
}
li
{
float: left;
}
li a
{
display: block;
color: white;
text-align: center;
padding: 20px 23px;
text-decoration: none;
}
li a:hover {
background-color: #B22222;
}
li.active a, li.active a:hover {
background-color: #470005;
}
<ul>
<li class="active">Home</li>
<li>About me</li>
<li>Contacts</li>
<li>Help</li>
<li>Other Works</li>
<li>News</li>
</ul>

This Fix It :
li:not(.active):hover a {
}
Full Code:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display:block;
position:absolute;
top:-2px;
left:0;
right: 0;
background-color: darkred;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 20px 23px;
text-decoration: none;
}
li:not(.active):hover a {
background-color: #B22222;
}
.active {
background-color: #470005;
}
<ul>
<li class="active">Home</li>
<li>About me</li>
<li>Contacts</li>
<li>Help</li>
<li>Other Works</li>
<li>News</li>
</ul>

Related

The submenu doesn't show up when hover the mouse

I have a problem. I don't know what wrong with the code as when I hover the mouse over 'About us', the submenu didn't show up. I did make the submenu display:none and then when hover, it display: block but nothing happen. Thanks for your help
#nav_menu{
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#nav_menu li{
float: left;
}
#nav_menu li a{
display: block;
width: 160px;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #800000;
color: white;
font-weight: bold;
text-align: center;
}
#nav_menu .current{
color: yellow;
}
#nav_menu #sub_menu{
display: none;
position: absolute;
top: 100%;
list-style: none;
}
#nav_menu #sub_menu:hover{
display: block;
}
#nav_menu #sub_menu li a{
float: right;
}
<ul id="nav_menu">
<li>Home</li>
<li>Speakers</li>
<li>Luncheons</li>
<li>Tickets</li>
<li>About Us
<ul id="sub_menu">
<li>Our History</li>
<li>Board of Directors</li>
<li>Past Speakers</li>
<li>Contact Information</li>
</ul>
</li>
</ul>
My method is a little different and shorter with the animation opening and closing for the submenu, I used the scale instead of display for animation;
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
#nav_menu{
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#nav_menu li{
float: left;
position: relative;
}
#nav_menu li a{
display: block;
width: 160px;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #800000;
color: white;
font-weight: bold;
text-align: center;
}
#nav_menu .current{
color: yellow;
}
#nav_menu #sub_menu{
position: absolute;
top: 100%;
list-style: none;
padding: 0;
transform : scaleY(0);
transform-origin: top;
transition: all 0.4s;
}
#nav_menu li:hover #sub_menu{
transform: scaleY(1);
}
#nav_menu #sub_menu li a{
float: right;
}
<ul id="nav_menu">
<li>Home</li>
<li>Speakers</li>
<li>Luncheons</li>
<li>Tickets</li>
<li>About Us
<ul id="sub_menu">
<li>Our History</li>
<li>Board of Directors</li>
<li>Past Speakers</li>
<li>Contact Information</li>
</ul>
</li>
</ul>
To display a submenu when hovering, you need to write like this:
#nav_menu li:hover #sub_menu {
display: block;
}
Because you are accessing the #sub_menu selector. Also, you need to add position: relative the selector #nav_menu li is relevant for the correctness of your position: absolute of the selector #nav_menu #sub_menu. And add padding: 0 for the selector of the same selector #nav_menu #sub_menu, in order to align it under the main menu.
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
#nav_menu{
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
#nav_menu li{
float: left;
position: relative;
}
#nav_menu li a{
display: block;
width: 160px;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
background-color: #800000;
color: white;
font-weight: bold;
text-align: center;
}
#nav_menu .current{
color: yellow;
}
#nav_menu #sub_menu{
display: none;
position: absolute;
top: 100%;
list-style: none;
padding: 0;
}
/*#nav_menu #sub_menu:hover{
display: block;
}*/
#nav_menu li:hover #sub_menu{
display: block;
}
#nav_menu #sub_menu li a{
float: right;
}
<ul id="nav_menu">
<li>Home</li>
<li>Speakers</li>
<li>Luncheons</li>
<li>Tickets</li>
<li>About Us
<ul id="sub_menu">
<li>Our History</li>
<li>Board of Directors</li>
<li>Past Speakers</li>
<li>Contact Information</li>
</ul>
</li>
</ul>

CSS for direction of list elements in nested Drop-Down Menu?

I don't know how to change the code to handle arbitrary nested items? I have no clue how to this to apply for multiple levels. The following works just for 2 levels. But if i add a 3. level it fails. I think this can be done cleverly without writing manually each level?
I want that it can be adjusted to arbitrary many childs and it should be work with hover and clicks.
Thank you very much.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
.nav li:hover ul {
display:block;
}
.nav ul li ul li {
display: block;
float:left; /* newly added */
height:auto; /* newly added */
line-height:34px; /* newly added */
}
<div class="nav"> <!-- Start of Nav Bar -->
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Is this something you wanted? I just added a > in .nav li:hover ul.
See comments in code below. ZZZZ, XXXX, and YYYY are added by me.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
padding: 0.5rem;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
/*.nav li:hover ul { OLD CODE
display:block;
}*/
.nav li:hover > ul { /* ADDED the charcter '>' */
display:block;
}
.nav ul li ul li {
display: block;
float:left;
height:auto;
line-height:34px;
}
<div class="nav">
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>ZZZZ
<ul> <!-- ADDED -->
<li>XXXX</li>
<li>YYYY</li>
</ul>
</ul>
</li>
</ul>
</li>
</ul>
</div>
You can use flex-inline and > for this approach of making menus.
.nav ul {
list-style: none;
text-align: center;
padding: 0;
margin: 0;
}
.nav ul li {
font-family: 'Roboto', sans-serif;
border-bottom: none;
height: 86px;
line-height: 86px;
font-size: 14px;
display: inline-block;
float:left;
margin: 0 auto;
}
.nav ul li a {
text-decoration: none;
color:#000000;
display: block;
transition: .3s background-color;
padding:0 24px;
}
.nav ul li a:hover {
background-color: #5c89c7;
color:#FFFFFF;
}
.nav a {
border-bottom:none;
}
.nav li ul {
position:absolute;
display:none;
width:inherit;
text-align:left;
}
.nav li:hover > ul {
display: inline-flex !important;
}
.nav ul li ul li {
/*display: block;*/
float:left;
height:auto;
line-height:34px;
}
<div class="nav"> <!-- Start of Nav Bar -->
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SERVICES
<ul>
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>CONTACT US
<ul>
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul>
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
I think what you're looking for is a multi-level dropdown. This is easier to do with separate classes rather than all one class (nav). I rewrote it for the sake of organization, but the design tweaks are really up to you! When running this snippet, make sure to use full screen because StackOverflow's snippet will distort the display a little bit.
.menu1 li {
list-style: none;
text-align: center;
position: relative;
float: left;
height: 30px;
width: 150px;
background-color: white;
}
.menu1 li:hover {
background-color: #5c89c7;
}
.menu1 li:hover>ul {
display: inline;
}
.menu1 a {
border-bottom: none;
font-family: Roboto, sans-serif;
color: black;
text-decoration: none;
padding: 0 0 0 10px;
display: block;
line-height: 30px;
}
.menu1 a:hover {
color: white;
}
.menu2 {
position: absolute;
top: 30px;
left: 0;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.menu2>li {
position: relative;
height: 30px;
background: #999999;
}
.menu2>li:hover {
background: #CCCCCC;
}
.menu3 {
position: absolute;
top: 0;
right: -150px;
width: 150px;
list-style: none;
padding: 0;
margin: 0;
display: none;
}
.menu3>li {
height: 30px;
background: #999999;
}
.menu3>li:hover {
background: #CCCCCC;
}
<ul class="menu1">
<li>HOME</li>
<li>ABOUT</li>
<li>
SERVICES
<ul class="menu2">
<li>PROGRAMS</li>
<li>EVENTS</li>
</ul>
</li>
<li>RESOURCES</li>
<li>GET INVOLVED</li>
<li>
CONTACT US
<ul class="menu2">
<li>AAAA</li>
<li>BBB</li>
<li>CCCC
<ul class="menu3">
<li>OOOO</li>
<li>BBBB</li>
</ul>
</li>
</ul>
</li>

Html CSS Nav link active background space issue

Any one know how to do that active button with around space?
like this my sample image
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
You need to add some padding to your li elements, like the snippet below.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
padding: 10px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 10px 10px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>

Center Menu Bar Links

I'm trying to center the menu bar links. In the css every time I try to center it, it puts each link on a new line. So I make it float:center; and it makes each link a new line and just doesn't center it all. Any ideas?
body {
margin: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
<ul>
<li><a class="active" href="#home">Home</a>
</li>
<li>News
</li>
<li>Contact
</li>
<li>About
</li>
</ul>
use display:flex and justify-content:center in your ul (remove the float:left from li)
Float:center doesn't exist.
body {
margin: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
display:flex;
justify-content:center
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #4CAF50;
}
<ul>
<li><a class="active" href="#home">Home</a>
</li>
<li>News
</li>
<li>Contact
</li>
<li>About
</li>
</ul>
Just add text-align:center to your ul and display:inline-block; to your li like this:
ul {
text-align:center;
}
li {
display:inline-block;
}
Here's a jsFiddle with above codes: https://jsfiddle.net/AndrewL32/4jendh7j/

CSS Dropdown menu/container?

I'm planning to do a new sort of dropdown menu style but I'm unsure of how to do it, I want it so that it basically dropsdown a container.
I've kinda marked it out, but I'm unsure on how to do it?
HTML:
<div id="header">
<div class="nav">
<ul>
<li>Home</li>
<li>Community</li>
<li>Staff</li>
<li>Shop</li>
<li>Enter</li>
</ul>
</div>
CSS:
a:visited, a:link, a:active {
text-decoration: none;
color: white;
}
#header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px;
background-color: #4a8fbc;
}
div.nav {
font-family: 'Lato', sans-serif;
font-size: 14px;
margin-left: 200px;
margin-right: 200px;
float: right;
color: white;
line-height: 50px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
margin: 0 30px 0 0;
}
li a:hover {
color: #256690;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
border: 1px solid #7d7d7d;
padding: 6px;
text-decoration: none;
background-color: #f2f2f2;
}
http://jsfiddle.net/UwRJ2/
This should get you a good start :
DEMO
HTML :
<div id="header">
<div class="nav">
<ul>
<li>Home
<ul>
<li>lala</li>
<li>lala</li>
<li>lala</li>
</ul>
</li>
<li>Community</li>
<li>Staff</li>
<li>Shop</li>
<li>Enter</li>
</ul>
</div>
</div>
CSS I added to your existing code + I also changed the display property on the li elements to display:inline-block; :
ul li > ul{
display:none;
border:1px solid red;
color:red;
position:absolute;
}
ul li:hover > ul{
display:block;
}