I have a full-width Bootstrap dropdown menu that is not working properly as it has a lot of space between the button and the menu.
So far I am only using CSS for this, no javascript. Here´s the code:
HTML:
<nav class="p-4 navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">
<img src="http://lorempixel.com/30/30/abstract" width="30" height="30" class="d-inline-block align-top" alt="">
Shopstrap
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
<i class="fas fa-angle-up"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item dropdown catalog">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Catalog
<i class="fas fa-angle-up"></i>
</a>
<div class="dropdown-menu catalog-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
</nav>
CSS:
.navbar-default .navbar-nav > li.dropdown:hover > a,
.navbar-default .navbar-nav > li.dropdown:hover > a:hover,
.navbar-default .navbar-nav > li.dropdown:hover > a:focus {
background-color: rgb(231, 231, 231);
color: rgb(85, 85, 85);
}
li.dropdown:hover > .dropdown-menu {
display: block;
}
.navbar .dropdown.catalog {
position: static;
float: none;
}
.navbar .dropdown .dropdown-menu.catalog-menu{
width: 100%;
}
Fiddle:
https://jsfiddle.net/m4Lxugea/1/
I hope any of you can help me with this. I was thinking in maybe I can´t do this purely CSS but I need to add javascript for the hovering.
Alejandro.
The key CSS for you is:
li.dropdown:hover > .dropdown-menu {
display: block;
}
Which means that: while you're hovering over the 'Catalog' link in the navigation (on desktop), show the child .dropdown-menu and as soon as you hover away, the .dropdown-menu goes to its default state of display:none;
With JS you can add a class which makes sure that the .dropdown-menu stays open unless you click somewhere else to undo this
Here is solution: Bootstrap adds margin-top for each dropdown-menu class.
Make it 0px;
Bootstrap has padding on navbar class, Also make override it to 0px;
Here is working example https://bootstrap-menu.com/detail-megamenu.html
navbar{ padding-top: 0; padding-bottom: 0; }
navbar .has-megamenu{position:static!important;}
navbar .megamenu{left:0; right:0; width:100%; padding:20px; }
navbar .nav-link{ padding-top:1rem; padding-bottom:1rem; }
Related
Nav is changing site width. I don't know why's that.
I thought that's because margins or padding, but couldn't find any way to fix the problem.
I came to conclusion that is cousing problems, but I don't know why's that.
Please help!
HTML code:
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">IT SPECIALIST</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#menu" aria-controls="menu" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="menu">
<ul class="navbar-nav ms-auto">
<li class="nav-item active">
<a class="nav-link" id="active" href="#">GŁÓWNA</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
O NAS
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Zespół</a>
<a class="dropdown-item" href="#">Partnerzy</a>
<a class="dropdown-item" href="#">Facebook Fanpage</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
OFERTA
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Odzyskiwanie Danych</a>
<a class="dropdown-item" href="#">Wsparcie dla twojej firmy</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" id="nav-link" href="#">USŁUGI DLA FIRM I OSÓB PRYWATNYCH</a>
</li>
<li class="nav-item">
<a class="nav-link" id="nav-link" href="#">DLACZEGO MY</a>
</li>
<li class="nav-item">
<a class="nav-link" id="nav-link" href="#">ZGŁOŚ AWARIĘ</a>
</li>
</ul>
</div>
</nav>
</div>
<div class="col-md-2"></div>
</div>
I deleted whole css code, and nothing changed, but in case you need it here it is:
CSS code:
body {
margin: none;
padding: none;
}
.navbar .navbar-brand {
margin-left: 1vw;
}
.navbar button {
margin-right: 1vw;
}
.navbar .dropdown-item:hover, .navbar #nav-link:hover, .navbar #active {
background-color: #5394dd;
color: white;
}
.navbar .nav-item {
margin-left: 0.5vw;
}
.navbar .nav-item:last-child {
margin-right: 1vw;
}
Edit 1:
row is cousing the problem, while using nav, without any rows, and columns, navigation is taking whole site width, nothing more.
So, is there any other way, to center site, like this?
Remove all col's from around the Navbar and add a container instead.
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<!--Your nav content here-->
</nav>
</div>
I'm not sure exactly how it is destroying the site's width but try this adding this to your styling:
*{
margin: 0;
padding: 0;
}
I am making a project and I added a dropdown menu on navbar and I don't know why that it seems to push the HTML down when toggled. I tried making its position absolute and top 100% but it didn't work, I am using bootstrap, please help
Here is the main HTML
body{
background-color: #0b0c10!important;
}
nav{
background-color: #0b0c10!important;
color: turquoise;
text-decoration: none;
}
nav a{
color: turquoise;
text-decoration: none;
font-family: 'Lato', sclear-serif;
}
nav a:hover{
color: white;
text-decoration: none;
transition-duration: 1s;
}
.navbar-nav{
margin-right: 80px;
text-align: center!important;
}
.navbar-brand{
font-family: 'Lobster', cursive;
font-size: 30px!important;
margin-left: 30px;
}
<nav class="navbar navbar-expand-lg ">
<a class="navbar-brand" href="#">Calcify</a>
</button>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
More Calculator
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</nav>
I think you have to add jquery.min.js, popper.min.js, and bootstrap.min.js.
In the introduction of Bootstrap it states which imports you need to add. https://getbootstrap.com/docs/4.0/getting-started/introduction/#quick-start
You have to add some scripts in order to get bootstrap fully working. It's important that you include them in this exact order. Popper.js is one of them:
<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.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
Example
<nav class="navbar navbar-expand-lg ">
<a class="navbar-brand" href="#">Calcify</a>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
More Calculator
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul></nav>
.navbar{
align-items: stretch!important;
}
.navbar-nav .dropdown-menu {
position: absolute;
}
for your dropdown items list use
position:absolute;
z-index:5;
this worked for me
body{
background-color: #0b0c10!important;
}
nav{
background-color: #0b0c10!important;
color: turquoise;
text-decoration: none;
}
nav a{
color: turquoise;
text-decoration: none;
font-family: 'Lato', sclear-serif;
}
nav a:hover{
color: white;
text-decoration: none;
transition-duration: 1s;
}
.navbar-nav{
margin-right: 80px;
text-align: center!important;
}
.navbar-brand{
font-family: 'Lobster', cursive;
font-size: 30px!important;
margin-left: 30px;
}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Calcify</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 mr-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
More Calculator
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
</div>
</nav>
I have a menu with a dropdown in it. When I click the dropdown, the child menu is a color. I'm trying to maintain that same color (which is in the hover state) in the parent item, after the menu is clicked. Currently, when I click the dropdown and then mouse away from the parent, it changes back to the original menu color (pink).
Best to open the snippet in full page to see what I'm referring to.
.dropdown-menu {
border: none;
border-radius: 0;
margin: 0;
}
.dropdown-item:hover {
background-color: #0091c7;
}
.nav-link.dropdown-toggle:hover {
background-color: #00aeef;
}
.nav-link > li.dropdown-toggle.active > a,
.nav-link > li.dropdown-toggle.active > a:focus,
.nav-link > li.dropdown-toggle.active > a:hover {
background-color: #00aeef;
color: #ffffff;
}
.bg-pink {
background-color: #d60c8c;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container p-5">
<div class="row">
<div class="col-sm-12">
<!-- HTML here -->
<div class="container-fluid bg-pink">
<div class="container">
<div class="row">
<div class="col-lg-12">
<nav class="navbar navbar-expand-lg navbar-light bg-pink">
<a class="navbar-brand" href="#">
<i class="fa fa-home fa-2x"></i>
</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 mr-auto">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown Parent
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown" style="background-color: #00aeef;">
<a class="dropdown-item" href="#">Action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Action Action</a>
<a class="dropdown-item" href="#">Action Action</a>
<a class="dropdown-item" href="#">Action Action</a>
<a class="dropdown-item" href="#">Action Action Action</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
When the dropdown is clicked, the show class is added to its parent li.
I added
li.show > #navbarDropdown,
to your css here:
li.show > #navbarDropdown,
.nav-link > li.dropdown-toggle.active > a,
.nav-link > li.dropdown-toggle.active > a:focus,
.nav-link > li.dropdown-toggle.active > a:hover {
background-color: #00aeef;
color: #ffffff;
}
I have a problem with my drop down menu on smaller devices. i cannot make it scroll able when i tried the solution here (that is overflow:auto/ overflow-y:scroll) it's not working even if i use !important. What i'm able to scroll was my main page even if the drop down is open.
<nav class="navbar navbar-toggleable-sm ">
<button class="navbar-toggler navbar-toggler-right main-navbar-toggler" type="button" data-toggle="collapse" data-target="#main-nav-collapse" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="fa fa-bars"></span>
</button>
<a class="navbar-brand animation" data-animation ="fadeInLeft" href="#"><img src="/093017/img/logo-tmi.png" alt="logo"/></a>
<div class="collapse navbar-collapse" id = "main-nav-collapse" >
<ul class="nav navbar-nav navbar-main mr-auto mt-2 mt-md-0 animation" data-animation = "fadeInRight">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Vehicles</a>
<div class="dropdown-menu default-menu main-menu sm-main-menu animation" data-animation = "fadeIn">
<!-- Nav tabs -->
<div class="sm-main-nav" >
<a class="dropdown-item" href="#">Cars</a><hr>
<a class="dropdown-item" href="#">Vans & Pickup</a><hr>
<a class="dropdown-item" href="#">SUVs & Crossover</a><hr>
<a class="dropdown-item" href="#">MPVs</a><hr>
<a class="dropdown-item" href="#">Hybrid</a><hr>
<a class="dropdown-item" href="#">Performance</a>
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link " href="#">Owners</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Shop</a>
</li>
<li class="nav-item">
<a class="nav-link " href="#">Promotions</a>
</li>
</ul>
</div>
just you can use #media in your css
#media (max-width: 500px) {
.dropdown-menu{
height:200px;
overflow-y:auto;
}
}
Use max-height instead of height:
.dropdown-menu {
max-height: 200px;
overflow-y: auto;
}
What I can suggest is in place of hard coding height in pixels you can take the advantage of the view port height like adding this style to your dropdown-menu item or add at the class level
Inline
style="overflow-y:auto; max-height:80vh"
For every dropdown
.dropdown-menu {
overflow-y:auto;
max-height:80vh
}
You can use a custom class for scroll bar for dropdown list.
<div class="sm-main-nav customClassForDropDown" >
<a class="dropdown-item" href="#">Cars</a><hr>
<a class="dropdown-item" href="#">Vans & Pickup</a><hr>
<a class="dropdown-item" href="#">SUVs & Crossover</a><hr>
<a class="dropdown-item" href="#">MPVs</a><hr>
<a class="dropdown-item" href="#">Hybrid</a><hr>
<a class="dropdown-item" href="#">Performance</a>
</div>
This is the custom css class.
.customClassForDropDown
{
height: 100px;
overflow-y: auto;
}
check this JSFIDDLE
<li class="nav-item active dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
I've wrote this code to create a dropdown with bootstrap (very easy) now I'm trying to remove the background when I hover over the item inside the dropdown but somehow they always stay white.
This is the css
.dropdown-menu{
background-color: transparent;
border: transparent;
border: none;
}
.dropdown-menu .dropdown-item > li > a:hover {
background-image: none;
background-color: #000;
}
add .bg-transparent to the .dropdown-item elements. Done.
easiest way to do this is take a new class inside drop down-menu and apply css
check this fiddle, updated fiddle https://jsfiddle.net/rb87gwzj/4/ or this snippet
.dropdown-menu{
background-color: transparent;
border: transparent;
border: none;
}
.dropdown-menu .dropdown-item > li > a:hover {
background-image: none;
background-color: #000!important;
}
.navbar {
background: none;
}
.dropdown-content a:hover {
background-color: transparent;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="index.html">
<img src="images/logo.png" width="30" height="30" class="d-inline-block align-top" alt="">
</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item active dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Projects
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink" >
<div class="dropdown-content">
<a class="dropdown-item" href="#html">HTML5/CSS</a>
<a class="dropdown-item" href="#python">Python</a>
<a class="dropdown-item" href="#php">PHP</a>
<a class="dropdown-item" href="#java">Java</a>
<a class="dropdown-item" href="#csharp">C#</a>
</div>
</div>
</li>
</ul>
</div>
</nav>
you applied CSS on dropdown-menu .dropdown-item > li > a:hover but you are structure is not same, you have no dropdown-item inside drop down-menu, no li inside dropdown-item, tell me if you want me explain more
Use
background-color: transparent !important;
or
Use same color which one is used in default case.
Where you want to change.