Nav Bar Dropdown moves to the left side of screen when parent is clicked - html

I have a nav bar with dropdowns that appear when hovered over. That part works perfectly. However if you click on the parent or element the dropdown moves to the far left of the screen. I want it to just stay where it is even if this is clicked on. I'll include my html and css code below. Any help getting this element to stay where it is when the screen is expanded would be helpful. I am using bootstrap 5 by the way.
navbar.html
<nav class="navbar navbar-expand-md">
<a class="navbar-brand" href="/">
<img class="navbar-logo" src="{% static 'site_base/images/image.png' %}" alt="Logo">
</a>
<button class="navbar-toggler navbar-dark" type="button" data-bs-toggle="collapse" data-bs-target="#main-navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse text-end" id="main-navigation">
<ul class="navbar-nav ms-auto d-flex justify-content-end">
<li class="nav-item">
<p class="nav-link dropdown-toggle" data-bs-toggle="dropdown">Dropdown 1</p>
<ul class="dropdown-menu text-end dropdown-menu">
<li class="dropdown-item">Sub-item-1</li>
<li class="dropdown-item">Sub-item-2</li>
<li class="dropdown-item">Sub-item-3</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Products</a>
<ul class="dropdown-menu text-end">
<li class="dropdown-item">Product 1</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Resources</a>
<ul class="dropdown-menu text-end">
<li class="dropdown-item">User Guides</li>
<li class="dropdown-item">Videos</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="/company">Company</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact-us">Contact Us</a>
</li>
</ul>
</div>
</nav>
style.css
#media all and (min-width: 992px) {
.navbar .nav-item .dropdown-menu {
display: none;
}
.navbar .nav-item:hover .nav-link {
}
.navbar .nav-item:hover .dropdown-menu {
display: block;
}
}
/* ============ desktop view .end// ============ */
/* ============ small devices ============ */
#media (max-width: 991px) {
.dropdown-menu .dropdown-menu {
margin-left: 0.7rem;
margin-right: 0.7rem;
margin-bottom: 0.5rem;
}
}

Related

Hiding Sub Menus In A Navigation Bootstrap Explanation [duplicate]

This question already has answers here:
Bootstrap dropdown sub menu missing
(11 answers)
Closed 1 year ago.
I am looking for an explanation on how the CSS selectors work to make it so that the Sub Menu is only shown when the parent element for that Sub Menu is hovered over. To my understanding there is a pure CSS solution for this with the use of :hover and the CSS display property however I do not understand how to use the two together to make the sub menu do what I want it to do. I have a hunch that the .dropdown-menu class is overwriting the display:hidden within the CSS for the submenu.
I have tried adding in some CSS in the selector .navbar .nav-item .submenu { display: hidden; position: absolute; left:100%; top:35px;} to hide the Sub Menu in the navbar. Once the Sub Menu is hidden I should be able to just show it again by using :hover on the parent element but I am unsure which element that would be as I have tried a mix of CSS selectors with no avail.
Here is the code for the page:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
</head>
<body style="">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark" style="background-color: black;">
<div class="container-fluid">
<a class="navbar-brand" href="#">Home</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main_nav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="main_nav">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> People </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href="#">Professional Institutions</a></li>
<li class="dropdown-submenu">
<li><a class="dropdown-item text-white" href="#">My New Drop Down</a>
<ul class="submenu dropdown-menu">
<li><a class="dropdown-item text-white" href="#">Sub Area 1</a></li>
<li><a class="dropdown-item text-white" href="#">Sub Area 2</a></li>
<li><a class="dropdown-item text-white" href="#">Sub Area 3</a></li>
</ul>
</li>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> Products </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href=""> Actuation Systems</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Knowledge </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href=""> A S Knowledge</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> Policy Deployment </a>
<ul class="dropdown-menu">
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown"> Processes And Procedures </a>
<ul class="dropdown-menu">
<li><a class="dropdown-item text-white" href="#"> Quality Management System</a></li>
<li><a class="dropdown-item text-white" href="#"> Buissness Management System </a></li>
</ul>
</li>
</ul>
</div> <!-- navbar-collapse.// -->
</div> <!-- container-fluid.// -->
</nav>
<style>
#media all and (min-width: 1200px) {
.nav-link {
font-size: 25px
}
.navbar .nav-item .dropdown-menu {
display: hidden;
font-size: 20px
}
.navbar .nav-item:hover .dropdown-menu {
display: block;
background: rgba(66, 66, 66, 0.4)
}
.dropdown-menu>li:hover {
background-color: black
}
.dropdown-menu .dropdown-item:hover {
background-color: black;
}
.navbar .nav-item .submenu {
display: hidden;
position: absolute;
left: 100%;
top: 35px;
}
}
</style>
</body>
</html>
Here is the code again: https://jsfiddle.net/p0etz8jg/
How it works:
The collapse JavaScript plugin is used to show and hide content.
Buttons or anchors are used as triggers that are mapped to specific
elements you toggle. Collapsing an element will animate the height
from its current value to 0. Given how CSS handles animations, you
cannot use padding on a .collapse element. Instead, use the class as
an independent wrapping element.
Bootstrap Collapse

