I'm working on a left nav. I want the red bar to appear before menu items when hovering. It works good for the Home item and its sub items but the other root items cause the red bar to span the entire width of the page, from top to bottom, when hovering.
$(document).foundation();
.vertical.dropdown.menu [href] > i {
display: block;
}
.vertical.dropdown.menu [href] {
display: block;
text-align: center;
}
.left-bar {
position: fixed;
top: 0;
left: 0;
width: 150px;
height: 100%;
color: #333;
background: #FFFFFF;
}
.left-bar .vertical.menu.nested {
padding: 0;
}
.left-bar [href] > i {
display: block;
}
.left-bar [href] {
display: block;
text-align: left;
padding: 0;
color: #333;
}
.left-bar [href]:hover {
background: #9B9B9BFF;
}
.left-bar .vertical.menu > li {
position: relative;
background: #FFFFFF;
color: #333;
border: 0;
}
.left-bar .top-level-item [href]:hover::before {
content: "";
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 5px;
background-color: red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0/css/material-design-iconic-font.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.0/css/foundation.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.1/js/foundation.min.js"></script>
<ul class="vertical dropdown menu left-bar" data-dropdown-menu>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-home zmdi-hc-3x"></i>
<span>Home</span>
</a>
<ul class="vertical menu nested">
<li><i class="zmdi zmdi-list zmdi-hc-3x"></i> Sub Item 1</li>
<li><i class="zmdi zmdi-list zmdi-hc-3x"></i> Sub Item 2</li>
<!-- ... -->
</ul>
</li>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-account zmdi-hc-3x"></i>
<span>Account</span>
</a>
</li>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-settings zmdi-hc-3x"></i>
<span>Settings</span>
</a>
</li>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-help-outline zmdi-hc-3x"></i>
<span>Help</span>
</a>
</li>
<!-- ... -->
</ul>
How to fix it so that the red bar only spans the height of the menu item?
I made a change in CSS, added a comment there.There was issue in CSS selector based on DOM structure
.left-bar.vertical.menu li instead of .left-bar .vertical.menu > li , I removed the space and > , so that all li are now having position relative
$(document).foundation();
.vertical.dropdown.menu [href] > i {
display: block;
}
.vertical.dropdown.menu [href] {
display: block;
text-align: center;
}
.left-bar {
position: fixed;
top: 0;
left: 0;
width: 150px;
height: 100%;
color: #333;
background: #FFFFFF;
}
.left-bar .vertical.menu.nested {
padding: 0;
}
.left-bar [href] > i {
display: block;
}
.left-bar [href] {
display: block;
text-align: left;
padding: 0;
color: #333;
}
.left-bar [href]:hover {
background: #9B9B9BFF;
}
/* I made a change here */
.left-bar.vertical.menu li {
position: relative;
background: #FFFFFF;
color: #333;
border: 0;
}
.left-bar .top-level-item [href]:hover::before {
content: "";
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 5px;
background-color: red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0/css/material-design-iconic-font.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.0/css/foundation.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.1/js/foundation.min.js"></script>
<ul class="vertical dropdown menu left-bar" data-dropdown-menu>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-home zmdi-hc-3x"></i>
<span>Home</span>
</a>
<ul class="vertical menu nested">
<li><i class="zmdi zmdi-list zmdi-hc-3x"></i> Sub Item 1</li>
<li><i class="zmdi zmdi-list zmdi-hc-3x"></i> Sub Item 2</li>
<!-- ... -->
</ul>
</li>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-account zmdi-hc-3x"></i>
<span>Account</span>
</a>
</li>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-settings zmdi-hc-3x"></i>
<span>Settings</span>
</a>
</li>
<li class="top-level-item">
<a href="#">
<i class="zmdi zmdi-help-outline zmdi-hc-3x"></i>
<span>Help</span>
</a>
</li>
<!-- ... -->
</ul>
I made a border left for the red; set to white initially.
I made changes:
Cut out all the CSS not needed including all the position related
stuff
Used classes instead of some [href] I consider "fragile"
Changed to specific background-color to be clear
Set a 0 border, then added the specifics for the left one
Used rem instead of px since typical browsers use a 16px= 1rem size base; and you can force if needed to support those that do not do so.
You can decide on the sub-item if you want the container to continue to have the border and do some things related to that with CSS as here: How to style the parent element when hovering a child element?
or perhaps with some simple JavaScript to help.
$(document).foundation();
.nav-item {
width: 10rem;
background-color: #FFFFFF;
text-align: center;
color: #333;
border: 0;
border-left-width: 0.25rem;
border-left-style: solid;
border-left-color: #FFFFFF;
}
.nav-item:hover {
border-left-color: #FF0000;
}
.nav-item .nav-link:hover {
background-color: #9B9B9BFF;
}
.nav-link {
color: #333333;
}
.nav-link * {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0/css/material-design-iconic-font.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.0/css/foundation.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.1/js/foundation.min.js"></script>
<ul class="vertical dropdown menu left-bar" data-dropdown-menu>
<li class="top-level-item nav-item">
<a class="nav-link" href="#">
<i class="zmdi zmdi-home zmdi-hc-3x"></i>
<span>Home</span>
</a>
<ul class="vertical menu nested">
<li class="nav-item"><a class="nav-link" href="#"><i class="zmdi zmdi-list zmdi-hc-3x"></i> Sub Item 1</a></li>
<li class="nav-item"><a class="nav-link" href="#"><i class="zmdi zmdi-list zmdi-hc-3x"></i> Sub Item 2</a></li>
</ul>
</li>
<li class="top-level-item nav-item">
<a class="nav-link" href="#">
<i class="zmdi zmdi-account zmdi-hc-3x"></i>
<span>Account</span>
</a>
</li>
<li class="top-level-item nav-item">
<a class="nav-link" href="#">
<i class="zmdi zmdi-settings zmdi-hc-3x"></i>
<span>Settings</span>
</a>
</li>
<li class="top-level-item nav-item">
<a class="nav-link" href="#">
<i class="zmdi zmdi-help-outline zmdi-hc-3x"></i>
<span>Help</span>
</a>
</li>
</ul>
you have position absolute on top-level-item href before
put position relative on top-level-item instead of the li
.top-level-item {
position: relative;
}
Related
Unable to move my GitHub icon/link to the bottom of the navbar. I'm trying to use the last-child margin-top auto however it won't work for me. is there something wrong in my code, or am I going about it the wrong way? any advice would be great. I'm new to HTML and CSS so if you see anything you would change in general please let me know.
:root {
font-size: 16px;
font-family: 'Open sans';
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
color: white;
background-color: black;
}
body::-webkit-scrollbar {
width: 0.3rem;
}
body::-webkit-scrollbar-track {
background-color: red;
}
body::-webkit-scrollbar-thumb {
background-color: blueviolet;
}
main {
margin-left: 15rem;
padding: 1rem;
}
/****** Navbar ******/
.img-logo {
width: 15rem;
margin-top: 1rem;
}
.navbar {
position: fixed;
background-color: blueviolet;
width: 15rem;
height: 100vh;
}
.navbar-nav {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
}
.nav-link {
display: flex;
align-items: center;
height: 5rem;
}
.nav-item:last-child {
margin-top: auto;
}
<body>
<div class="navbar">
<nav>
<img class="img-logo" src="img/Logo.png" alt="My logo">
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-house"></i>
<span class="link-text">Home</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-user"></i>
<span class="link-text">About Me</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-screwdriver-wrench"></i>
<span class="link-text">What I Do</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-folder-open"></i>
<span class="link-text">Projects</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-id-card"></i>
<span class="link-text">Contact</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-brands fa-github"></i>
<span class="link-text"></span>
</a>
</li>
</ul>
</nav>
</div>
I got it working by flexing the navbar (parent of both navbar-nav and your image):
:root {
font-size: 16px;
font-family: 'Open sans';
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
color: white;
background-color: black;
}
body::-webkit-scrollbar {
width: 0.3rem;
}
body::-webkit-scrollbar-track {
background-color: red;
}
body::-webkit-scrollbar-thumb {
background-color: blueviolet;
}
main {
margin-left: 15rem;
padding: 1rem;
}
/****** Navbar ******/
.img-logo {
width: 15rem;
margin-top: 1rem;
order: 2;
}
nav {
display: flex;
flex-flow: column nowrap;
position: fixed;
background-color: blueviolet;
width: 15rem;
height: 100vh;
}
.navbar-nav {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
}
.nav-link {
display: flex;
align-items: center;
height: 5rem;
}
.nav-item:last-child {
margin-top: auto;
}
<body>
<nav>
<img class="img-logo" src="img/Logo.png" alt="My logo">
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-house"></i>
<span class="link-text">Home</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-user"></i>
<span class="link-text">About Me</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-screwdriver-wrench"></i>
<span class="link-text">What I Do</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-folder-open"></i>
<span class="link-text">Projects</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-id-card"></i>
<span class="link-text">Contact</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-brands fa-github"></i>
<span class="link-text"></span>
</a>
</li>
</ul>
</nav>
You already have a flexbox for your nav items so it makes sense to just add the logo as a list item. For this example, I called it .logo. You can then take this new list item and nest the image within. Next, we'll flex this new list item and use flex-basis: 100%;. Then you can specify align-items: flex-end; for it to be at the very bottom of the navbar.
From here, you can specify margin-left: auto; for it to be aligned to the right or the opposite for it to be aligned on the left.
:root {
font-size: 16px;
font-family: 'Open sans';
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
color: white;
background-color: black;
}
body::-webkit-scrollbar {
width: 0.3rem;
}
body::-webkit-scrollbar-track {
background-color: red;
}
body::-webkit-scrollbar-thumb {
background-color: blueviolet;
}
main {
margin-left: 15rem;
padding: 1rem;
}
/****** Navbar ******/
.img-logo {
margin-top: 1rem;
}
.navbar {
position: fixed;
background-color: blueviolet;
width: 15rem;
height: 100vh;
}
.navbar-nav {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
}
.nav-link {
display: flex;
align-items: center;
height: 5rem;
}
.nav-item:last-child {
margin-top: auto;
}
.logo {
display: flex;
flex-basis: 100%;
align-items: flex-end;
}
<body>
<div class="navbar">
<nav>
<ul class="navbar-nav">
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-house"></i>
<span class="link-text">Home</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-user"></i>
<span class="link-text">About Me</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-screwdriver-wrench"></i>
<span class="link-text">What I Do</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-folder-open"></i>
<span class="link-text">Projects</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-solid fa-id-card"></i>
<span class="link-text">Contact</span>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<i class="fa-brands fa-github"></i>
<span class="link-text"></span>
</a>
</li>
<li class="logo">
<img class="img-logo" src="https://dummyimage.com/75/000/fff" alt="My logo">
</li>
</ul>
</nav>
</div>
I have a navigation menu using bootstrap, and the menu worked fine when I had the second sub-menu off to the side of the first sub-menu. I've changed it to put the third level sub-menu under the dropdown's link. Now when hovered over it the third level menu will not display.
I have tried different combinations of classes and adding ids but can't seem to get it to show up on hover. I'm not using JavaScript for this currently as it worked fine with html and css.
This is a small mockup of what is going on.
html {
padding: 0;
margin: 0;
min-height: 100%;
}
body {
padding: 0;
margin: 0;
min-height: 100vh;
overflow-x: hidden;
border-top: 7px solid black;
font-size: 62%;
font-family: "Open Sans";
}
* {
font-family: 'Open Sans';
}
.container {
padding-right: 0;
}
main {
padding-bottom: 22em;
}
p {
font-size:
}
.extra-bold {
font-weight: 800;
}
.semi-bold {
font-weight: 600;
}
.bold {
font-weight: bold;
}
.navbar {
background-image: linear-gradient(rgba(235,235,235,0), rgba(235,235,235,1));
height: 129px;
}
.navbar-brand {
margin-left: 1em;
margin-top: .5em;
}
.nav {
min-width: 100%;
flex-direction: row;
letter-spacing: 1px;
margin-left: 10em;
}
.navbar-nav {
flex-direction: row;
}
.nav li {
margin: 0em 1em;
}
.nav-link {
color: black;
}
.dropdown-menu a:hover {
color: white;
background-color: rgb(217,0,0);
}
.dropdown-item:hover {
color: white;
background-color: rgb(217,0,0);
}
.nav-item {
font-size: 1.6em;
}
.nav > li.disabled > a:hover {
text-decoration: underline;
color: rgb(217,0,0);
}
.nav-link:hover {
text-decoration: underline;
color: rgb(217,0,0);
}
.dropdown-hover {
color: white;
background-color: rgb(217,0,0);
}
.dropdown-item:hover {
background-color: rgb(217,0,0);
color: white;
}
.dropdown-menu.disabled:hover {
color: white;
background-color: rgb(217,0,0);
}
a.dropdown-item.dropdown.disabled:hover {
color: white;
background-color: rgb(217,0,0);
}
.dropdown-menu li:hover {
background-color: rgb(217,0,0);
color: white;
}
.dropdown ul li:hover > a {
background-color: rgb(217,0,0);
color: white;
}
.dropdown-menu .sub-menu {
position: relative;
top: 0;
display: none;
margin-top: -1px;
background-color: #fff;
border: 1px solid rgba(0,0,0,.15);
border-radius: .25rem;
list-style-type: none;
padding-left: 0;
}
#toggle:hover .sub-menu {
display: block;
}
.fa-caret-right {
float: right;
line-height: 1.5;
}
.fa-caret-right {
line-height: 1.75;
margin-left: .5em
}
.container {
margin-left: auto;
margin-right: auto;
}
ul.nav li.dropdown:hover div.dropdown-menu {
display: block;
}
* {
position: relative;
}
#navbarDropdown {
color: black;
}
.dropdown-item.disabled, .dropdown-item:disabled {
color: black;
}
.dropdown-item.disabled:hover, .dropdown-item:disabled:hover {
color: white;
background-color: rgb(217,0,0);
}
.nav-link.disabled:hover {
color: white;
background-color: rgb(217,0,0);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0 shrink-to-fit=no">
<link rel="canonical" href="http://hmi.blueseaonline.net/index.php" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.11.1/css/light.css" integrity="sha384-Rg8mXhpzJH99uBsgdsCp9zXjkcsw/pm+s4Kgu/56eRQSd8SAszYc6hssH5MLusHl" crossorigin="anonymous">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.11.1/css/fontawesome.css" integrity="sha384-O68Og25nY+MZZRUiP6gm7XPDuHsNC5DgKWEoxn6+3CFcGLRXuFihbGO/8c5Ned0i" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" />
<link rel="stylesheet" href="http://hmi.blueseaonline.net/index.php/site/style">
</head>
<body>
<header>
<nav class="navbar navbar-expand-md navbar-right">
<div class="container">
<div class="row" style="min-width: 100%;">
<div class="col-xs-12">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="nav justify-content-center" style="list-style-type: none;">
<li class="nav-item dropdown">
Test <i class="fa fa-caret-down"></i>
<div class="dropdown-menu toggle-menu" aria-labelledby="navbarDropdown">
<ul style="padding:0; list-style-type: none;">
<li>
<a class="dropdown-item dropdown disabled" id="toggle" href="">Test <i class="fa fa-caret-right"></i></a>
<ul class="dropdown-menu sub-menu">
<li><a class="dropdown-item" href="">Test</a></li>
<li><a class="dropdown-item" href="">Test</a></li>
<li><a class="dropdown-item" href="">Test</a></li>
<li><a class="dropdown-item" href="">Test</a></li>
</ul>
</li>
<li>
<a class="dropdown-item" href="#">Test</a>
</li>
<li>
<a class="dropdown-item dropdown" href="">Test <i class="fa fa-caret-right"></i></a>
<ul class="dropdown-menu sub-menu">
<li><a class="dropdown-item" href="">Test</a></li>
<li><a class="dropdown-item" href="">Test</a></li>
</ul>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
</ul>
</div>
</li>
<li class="nav-item">
Test
</li>
<li class="nav-item dropdown">
Test <i class="fa fa-caret-down"></i>
<span class="caret"></span><span class="sr-only">Dropdown Menu Toggle</span>
<div class="dropdown-menu toggle-menu" aria-labelledby="navbarDropdown">
<ul style="padding:0; list-style-type: none;">
<li>
<a class="dropdown-item" href=""Test</a>
</li>
<li>
<a class="dropdown-item dropdown" href="">Test <i class="fa fa-caret-right"></i></a>
<ul class="dropdown-menu sub-menu">
<li><a class="dropdown-item" href="">Test</a></li>
<li><a class="dropdown-item" href="">Test</a></li>
<li><a class="dropdown-item" href="">Test</a></li>
</ul>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
<li>
<a class="dropdown-item" href="">Test</a>
</li>
</ul>
</div>
</li>
<li class="nav-item">
Test
</li>
</ul>
</div>
</div>
</div>
</div>
</nav>
<div class="clearBoth"></div>
<!-- Load jQuery, then Popper, then Bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<!-- Load Map Resizer -->
<!-- Load lightbox -->
</body>
</html>
This is what I'm trying to get: Dropdown Design
I am working on a horizontal dropdown navigation menu. I am using a transition to slow it down, but I've hit a bug. If you jump too fast (before the transition completes?) from one nav item to the other, you are still shown the other dropdown menu. It's a bit hard to explain - but I've created a CodePen.
I'm sure I'm just overlooking something basic... all feedback is appreciated. Thank you in advance!
CodePen link: https://codepen.io/zp12345/pen/mQzvXr
HTML
<div class="nav-links">
<ul class="nav-primary" id="nav-primary">
<li class="nav-item-top">
<a href="#link">
<span class="nav-item-label">Item One</span>
</a>
<ul class="nav-dropdown">
<li class="nav-dropdown-item">
<a href="#link">
<h5>1.1</h5>
</a>
</li>
<li class="nav-dropdown-item">
<a href="#link">
<h5>1.2</h5>
</a>
</li>
</ul>
</li>
<li class="nav-item-top">
<a href="#link">
<span class="nav-item-label">Item Two</span>
</a>
<ul class="nav-dropdown">
<li class="nav-dropdown-item">
<a href="#link">
<h5>2.1</h5>
</a>
</li>
<li class="nav-dropdown-item">
<a href="#link">
<h5>2.2</h5>
</a>
</li>
</ul>
</li>
<li class="nav-item-top">
<a href="#link">
<span class="nav-item-label">Item Three</span>
</a>
<ul class="nav-dropdown">
<li class="nav-dropdown-item">
<a href="#link">
<h5>3.1</h5>
</a>
</li>
<li class="nav-dropdown-item">
<a href="#link">
<h5>3.2</h5>
</a>
</li>
</ul>
</li>
<li class="nav-item-top">
<a href="#link">
<span class="nav-item-label">Item Four</span>
</a>
<ul class="nav-dropdown">
<li class="nav-dropdown-item">
<a href="#link">
<h5>4.1</h5>
</a>
</li>
</ul>
</li>
</ul>
</div>
SCSS
.nav-primary {
display: flex;
flex-grow: 1;
justify-content: center;
list-style-type: none;
padding-left: 0;
}
.nav-item-top .nav-item-label {
color: #383838;
font-size: 18px;
padding: 0 24px;
cursor: pointer;
}
.nav-item-top {
&:hover {
.nav-item-label {
color: #319644;
}
.nav-dropdown {
visibility: visible;
opacity: 1;
padding: 16px 0;
}
}
}
.nav-dropdown {
width: 100%;
left: 0;
position: fixed;
top: 60px;
transition: .2s;
opacity: 0;
z-index: 3;
padding: 0;
background-color: #133751;
color: #133751;
display: flex;
align-items: center;
justify-content: center;
visibility: hidden;
list-style-type: none;
.nav-dropdown-item {
transition: .2s;
padding: 12px 24px;
text-align: center;
color: #fff;
cursor: pointer!important;
}
h5 {
color: #fff;
margin: 0;
text-transform: none;
font-size: 16px;
}
}
.nav-dropdown-item {
a {
transition: all 0.2s;
text-decoration: none;
}
}
You can use transition-delay:0.2s; on hover when you show the .nav-dropdown. That will not show the dropdown when you move mouse fast.
You can test it here
#import "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
#import "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css";
#import "https://daneden.github.io/animate.css/animate.min.css";
#media (min-width: 768px) {
.bootstrap-vertical-nav .collapse {
display: block;
}
}
/*-------------------------------*/
/* Sidebar nav styles */
/*-------------------------------*/
.sidebar-nav {
list-style: none;
margin: 0;
padding: 0;
position: absolute;
top: 0;
width: 220px;
}
.sidebar-nav li {
display: inline-block;
line-height: 20px;
position: relative;
width: 100%;
}
/* this background color doesn't trigger */
.sidebar-nav li:before {
content: '';
height: 100%;
left: 0;
position: absolute;
top: 0;
transition: width 0.2s ease-in;
width: 3px;
z-index: 0;
}
.sidebar-nav li:first-child a {
background-color: #1a1a1a;
color: 555;
}
.sidebar-nav li:nth-child(2):before {
background-color: #d12525;
}
.sidebar-nav li:nth-child(3):before {
background-color: #4c366d;
}
.sidebar-nav li:nth-child(4):before {
background-color: #583e7e;
}
.sidebar-nav li:nth-child(5):before {
background-color: #64468f;
}
.sidebar-nav li:nth-child(6):before {
background-color: #704fa0;
}
.sidebar-nav li:nth-child(7):before {
background-color: #7c5aae;
}
.sidebar-nav li:nth-child(8):before {
background-color: #8a6cb6;
}
.sidebar-nav li:nth-child(9):before {
background-color: #987dbf;
}
.sidebar-nav li:hover:before {
transition: width 0.2s ease-in;
width: 100%;
}
.sidebar-nav li a {
background-color: #1a1a1a;
color: #fff;
display: block;
padding: 10px 15px 10px 30px;
text-decoration: none;
z-index: -2;
}
.sidebar-nav li.open:hover before {
transition: width 0.2s ease-in;
width: 100%;
}
.sidebar-nav .dropdown-menu {
background-color: #222222;
border-radius: 0;
border: none;
box-shadow: none;
margin: 0;
padding: 0;
position: relative;
width: 100%;
}
.sidebar-nav li a:hover,
.sidebar-nav li a:active,
.sidebar-nav li a:focus,
.sidebar-nav li.open a:hover,
.sidebar-nav li.open a:active,
.sidebar-nav li.open a:focus {
background-color: transparent;
color: #fff;
text-decoration: none;
}
.sidebar-nav > .sidebar-brand {
font-size: 20px;
height: 65px;
line-height: 44px;
}
<div class="collapse" id="collapseExample">
<ul class="nav flex-column sidebar-nav" id="exCollapsingNavbar3">
<li class="nav-item sidebar-brand">
<a nav-link href="#">
Bootstrap 4
</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#"><i class="fa fa-fw fa-home"></i> Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-fw fa-folder"></i> Page one</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-fw fa-file-o"></i> Second page</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-fw fa-cog"></i> Third page</a>
</li>
<li class="nav-item dropdown">
<a class="nav-item" href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-fw fa-plus"></i> Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Dropdown heading</li>
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li>Separated link</li>
<li>One more separated link</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-fw fa-bank"></i> Page four</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-fw fa-bank"></i> Page 5</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><i class="fa fa-fw fa-twitter"></i> Last page</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
Issue 1) I'm working on a HTML + CSS sidenav, I'm having some trouble with the Z-Index, if you hover over an item in the side nav, there will be a color transition from left to right. I want the text to stay on top.
Issue 2) My drop down list won't work at all, I thought i followed the bootstrap tutorial on it.
This entire project is an angular2 project. if you can use ng-bootstrap for any of the functionality, great! if not, i appreciate any feedback on how to resolve either issue.
UPDATED: Solution, you'll need ng-bootstrap to have it working. Wasn't sure how to add the library to the snippet tool.
#import "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css";
#import "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css";
#import "https://daneden.github.io/animate.css/animate.min.css";
#media (min-width: 768px) {
.bootstrap-vertical-nav .collapse {
display: block;
}
}
/*-------------------------------*/
/* Sidebar nav styles */
/*-------------------------------*/
.sidebar-nav {
list-style: none;
margin: 0;
padding: 0;
position: absolute;
top: 0;
width: 220px;
}
.sidebar-nav li {
display: inline-block;
line-height: 20px;
position: relative;
width: 100%;
}
.sidebar-nav div {
display: inline-block;
line-height: 20px;
position: relative;
width: 100%;
}
/* this background color doesn't trigger */
.sidebar-nav li:before {
content: '';
height: 100%;
left: 0;
position: absolute;
top: 0;
transition: width 0.2s ease-in;
width: 3px;
z-index: -2;
}
.sidebar-nav div:before {
content: '';
height: 100%;
left: 0;
position: absolute;
top: 0;
transition: width 0.2s ease-in;
width: 3px;
z-index: -2;
}
.sidebar-nav li:first-child a {
background-color: #1a1a1a;
color: 555;
}
.sidebar-nav li:nth-child(n):before {
z-index: 1;
background-color: #b7e7ff;
}
.sidebar-nav li:nth-child(1):before {
background-color: transparent;
}
/*
BLUE: 0085c6 and darker is: 0c78ad
YELLOW: f3c300
BLACK: 000
GREEN: 009f3d
RED: e00024
*/
.sidebar-nav li:nth-child(n) div:before {
background-color: #0085c6;
z-index: 1;
}
.sidebar-nav li:hover:before {
transition: width 0.2s ease-in;
width: 100%;
z-index: -1;
}
.sidebar-nav li a {
background-color: #1a1a1a;
color: #fff;
display: block;
padding: 10px 15px 10px 30px;
text-decoration: none;
}
.sidebar-nav li.open:hover before {
transition: width 0.2s ease-in;
width: 100%;
}
.sidebar-nav .dropdown-menu {
background-color: #1a1a1a;
border-radius: 0;
border: none;
box-shadow: none;
margin: 0;
padding: 0 0 0 15%;
position: relative;
width: 100%;
}
.sidebar-nav .dropdown-menu li:nth-child(1):before {
background-color: #b7e7ff;
}
.sidebar-nav li a:hover,
.sidebar-nav li a:active,
.sidebar-nav li a:focus,
.sidebar-nav li.open a:hover,
.sidebar-nav li.open a:active,
.sidebar-nav li.open a:focus {
background-color: transparent;
color: #fff;
text-decoration: none;
}
.sidebar-nav>.sidebar-brand {
font-size: 20px;
height: 65px;
line-height: 44px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-4 col-lg-3">
<!--<div id="collapseExample" [ngbCollapse]="isCollapsed">
<div class="card">
<div class="card-block">
You can collapse this card by clicking Toggle
</div>
</div>
</div>-->
<div id="collapseExample" [ngbCollapse]="isCollapsed" class="bootstrap-vertical-nav">
<div class="collapse" id="collapseExample">
<ul class="nav flex-column sidebar-nav" id="exCollapsingNavbar3">
<li class="nav-item sidebar-brand">
<a nav-link href="#">
Bootstrap 4
</a>
</li>
<li class="nav-item">
<div>
<a class="nav-link active" href="#"><i class="fa fa-fw fa-home"></i> Home</a>
</div>
</li>
<li class="nav-item">
<div>
<a class="nav-link" href="#"><i class="fa fa-fw fa-folder"></i> Page one</a>
</div>
</li>
<li class="nav-item">
<div>
<a class="nav-link" href="#"><i class="fa fa-fw fa-file-o"></i> Second page</a>
</div>
</li>
<li class="nav-item">
<div>
<a class="nav-link" href="#"><i class="fa fa-fw fa-cog"></i> Third page</a>
</div>
</li>
<li class="nav-item dropdown">
<div ngbDropdown>
<a class="nav-item" href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-fw fa-plus"></i> Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
</div>
</li>
<li class="nav-item">
<div>
<a class="nav-link disabled" href="#">Disabled</a>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-8 col-lg-9">
<p>
<button type="button" class="btn btn-outline-primary" (click)="isCollapsed = !isCollapsed" [attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample">
Toggle
</button>
</p>
<hr />
<div>
<h1>Right Pane - Bootstrap 4.0 Vertical Nav Example</h1>
</div>
</div>
</div>
</div>
The problem is you cannot use that li:before for both showing the left colored bar and also for the color transition, and still have the text on top.
What I suggest is adding a span element inside you list item, and that span should represent the colored bar that should always be present:
<li class="nav-item">
<span class="before-bar"></span>
<a class="nav-link active" href="#"><i class="fa fa-fw fa-home"></i> Home</a>
</li>
.before-bar {
position: absolute;
height: 100%;
width: 3px;
}
.sidebar-nav li:nth-child(2):before,
.sidebar-nav li:nth-child(2) .before-bar {
background-color: #d12525;
}
See http://plnkr.co/edit/AHaTOQmBwllZV5bATFtJ?p=preview (only modified for the "Home" link)
Probably an even better solution would be to keep the li:before for the 3px color bar, and have the span for the color transition (with a lower z-index).
To keep the text on top, change the z-index for li:before
.sidebar-nav li:before {
content: '';
height: 100%;
left: 0;
position: absolute;
top: 0;
transition: width 0.2s ease-in;
width: 3px;
z-index: -1;
}
Issue 1) I'm working on a HTML + CSS sidenav, I'm having some trouble with the Z-Index, if you hover over an item in the side nav, there will be a color transition from left to right. I want the text to stay on top.
.sidebar-nav li a{
position:relative;
z-index:0;
}
Issue 2) My drop down list won't work at all, I thought i followed the bootstrap tutorial on it.
This entire project is an angular2 project. if you can use ng-bootstrap for any of the functionality, great! if not, i appreciate any feedback on how to resolve either issue.
Try to add bootstrape.min.js
I was just doing some tests with my navbar and when I wanted to put an image right before username, this happend. I don't really know what is the problem, I usually do server-side scripting but not these design. This active class on dropdown menu, as planned, needed to be together with the image, but actually it's only going to the half. If someone can give me example of what things I could put on my CSS file because there lies the problem.
Here is the image
.navbar-image {
width: 32px;
height: 32px;
position: absolute;
top: 10px;
left: -19px;
border-radius: 50%;
}
.nav-img {
position: relative;
padding-left: 50px;
}
<li class="dropdown">
<a href="#" class="dropdown-toggle nav-img" data-toggle="dropdown" role="button" aria-expanded="false">
<img src="/uploads/avatars/{{ Auth::user()->avatar }}" class='navbar-image'>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><i class='fa fa-btn fa-sticky-note'></i>Posts</li>
<li><i class='fa fa-btn fa-user'></i>Profile</li>
<li role="separator" class="divider"></li>
<li><i class="fa fa-btn fa-sign-out"></i>Log out</li>
</ul>
</li>
Try
.dropdown, .dropdown-toggle, .dropdown-menu {
display: inline-block;
}
.dropdown-toggle, .dropdown-menu {
float: right;
}
.navbar-image {
width: 32px;
height: 32px;
border-radius: 50%;
}
Here are the fixes of your menu image:
.dropdown { position: relative; }
.navbar-image {
position: absolute;
width: 32px;
height: 32px;
top: 10px; /*** Set value as per your required ***/
right: 10px; /*** Set value as per your required ***/
border-radius: 50%;
}
Try this code:
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
.navbar-image {
width: 32px;
height: 32px;
border-radius: 50%;
margin-right: 5px;
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<img src="//placehold.it/80x80/?text=%20" class='navbar-image'>
User Name <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><i class='fa fa-btn fa-sticky-note'></i>Posts</li>
<li><i class='fa fa-btn fa-user'></i>Profile</li>
<li role="separator" class="divider"></li>
<li><i class="fa fa-btn fa-sign-out"></i>Log out</li>
</ul>
</li>
</ul>
</div>
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>