For a Bootstrap 4 website I've made a navbar with splitted dropdown buttons. The leftside of the dropdown button is a normal link and the rightside is a caret button to show the submenu.
For some reason I cannot find a working CSS way to have the hover effect only on the rightside (caret) and not on the leftside (link). Is there a CSS way (without Javascript) to do this?
Thanx in advance.
You'll find the jsfiddle on https://jsfiddle.net/bnsthp57/
Update
The hover is working! The updated snippet below is ready to use. Tested on Internet Explorer 11, Crhome 79, Opera 66, Edge 44, and Firefox 72.
#topnav .navbar-nav {
font-size: 0rem;
}
#topnav .nav-link {
display: inline-block;
padding: 0.625rem 1rem;
font-size: 1rem;
}
#topnav .dropdown-link {
padding: 0.625rem 0.25rem;
}
#topnav .dropdown-toggle {
padding-left: 0.25rem;
}
#media (max-width: 767.98px) {
#topnav .navbar-nav {
padding-top: 0.5rem;
}
#topnav .nav-link:first-child,
#topnav .dropdown-link {
display: block;
width: calc(100% - 50px);
padding: 0.625rem 1rem;
border-right: 1px solid #88d;
}
#topnav .dropdown-toggle {
display: block;
width: 50px;
padding: 0.625rem 0.5rem;
position: absolute;
top:0; right:0;
text-align: center;
}
}
#media (min-width: 768px) {
#topnav .nav-link {
border-right: 1px solid rgba(0, 0, 0, 0.1);
}
#topnav .dropdown-link,
#topnav .nav-item:last-child a.nav-link {
border-right: 0;
}
#topnav .dropdown-menu {
margin-top: 0;
}
#topnav .dropdown-toggle:hover + .dropdown-menu,
#topnav .dropdown-menu:hover {
display: block; /* open submenu on hover */
}
#topnav .dropdown-menu:not(:hover) {
display: none; /* close submenu on not hover */
}
}
/* navbar theme */
#navbar,
#navbar .navbar-toggler {
color: #fff;
background-color: #1c8cbc;
border-color: #1f90bb #1f90bb #145e7a;
}
#navbar .navbar-toggler-icon {
background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3e%3cpath stroke='rgba(255, 255, 255, 0.5)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
}
#topnav .nav-link {
color: #fff;
background-color: transparent;
font-weight: 600;
}
#topnav .nav-link:hover {
background-color: rgba(0, 0, 0, 0.1);
}
#topnav .dropdown-menu,
#topnav .dropdown-item,
#topnav .dropdown-item:focus {
color: #fff;
background-color: #0c7cac;
}
#topnav .dropdown-item.active {
color: #ff6;
background-color: #0c7cac;
font-weight: 600;
}
#topnav .dropdown-item:hover,
#topnav .dropdown-item:active {
color: #fff;
background-color: #1383af;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<nav id="navbar" class="navbar navbar-expand-md mr-auto">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#topnav" aria-controls="topnav" aria-expanded="false"
aria-label="Toggle menu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="topnav">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#pagelink">Home</a>
</li>
<li class="nav-item dropdown active">
<a class="nav-link dropdown-link" href="#pagelink">Magazine</a>
<a class="nav-link dropdown-toggle" href="#" id="navlink01" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></a>
<div class="dropdown-menu" aria-labelledby="navlink01">
<a class="dropdown-item" href="#pagelink">Sub item 1</a>
<a class="dropdown-item active" href="#pagelink">Sub item 2</a>
<a class="dropdown-item" href="#pagelink">Sub item 3</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php">Portfolio</a>
</li>
</ul>
</div>
</div>
</nav>
Navbar with splitted dropdown buttons
.dropdown-toggle:hover + .dropdown-menu{
display:block;
}
With this css it will work
#media (min-width: 768px) {
#topnav .dropdown-toggle:hover + .dropdown-menu,
#topnav .dropdown-menu:hover {
display: block;
}
#topnav .dropdown-menu:not(:hover) {
display: none;
}
}
on this dropdown nav-item
<li class="nav-item dropdown active">
<a class="nav-link dropdown-link" href="#pagelink">Magazine</a>
<a class="nav-link dropdown-toggle" href="#" id="navlink01" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></a>
<div class="dropdown-menu" aria-labelledby="navlink01">
<a class="dropdown-item" href="#pagelink">Sub item 1</a>
<a class="dropdown-item active" href="#pagelink">Sub item 2</a>
<a class="dropdown-item" href="#pagelink">Sub item 3</a>
</div>
</li>
Related
I want to change the background color of the active nav-item and of any nav-item hovered to "chartreuse" (just a color to test).
I've tried love of things but it's not working, why?
Not even using !important.
https://codepen.io/ogonzales/pen/abNQXLg
CSS:
/* NAVBAR TOP */
#top-navbar {
background-color: #1f1f1f;
}
.navbar-nav > .active > a {
color: aqua;
background-color: chartreuse;
}
.nav-item > a:hover {
color: aqua;
}
#navbarCollapse {
margin-left: 1%;
}
#navbarCollapse ul li {
display: inline-block;
}
.nav-link {
margin: 5px;
width: 100px;
}
/* start update */
nav ul {
margin-bottom: 0;
}
.navbar-light .navbar-toggler-icon {
background-image: none;
font-size: 25px;
height: auto;
width: auto;
padding: 3px;
}
#navbarCollapse ul {
padding-top: 10px
}
#navbarCollapse ul li a {
margin: 0;
padding: 5px 10px;
}
nav {
position: relative
}
nav form {
position: absolute;
top: 7px;
right: 15px;
}
Target the active nav-item like this:
.nav-item.active > a{
}
Then you can add css styling into it.
Before you weren't selecting the element properly.
What you desired:
.nav-item.active > a {
color: aqua;
}
.nav-item > a:hover {
background-color: chartreuse;
}
See codepen: https://codepen.io/koder613/pen/ZEWNEgw?editors=1100
To change the background color of the active nav-item. Call the navbar-nav class as it is in your ul tag as shown.
<div class="collapse navbar" id="navbarCollapse">
<!-- Call the navbar-nav class in the ul tag -->
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="/">Inicio</a>
</li>
<li class="nav-item">
<a href="/iglesias/" class="nav-link" >Iglesias</a>
</li>
<li class="nav-item">
<a href="/doctrina/" class="nav-link" >Doctrina</a>
</li>
</ul>
</div>
And the following will show the "chartreuse" color when you hover over the nav-items.
.nav-item > a:hover { color: aqua; background-color: chartreuse; }
I added active class on your second li to show how it looks like.
But more or less you should set your changing color in active class like:
.active{
background-color: red;
}
.active:hover > a{
color: yellow !important;
}
Like this both color will change when mouse is going hover your li with active class. And You add important where you want to make it stronger.
Pretty much duplicate of: Changing the color of active nav-item
$(document).ready(function() {
"use strict";
$('ul > li').click(function(e) {
e.preventDefault();
$('ul > li').removeClass('active');
$(this).addClass('active');
});
});
/* NAVBAR TOP */
#top-navbar {
background-color: #1f1f1f;
}
.navbar-nav > .active > a {
color: aqua;
background-color: chartreuse;
}
.active{
background-color: red;
}
.nav-item.active:hover > a{
color: yellow !important;
}
.nav-item > a:hover {
color: aqua;
}
#navbarCollapse {
margin-left: 1%;
}
#navbarCollapse ul li {
display: inline-block;
}
.nav-link {
margin: 5px;
width: 100px;
}
/* start update */
nav ul {
margin-bottom: 0;
}
.navbar-light .navbar-toggler-icon {
background-image: none;
font-size: 25px;
height: auto;
width: auto;
padding: 3px;
}
#navbarCollapse ul {
padding-top: 10px
}
#navbarCollapse ul li a {
margin: 0;
padding: 5px 10px;
}
nav {
position: relative
}
nav form {
position: absolute;
top: 7px;
right: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<!--MENU SUPERIOR-->
<!-- Fixed navbar -->
<nav class="navbar navbar-expand-md navbar-light fixed-top" id="top-navbar">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<!-- <span class="navbar-toggler-icon"><i class="fas fa-bars"></i></span> -->
<span class="navbar-toggler-icon">
<i class="fas fa-bars" style="color:#fff; font-size:28px;"></i>
</span>
</button>
<a class="navbar-brand mr-auto mt-0" href="/"><img src="/static/img/logos/elim-palmera-la-central-fondo-blanco.png" width="25px"
height="30px"></a>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul>
<li class="nav-item">
<a class="nav-link" href="/">Inicio</a>
</li>
<li class="nav-item active">
<a href="/iglesias/" class="nav-link" >Iglesias</a>
</li>
<li class="nav-item">
<a href="/doctrina/" class="nav-link" >Doctrina</a>
</li>
</ul>
</div>
<form class="form-inline ml-auto mt-3">
<span class="text-white">Hola, ogonzales.</span>
Salir
</form>
</div>
</nav>
</header>
I have made a responsive navbar with bootstrap 4. I have aligned it in the center on tablet and mobile view (on the left on desktop).
However, when I expand it, the button takes a margin-left that I can't erase. I have tried margin-left 0px on the nav, many divs and on the button but it doesn't work it always keep the margin-left. I have also tried placing a !important and being more specific with my classes.
I would really appreciate your help.
You can see my whole page here: https://codepen.io/MiaSalazar/pen/GxBgRe
/*menu principal lateral*/
ul .no-list {
padding-bottom: 2px;
}
.menu-principal {
text-align: right;
padding-bottom: 5px;
padding-left: 0px;
font-size: 15px;
list-style-type: none;
-webkit-transition: width 4s; /* Safari */
transition: width 4s;
}
.navbar-dark .navbar-nav .nav-link:hover {
color: #b47068;
}
.navbar-dark .navbar-nav .show > .nav-link,
.navbar-dark .navbar-nav .active > .nav-link,
.navbar-dark .navbar-nav .nav-link.show,
.navbar-dark .navbar-nav .nav-link.active {
color: #b47068;
}
.navbar-dark .navbar-nav .nav-link {
color: #a59391;
}
.nav-menu {
text-decoration: none;
padding-right: 20px;
padding-left: 20px;
padding-top: 5px;
padding-bottom: 5px;
font-family: "Merriweather";
font-weight: 200;
float: right;
border-right: 2px solid #a59391;
line-height: 1.5;
letter-spacing: 1px;
}
.nav-menu-last {
border-right: 0px;
}
nav a:hover {
color: #b47068;
text-decoration: none;
}
.list-inline li {
display: inline;
width: auto;
}
.linea2 {
display: block;
}
.navbar-expand-lg .navbar-nav .nav-link {
padding-right: 1.7rem;
padding-left: 1.5rem;
padding-bottom: 0.5rem;
}
.navbar-toggler {
border-width: 0px;
background-color: #b47068;
}
.navbar-expand-lg .navbar-nav .nav-link:hover {
font-size: 20px;
}
#media screen and (max-width: 991px) {
.nav-menu {
border-right: 0px;
margin: auto;
}
.linea2 {
display: inline;
}
.nav-menu {
padding-top: 10px;
}
.xs-center {
display: flex;
justify-content: center;
padding-top: 20px;
}
.navbar-toggler:focus {
outline: none;
}
.ml-auto,
.mx-auto {
margin-left: 0px;
}
div .navbar-collapse .collapse show {
padding-top: 5px;
}
div nav button .toggle-center{
margin-left: 0px;
}
}
<!--Menu-->
<nav class="navbar navbar-expand-lg menu-principal navbar-toggleable-sm navbar-dark navbar-right d-flex" role="navigation">
<button class="navbar-toggler navbar-toggler-right ml-auto toggle-center " type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo01">
<ul class="navbar-nav no-list">
<li class="nav-item list-inline ">
<a class="nav-link nav-menu" href="#"><span class="linea1">1</span><span class="linea2"> 1</span> </a> </li>
<!--dropdown-->
<li class="nav-item dropdown list-inline">
<a class="nav-link dropdown-toggle nav-menu" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="linea1">1</span><span class="linea2"> 2</span> </a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Cosa 1</a>
<a class="dropdown-item" href="#">Cosa 2</a>
<a class="dropdown-item" href="#">Cosa 3</a>
</div>
</li>
<!--dropdown final-->
<li class="nav-item list-inline ">
<a class="nav-link nav-menu " href="#"><span class="linea1">1</span><span class="linea2"> 3</span> </a> </li>
<li class="nav-item list-inline ">
<a class="nav-link nav-menu nav-menu-last" href="#"><span class="linea1">1</span><span class="linea2"> 4</span> </a> </li>
</ul>
</div>
</nav>
<!--<fin-menu-->
<!--FIN HEADER-->
I checked the issue in codepen. In the mobile view give "margin: 0 auto;" to your button. Your issue will be fixed.
I have a multilevel menu that works well in desktop but there is no provision for small devices so the menu just flows all over the place. I have looked at many examples but cannot seem to move forward with coding this.
I am familiar with media queries but just need a starting point or some guidance so I can move forward with the rest of the site. If possible, I would like the submenu to open on hover.
Many thanks
html
<div class="menu">
<ul class="navigation">
<li>Home</li>
<li>Requests
<ul>
<li>Boxes
<ul>
<li>Add Box(es)</li>
<li>Retrievals</li>
<li>Returns</li>
<li>Destructions</li>
<li>Permanent Retrieval</li>
</ul>
</li>
<li>Files
<ul>
<li>Add File(s)</li>
<li>Retrievals</li>
<li>Returns</li>
<li>Destructions</li>
<li>Permanent File Retrieval</li>
</ul>
</li>
<li>Recycle Collection
<ul>
<li>Sacks</li>
<li>Boxes</li>
</ul>
</li>
<li>Box Supply</li>
<li>Sack Supply</li>
</ul>
</li>
<li>Reports</li>
<li>Invoices</li>
<li>Control Panel</li>
<li>Logout</li>
</ul>
<div class="clear"></div>
</div>
css
.menu {
width: 100%;
height: 40px;
margin: 0;
padding: 0;
background: #073648;
position: static;
}
.navigation {
list-style: none;
padding: 0;
margin: 0 0 0 16px;
background: rgb(58, 58, 58);
font-size: 16px;
}
.navigation li {
float: left;
}
.navigation li:hover {
background: #1a4655;
}
.navigation li:first-child {
-webkit-border-radius: 5px 5px 0 0;
border-radius: 5px 0 0 5px;
}
.navigation li a {
display: block;
padding: 0 20px;
text-decoration: none;
line-height: 40px;
color: #fff;
}
.navigation ul {
display: none;
position: absolute;
list-style: none;
padding: 0;
}
.navigation ul ul {
left: 100%;
top: 0;
}
.navigation ul li {
float: none;
position: relative;
}
.navigation li:hover>ul {
display: block;
background: #1a4655;
/* border: solid 3px #fff;*/
border-top: 0;
/*
-webkit-border-radius: 0 0 8px 8px;
border-radius: 0 0 8px 8px;
*/
-webkit-box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.25);
box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.25);
position: absolute;
width: 250px;
}
.navigation li:hover>ul li:hover {
-webkit-border-radius: 0 0 5px 5px;
/* border-radius: 0 0 5px 5px;*/
}
.navigation li li a:hover {
background: #0c323f;
color: white;
}
.navigation ul li:last-child a,
.navigation ul li:last-child a:hover {
/*
-webkit-border-radius: 0 0 5px 5px;
border-radius: 0 0 5px 5px;
*/
}
The easiest way for a beginner to build responsive websites is to use a framework like Bootstrap.
The new version of this framework, Bootstrap 4, is amazing.
Here's a complete, responsive code example for a working navigation that includes a drop-down that could be turned into a multi-level drop-down:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.navbar-collapse .navbar-nav .dropdown .dropdown-menu{
margin-left: 0px !important;
margin-top: 0px !important;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu a::after {
transform: rotate(-90deg);
position: absolute;
right: 6px;
top: .8em;
transition: all 0.3s ease-in-out 0s;
}
.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-left: .1rem;
margin-right: .1rem;
}
.dropdown:hover>.dropdown-menu{
display: block;
}
.dropdown-submenu:hover>.dropdown-menu{
display: block;
}
#media only screen and (max-width: 991px) {
.dropdown-submenu a::after {
transform: rotate(-90deg);
position: absolute;
right: 6px;
top: .8em;
transition: all 0.3s ease-in-out 0s;
}
}
.submenuNav{
opacity: 0.8;
margin-right: 3px;
}
.submenuNav:hover{
opacity: 1;
transform: translateX(3px);
transition: 0.4s all ease;
margin-right: 0px;
}
.dropdown-item{
background-color: none !important;
color: #007bff;
opacity: 0.8;
}
.dropdown-item:hover{
background-color: none !important;
color: #007bff;
opacity: 1;
transition: 0.2s all ease;
}
.customNav{
background-color: none !important;
color:#007bff !important;
}
.navbar .navbar-toggler .navbar-toggler-icon{
background-color: #007bff;
}
.dropdown-submenu a:active {
background-color: white;
}
</style>
<nav class="navbar navbar-expand-lg">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav" aria-controls="mainNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="mainNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<!-- Trekking In Nepal -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Trekking in Nepal
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Annapurna Region</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item submenuNav " href="#">Annapurna Circuit</a>
</li>
<li>
<a class="dropdown-item submenuNav " href="#"> Annapurna Base Camp</a>
</li>
<li>
<a class="dropdown-item submenuNav " href="#"> Upper Mustang</a>
</li>
</ul>
</li>
<li class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Everest Region</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item submenuNav " href="#">Everest Base Camp</a>
</li>
<li>
<a class="dropdown-item submenuNav " href="#"> Jiri to Everest Base Camp</a>
</li>
<li>
<a class="dropdown-item submenuNav " href="#"> Everest Three Pass</a>
</li>
</ul>
</li>
<li class="dropdown-submenu">
<a class="dropdown-item dropdown-toggle" href="#">Langtang Region</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item submenuNav " href="#">Langtang Helambu</a>
</li>
<li>
<a class="dropdown-item submenuNav " href="#"> Langtang Gosaikunda Lake</a>
</li>
<li>
<a class="dropdown-item submenuNav " href="#">Langtang Valley</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
More info:
https://getbootstrap.com/docs/4.0/components/navbar/
I am trying to change link color on nav-bar but after the changing it seems the same. What is wrong? Why its not working? Here is the code:
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top" id="sideNav">
<a class="navbar-brand" href="#page-top">
<span class="d-block d-lg-none">Start Bootstrap</span>
<span class="d-none d-lg-block">
<img class="img-fluid img-profile rounded-circle mx-auto mb-2" src="img/profile.jpg" alt="">
</span>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#experience">Experience</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#education">Education</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#skills">Skills</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#interests">Interests</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#awards">Awards</a>
</li>
</ul>
</div>
</nav>
CSS
.bg-primary {
background-color: #BD5D38 !important;
}
,
.nav-link {
color: #222222 !important;
}
.nav-link:hover,
.nav-link:focus,
.nav-link:active {
color: #ffffff;
}
#sideNav .navbar-nav .nav-item .nav-link {
font-weight: 600;
text-transform: uppercase;
}
#media (min-width: 992px) {
#sideNav {
text-align: center;
position: fixed;
top: 0;
left: 0;
display: flex;
flex-direction: column;
width: 17rem;
height: 100vh;
}
#sideNav .navbar-brand {
display: flex;
margin: auto auto 0;
padding: 0.5rem;
}
#sideNav .navbar-brand .img-profile {
max-width: 10rem;
max-height: 10rem;
border: 0.5rem solid rgba(255, 255, 255, 0.2);
}
#sideNav .navbar-collapse {
display: flex;
align-items: flex-start;
flex-grow: 0;
width: 100%;
margin-bottom: auto;
}
#sideNav .navbar-collapse .navbar-nav {
flex-direction: column;
width: 100%;
}
#sideNav .navbar-collapse .navbar-nav .nav-item {
display: block;
}
#sideNav .navbar-collapse .navbar-nav .nav-item .nav-link {
display: block;
}
}
I changed it on the .nav-link class but it's not working. While nav-link lass is inside tag. I don't want to use tags for defining hover and link colors because on my site other links will have other colors while only nav-links must have this color. That is why I am trying to use nav-link class for it. How can I make it worked?
Thanks in advance!
I think you have an unneeded comma after closing .bg-primary in your css. Right now it looks like:
.bg-primary {
background-color: #BD5D38 !important; },
Remove the comma at the end so it should be:
.bg-primary {
background-color: #BD5D38 !important; }
I think the comma is messing with the following lines of code. And the important in your color css is preventing the hover, focus and active states to work.
Here is how the top part of your css should look:
.bg-primary {
background-color: #BD5D38 !important; }
.nav-link {
color: #222222;
}
.nav-link:hover, .nav-link:focus, .nav-link:active {color:#ffffff}
Also if you are linking to the new beta version of bootstrap 4 you need to be more specific and add the .navbar-dark .navbar-nav so it would look like:
.bg-primary {
background-color: #BD5D38 !important;
}
.navbar-dark .navbar-nav .nav-link {
color: #222222;
}
.navbar-dark .navbar-nav .nav-link:hover,
.navbar-dark .navbar-nav .nav-link:focus,
.navbar-dark .navbar-nav .nav-link:active {
color:#ffffff
}
I have a static HTML header that contains icons.
One icon uses Bootrap's navbar dropdown component.
My problem is, when I click on the user icon and select one of the dropdown menus, the user icon is also highlighted (replicates in the attached scriptlet when you click on "D" and move your cursor down a little).
How do I prevent the icon "D" from being highlighted after moving the cursor down a little?
<style> li.navbar-nav > a:hover,
li.navbar-nav > a:focus {
background-color: rgba(255, 255, 255, 0.2);
border-radius: 4px;
color: #ffffff;
opacity: 1;
}
.navbar.navbar-ct-blue {
background: #1B96BF;
border: 1px solid #1B96BF;
max-height: 50px;
line-height: 30px;
margin: 0;
}
.navbar.navbar-ct-blue .nav-justified > li {
display: table-cell;
width: 1%
}
.navbar.navbar-ct-blue .nav-justified > li > a {
color: white;
font-size: 30px;
}
.navbar.navbar-ct-blue .nav-justified > li > a:hover {
color: #00F2FF;
background: none;
}
.navbar.navbar-ct-green {
background: #1B96BF;
border: 1px solid #1B96BF;
max-height: 50px;
line-height: 30px;
margin: 0;
}
.navbar.navbar-ct-green .nav-justified > li {
display: table-cell;
width: 1%
}
.navbar.navbar-ct-green .nav-justified > li > a {
color: white;
font-size: 30px;
}
.navbar.navbar-ct-green .nav-justified > li > a:hover {
color: #00F2FF;
background: none;
}
.table {
margin-bottom: 60px;
}
</style>
<link href="http://themes-pixeden.com/font-demos/7-stroke/Pe-icon-7-stroke.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Fixed navbar -->
<nav class="navbar navbar-ct-green navbar-fixed-top" role="navigation">
<div class="container">
<ul class="nav nav-justified">
<li>
<a href="#">
<i class="pe-7s-timer"></i>A
</a>
</li>
<li class="navbar-nav">
<a href="#">
<i class="pe-7s-note2" style="color: white;"></i>B
</a>
</li>
<li class="navbar-nav">
<a href="#">
<i class="pe-7s-config" style="color: white;"></i>C
</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<i class="pe-7s-user"></i> <span class="caret"></span>D
</a>
<ul class="dropdown-menu" background="blue">
<li>
Logout
</li>
<li>
Profile
</li>
</ul>
</li>
</ul>
</div>
</nav>
You can apply an inline style as background:none (or transparent) on last a class="dropdown-toggle"...> which is parent of your D element:
<li class="dropdown">
<a href="#" class="dropdown-toggle" style="background:none !important;" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
...
Edit:
another way is to define a css class like as noFocusBkg as following and apply it to containing li item as:
.noFocusBkg .dropdown-toggle:focus { /*** new class to prevent highlighting */
background: none; /* or transparent or inherit */
}
<li class="dropdown noFocusBkg">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
...
the full code snippet:
<style>
li.navbar-nav > a:hover,
li.navbar-nav > a:focus {
background-color: rgba(255, 255, 255, 0.2);
border-radius: 4px;
color: #ffffff;
opacity: 1;
}
.navbar.navbar-ct-blue {
background: #1B96BF;
border: 1px solid #1B96BF;
max-height: 50px;
line-height: 30px;
margin: 0;
}
.navbar.navbar-ct-blue .nav-justified > li {
display: table-cell;
width: 1%
}
.navbar.navbar-ct-blue .nav-justified > li > a {
color: white;
font-size: 30px;
}
.navbar.navbar-ct-blue .nav-justified > li > a:hover {
color: #00F2FF;
background: none;
}
.navbar.navbar-ct-green {
background: #1B96BF;
border: 1px solid #1B96BF;
max-height: 50px;
line-height: 30px;
margin: 0;
}
.navbar.navbar-ct-green .nav-justified > li {
display: table-cell;
width: 1%
}
.navbar.navbar-ct-green .nav-justified > li > a {
color: white;
font-size: 30px;
}
.navbar.navbar-ct-green .nav-justified > li > a:hover {
color: #00F2FF;
background: none;
}
.table {
margin-bottom: 60px;
}
.noFocusBkg .dropdown-toggle:focus { /*** new class to prevent highlighting */
background: none; /* or transparent or inherit */
}
</style>
<link href="http://themes-pixeden.com/font-demos/7-stroke/Pe-icon-7-stroke.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Fixed navbar -->
<nav class="navbar navbar-ct-green navbar-fixed-top" role="navigation">
<div class="container">
<ul class="nav nav-justified">
<li>
<a href="#">
<i class="pe-7s-timer"></i>A
</a>
</li>
<li class="navbar-nav">
<a href="#">
<i class="pe-7s-note2" style="color: white;"></i>B
</a>
</li>
<li class="navbar-nav">
<a href="#">
<i class="pe-7s-config" style="color: white;"></i>C
</a>
</li>
<li class="dropdown noFocusBkg">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<i class="pe-7s-user"></i> <span class="caret"></span>D
</a>
<ul class="dropdown-menu" background="blue">
<li>
Logout
</li>
<li>
Profile
</li>
</ul>
</li>
</ul>
</div>
</nav>
The highlighting is occurring on the :focus event. So you can prevent this by overriding Bootstrap's default :focus event, like so:
.nav > li > a:focus {
background-color: transparent;
}