What css must I apply to get rid of this useless spacing?

I have a navbar that has some items, 4 of them to the left and one that must always be in the right. The thing is when I introduced the last one to the left, they got one word on top of the other and it looks awful. And one more thing is that when I change the resolution of the monitor, the item that's always on the right moves to the left almost touching the middle.
Some additional info would be that this is part of a project consisting in a web platform for colleges. It is mainly created with SpringBoot and its libraries, fully Java written in backend. Nonetheless, the frontend is mostly HTML, some CSS when I need to make it not look ugly and Javascript in case of animatios of effects. Also not to mention Thymeleaf framework so I can communicate with the backend properly.
I tried many things but I have no idea how to make them look nice. Someone willing to help me on this stylish matter?
I will add two pictures:
this one is when I have my browser put on the 24inch screen
this one is when I have my browser put on the 15.6inch screen of the laptop
And the code below is from the page:
#personal-page {
padding-left: 780px;
font-size: 20px;
}
#admin-page {
padding-left: 730px;
font-size: 20px;
}
#assistant-page {
padding-left: 700px;
font-size: 20px;
}
<html>
<head>
<nav class="fixed-top navbar navbar-dark bg-dark navbar-expand">
<a class="navbar-brand" href="https://curs.upb.ro/">
<span class="fa fa-university"></span> E-Learning Platform
</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" id="navi">
<li class="nav-item active">
<a class="nav-link" href="/">Homepage<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="/menu">Menu Page</a>
</li>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="/admin/students">Students List</a>
</li>
<li class="nav-item">
<a class="nav-link" aria-current="page" href="/admin/camine">Camine Page</a>
</li>
<div th:switch="${isDevAcc}">
<li th:case="'true'" class="nav-item">
<div th:switch="${loggedUsername}">
<a th:case="'iancu'" id="admin-page" class="nav-link fas fa-code" aria-current="page" href="/admin/devAdminPage">
<span> Dev Account :: ADMIN </span>
</a>
<a th:case="'lixi'" id="assistant-page" class="nav-link fas fa-code" aria-current="page" href="/admin/devAdminPage">
<span> Dev Account :: ASISTENT </span>
</a>
</div>
</li>
<li th:case="'false'" class="nav-item" id="personal-page">
<a class="nav-link fas fa-user-secret" aria-current="page" href="#" th:text="${loggedUsername}"></a>
</li>
</div>
</ul>
</div>
</nav>
</head>
<body>
</body>
</html>
Use flexbox to align your navbar horizontally: nav ul { display: flex; }
Then you also can use the :last-child selector to apply margin-left: auto; which will push the last item to the right: nav ul li:last-child { margin-left: auto; }. margin: auto; within a flex-container will consume all remaining space.
nav ul {
display: flex;
}
nav ul li:last-child {
margin-left: auto;
}
/* For styling purpose only */
nav ul {
list-style: none;
padding: 5px;
}
nav li {
margin: 0 10px;
}
<nav>
<ul>
<li>E-Learning Plattform</li>
<li>Homepage</li>
<li>Menu Page</li>
<li>Students List</li>
<li>Camine Page</li>
<li>Admin</li>
</ul>
</nav>

Cannot make Bootstrap navbar transparent

I have been trying to make my navbar transparent but so far nothing has worked for me. Can someone teach me how to do it? I assume it has something to do with the space that it is taking up in regards to the body content, but I am not sure what can be done as I have already tried reversing the div tags to be outside of the body.
This is my desired result :
Fiddle : https://jsfiddle.net/zr1qh780/
My HTML :
<body>
<div id="container">
<!-- Navigation menu -->
<header>
<nav class="my-nav navbar navbar-light navbar-expand-lg">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-
target="#navbarsExample08" aria-controls="navbarsExample08" aria-expanded="false" aria-
label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-md-center" id="navbarsExample08">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="index.html" id="dropdown08" data-toggle="dropdown" aria-
haspopup="true" aria-expanded="false">ACTIVITÉS</a>
<div class="dropdown-menu" aria-labelledby="dropdown08">
<a class="dropdown-item" href="rafting.html">Rafting</a>
<a class="dropdown-item" href="canyoning.html">Canyoning</a>
<a class="dropdown-item" href="saut-parachute.html">Saut en parachute</a>
<a class="dropdown-item" href="soufflerie.html">Simulation de chute libre</a>
<a class="dropdown-item" href="saut-elastique.html">Saut à l’élastique</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">OFFRES</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">ÉVÉNEMENTS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">À PROPOS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">ESPACE HANDICAP</a>
</li>
<li class="nav-item">
<a class="nav-link contact" href="contact.html">CONTACT</a>
</li>
</ul>
</div>
<div class="hamburger">
<span style="font-size:30px;cursor:pointer;color: white;" onclick="openNav()">☰</span>
</div>
</nav>
</header>
<div id="myNav" class="overlay">
×
<div class="bar overlay-content">
ACTIVITÉS
OFFRES
ÉVÉNEMENTS
À PROPOS
ESPACE HANDICAP
CONTACT
</div>
</div>
My CSS:
.logo {
margin-right:5px;
}
.my-nav{
display: flex;
align-items: center;
justify-content: space-around;
padding: 8px;
}
.navbar-light .navbar-nav .nav-link {
color: white;
}
.navbar-nav > li {
padding-left:25px;
padding-right:45px;
float:none;
display:table-cell;
text-align:center;
font-size: 15px;
font-weight: 500;
}
.navbar-light .navbar-nav .nav-link:focus, .navbar-light .navbar-nav .nav-link:hover {
color: #fc3218;
}
.nav-link:active {
color: #fc3218;
}
.navbar {
background: grey
}
.dropdown-menu {
color: white;
background-color: #fc3218;
}
.dropdown-item {
color: white;
font-size: 14px;
}
.navbar-collapse {
flex-grow: 0.3;
}
Have you tried something like this?
navbar{
background-color: transparent;
}
If that doesn't work, add !important to break the predefined bootstrap styles
Set the opacity of the container for nav bar to 0.
Note: Place the elements of nav bar in different div otherwise the text in nav bar will also get effected.

Hover menu disappear too fast

I got a problem, I do not know what is the problem with the hover menu but it disappears too fast
<div class="dropdown">
<li class="nav-item log-main">
Member
<ul class=" dropdown-menu log">
<li>Sign up</li>
<li>Log in</li>
</ul>
</li>
</div>
css:
.navbar .nav-item ul.log{
display: none;
list-style-type:none;
}
.navbar .dropdown-menu:hover ul.log{
display: block;
}
.log a{
color:var(--main-text-color);
font-family:var(--main-text-font);
font-size: 24px;
}
You need to use :hover pseudo class on .log-main:hover to show the next ul which will be .dropdown-menu
Live Demo:
.navbar .nav-item ul.log {
display: none;
list-style-type: none;
}
.log-main:hover .dropdown-menu {
display: block;
margin-top: 0;
}
.log a {
color: var(--main-text-color);
font-family: var(--main-text-font);
font-size: 24px;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="dropdown">
<li class="nav-item log-main">
Member
<ul class="dropdown-menu log">
<li>Sign up</li>
<li>Log in</li>
</ul>
</li>
</div>
You need to add hover to the element that is visible and the dropdown that is hidden.
.dropdown:hover .dropdown-menu,
.dropdown .dropdown-menu:hover{
display: block;
position: absolute;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="navbar-nav">
<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
</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>
</ul>
</nav>

Centering navbar links in bootstrap

I want to center my links on mobile view (md and down view) of my navbar but I can't seem to find a solution for it. I am using bootstrap v4-alpha
<div class="container-fluid p-b-3">
<nav class="navbar navbar-full navbar-fixed-top navbar-light bg-faded">
<ul class="nav navbar-nav">
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" data-toggle="modal" data-target="#myModal" style="cursor:pointer;">LINK 3</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#kontakti">LINK 2</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#produktet">LINK 1</a>
</li>
</ul>
</nav>
</div>
Here's the codepen link
You can achieve it using flexbox.
Here's a working pen I created: http://codepen.io/anon/pen/bwbpNx
CSS
.navbar-nav {
display: flex;
align-items: center;
justify-content: center;
}
I think this is one of the proper way to do it. So that, if you add another link. It will remain at center. Hope it helps. Cheers!
Use media query and override styles to make your links center aligned.
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css');
#media (max-width: 767px) {
.navbar ul {
text-align: center;
}
.navbar ul li {
display: inline-block;
vertical-align: top;
padding: 0 10px;
}
.navbar ul li a {
margin-left: 0 !important;
}
}
<div class="container-fluid p-b-3">
<nav class="navbar navbar-full navbar-fixed-top navbar-light bg-faded">
<ul class="nav navbar-nav">
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" data-toggle="modal" data-target="#myModal" style="cursor:pointer;">LINK 3</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#kontakti">LINK 2</a>
</li>
<li>
<a class="nav-item nav-link pull-xs-right m-l-2 font-weight-bold" href="#produktet">LINK 1</a>
</li>
</ul>
</nav>
</div>