Make link clickable, but also have the dropdown? - html

I have an navigation bar, where I have an "nav-item" with a dropdown function.
But what I want is the "button / nav-item", that is visible in the navb-bar to be clickable and have a redirect to another page, and then the dropdown arrow should be on the right side, where you can click on the arrow and then get the dropdown menu.
I have two approaches:
1.
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Navbar Toggle -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar Toggle END -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<!-- Navbar Items Links-->
<ul class="navbar-nav">
<!-- Home -->
<li class="nav-item">
<a class="nav-link" href="../Startseite/Index.jsp">Home <i class="fas fa-home"></i></a>
</li>
<!-- Gruppen Dropdown-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#gruppen" id="navbardrop1" data-toggle="dropdown">Gruppen <i class="fas fa-users"></i></a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#gruppe1">Gruppe 1</a>
<a class="dropdown-item" href="#gruppe2">Gruppe 2</a>
<a class="dropdown-item" href="#gruppe3">Gruppe 3</a>
</div>
</li>
<!-- Zahlungen -->
<li class="nav-item"><a class="nav-link" href="../Zahlungsuebersicht/Zahlungsuebersicht.jsp">Zahlungen
<i class="fas fa-receipt"></i>
</a></li>
</ul>
<!-- Navbar Items Rechts-->
<ul class="navbar-nav ml-auto">
<!-- Profil -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#asd" id="navbardrop2" data-toggle="dropdown">Benutzer <i class="fas fa-user"></i></a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#asd">Profil bearbeiten <i
class="fas fa-user-edit"></i>
</a> <a class="dropdown-item" href="#asd">Abmelden <i class="fas fa-sign-in-alt"></i>
</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link active" href="../Registrieren/Registrieren.jsp">Registrieren <i class="fas fa-sign-in-alt"></i></a>
</li>
</ul>
<!-- Navbar Items END -->
</div>
</nav>
This one is from the design what I am looking for, but I would like the element "Gruppen" clickable, so it redirects you to another page.
2
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Navbar Toggle -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar Toggle END -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<!-- Navbar Items Links-->
<ul class="navbar-nav">
<!-- Home -->
<li class="nav-item">
<a class="nav-link" href="../Startseite/Index.jsp">Home <i class="fas fa-home"></i></a>
</li>
<!-- Gruppen Dropdown-->
<li class="nav-item">
<a class="nav-link" href="#gruppen">Gruppen <i class="fas fa-home"></i></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop1" data-toggle="dropdown"></a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#gruppe1">Gruppe 1</a>
<a class="dropdown-item" href="#gruppe2">Gruppe 2</a>
<a class="dropdown-item" href="#gruppe3">Gruppe 3</a>
</div>
</li>
<!-- Zahlungen -->
<li class="nav-item"><a class="nav-link" href="../Zahlungsuebersicht/Zahlungsuebersicht.jsp">Zahlungen
<i class="fas fa-receipt"></i>
</a></li>
</ul>
<!-- Navbar Items Rechts-->
<ul class="navbar-nav ml-auto">
<!-- Profil -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#asd" id="navbardrop2" data-toggle="dropdown">Benutzer <i class="fas fa-user"></i></a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="#asd">Profil bearbeiten <i
class="fas fa-user-edit"></i>
</a> <a class="dropdown-item" href="#asd">Abmelden <i class="fas fa-sign-in-alt"></i>
</a>
</div>
</li>
<!-- /c:if -->
<li class="nav-item">
<a class="nav-link active" href="../Registrieren/Registrieren.jsp">Registrieren <i class="fas fa-sign-in-alt"></i></a>
</li>
</ul>
<!-- Navbar Items END -->
</div>
</nav>
This one solution is really dirty in my opinion.
Isn't there a clean way to achieve a proper solution?
I'm pretty new to all HTML, Bootstrap and CSS.
EDIT: I have no idea why, but i have run the two exact same code snippets on my local server, and the outcome was different... I want it to like the second approach, but without the arrow being a navitem by it's own, but when I do it, the dropdown arrow is at the top of the navbar, like above all the text. is there a way to fix that?

Your current code structure simplified:
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop1" data-toggle="dropdown">
Gruppen <i class="fas fa-users"></i>
</a>
<div class="dropdown-menu">
...
</div>
</li>
As I understood - the aim is to make <i class="fas fa-users"></i> a separate dropdown link and the style should not degrade.
This will solve your issue:
<li class="nav-item dropdown nav-link">
<a href="/#yourlink">
Gruppen
</a>
<a class="dropdown-toggle" href="#" id="navbardrop1" data-toggle="dropdown">
<i class="fas fa-users"></i>
</a>
<div class="dropdown-menu">
...
</div>
</li>
The trick is in the nav-link class, which makes the propper positioning of your menu element. Moving it to the <li> element may have caused some conflicts in styles if the CSS was more complex, so be careful with moving class from one element to another in future.
Note: you must understand that the dropdown menu should not be made the way you want them now, because interacting with it the average user expects a dropdown after the click, not the redirect to some external page.

Related

Bootstrap 5 Navbar collapse and dropdown

I'm having a little trouble with my navbar, using bootstrap 5. The navbar doesn't collapse after expanding and the dropdown list doesn't drop.
<!-- Bootstrap-5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!-- Body -->
<nav class="navbar navbar-expand-md bg-light sticky-top">
<div class="container-fluid ">
<a class="navbar-brand" href="#"><img class="" width="100px" src="icons/cake/Asset 4.png"></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
<span class="fa fa-navicon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" href="#">SERVICES</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Delivery</a></li>
<li>
<a class="dropdown-item" href="#">Event Planning</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#">CONTACT</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><button class="btn btn-danger">ORDER ONLINE</button></a>
</li>
</ul>
</div>
</div>
</nav>
Many of Bootstrap's components require the use of JavaScript to function. Including navbars and dropdowns. I added the relevant source for both font-awesome and the Bootstrap JS.
Read more here on Bootstrap 5 Introduction.
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<nav class="navbar navbar-expand-md bg-light sticky-top">
<div class="container-fluid ">
<a class="navbar-brand" href="#"><img class="" width="100px" src="icons/cake/Asset 4.png"></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
<span class="fa fa-navicon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown" href="#">SERVICES</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Delivery</a></li>
<li>
<a class="dropdown-item" href="#">Event Planning</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#">CONTACT</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#"><button class="btn btn-danger">ORDER ONLINE</button></a>
</li>
</ul>
</div>
</div>
</nav>

Font awesome book reader icon not loading

Im trying to use font awesome icon on my page beside the navbar brand. But for some reason its not loading. Need Help
<script src="https://use.fontawesome.com/e017101791.js"></script>
This is the script tag that font awesome mailed me and the one i'm using in my code
In the font awesome website the html for book-reader icon is
<i class="fas fa-book-reader"></i>
But in my code if i use 'fas' with 's' its not working so i had to write the class name as 'fa fa-book-reader'
In the footer I'm using facebook, twitter, instagram icon they are all loading perfectly after writing their class names as 'fa fa-facebook' instead of 'fab fa-facebook'.
.HTML
<nav class="navbar navbar-expand-lg">
<a class="navbar-brand" href="#">
<i class="fa fa-book-reader fa-3x mx-3"></i>
Books</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNavAltMarkup"
aria-controls="navbarNavAltMarkup"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="mr-auto"></div>
<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="#">Course</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Price</a>
</li>
<li class="nav-item dropdown">
<div class="dropdown">
Pages
<div class="dropdown-content">
Generic
Element
</div>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Fact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</nav>
My Webpage Pic
Expected Webpage Output
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
Use this cdn type and you will be good to go.
Include this in the head section of your page:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css">

Bootstrap 4 navbar/menu keep entry visible when collapsed

This is a question similar to this one, but it is for Bootstrap 4.
I am not able to add right-justified entries on the BS4 navbar that stay visible both when collapsed and not collapsed. I spent one full afternoon without success.
This is my goal:
Here my current code (that works only when not collapsed):
<nav class="navbar navbar-light navbar-expand-xl border-bottom mainmenu sticky-top">
<a class="navbar-brand text-capitalize text-blur" href="/">
<img class="mr-1" src="/images/logo/favicon-32x32.png" alt="Logo">
<span class="">Portami in Pista</span>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#myTopMenu"
aria-controls="collapsableTopMen" aria-expanded="false" aria-label="[Menu]">
<span class="sr-only">[Menu]</span>
<span class="navbar-toggler-icon" title="[Menu]"/>
</button>
<div class="collapse navbar-collapse" id="collapsableTopMen">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/Schools/">
<i class="fa fa-motorcycle fa-rotate-315 text-danger" aria-hidden="true"/>
<span class="text-blur-danger">PiP Reparto Corse</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Tracks/">
<i class="fa fa-flag-checkered text-primary" aria-hidden="true"/>
<span>Piste</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Blog/le-guide-del-giovedi/">
<i class="fa fa-mortar-board text-primary" aria-hidden="true"/>
<span>Le guide del giovedì</span>
</a>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<!- START of section should be always visible -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownLang" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="famfamfam-flags it" title="Italiano"/>
<span class="d-inline d-xl-none">Italiano</span>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownLang">
<a class="dropdown-item" href="/AbpLocalization/ChangeCulture?cultureName=en&returnUrl=/">
<i class="famfamfam-flags gb" aria-hidden="true"/>
<span class="">English</span>
</a>
</div>
</li>
<!- END of section should be always visible -->
<li class="nav-item">
<a class="nav-link" href="/Account/Login"><i class="fa fa-sign-in"/> Log in</a>
</li>
</ul>
</div>
</nav>
Since you're using Bootstrap 4, this answer is more relevant to your question:
https://stackoverflow.com/a/41513784/171456 (see the last part)
The part that you always want to keep visible needs to be separate from any of the collapsible parts. Then use the order-* classes to position the items as desired:
Demo: https://codeply.com/p/ylDhhZtpiH
<nav class="navbar navbar-light navbar-expand-xl border-bottom mainmenu sticky-top justify-content-start">
<a class="navbar-brand text-capitalize text-blur" href="/">
<img class="mr-1" src="//placehold.it/32" alt="Logo">
<span class="">Portami in Pista</span>
</a>
<button class="navbar-toggler order-2 ml-1" type="button" data-toggle="collapse" data-target=".collapsable" aria-controls="collapsableTopMen" aria-expanded="false" aria-label="[Menu]">
<span class="sr-only">[Menu]</span>
<span class="navbar-toggler-icon" title="[Menu]"></span>
</button>
<!-- 1st collapse menu -->
<div class="collapse navbar-collapse collapsable flex-grow-0 flex-xl-grow-1 order-last" id="collapsableTopMen">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/Schools/">
<i class="fa fa-motorcycle fa-rotate-315 text-danger" aria-hidden="true"></i>
<span class="text-blur-danger">PiP Reparto Corse</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Tracks/">
<i class="fa fa-flag-checkered text-primary" aria-hidden="true"></i>
<span>Piste</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/Blog/le-guide-del-giovedi/">
<i class="fa fa-mortar-board text-primary" aria-hidden="true"></i>
<span>Le guide del giovedì</span>
</a>
</li>
</ul>
</div>
<!-- always visible portion -->
<ul class="navbar-nav order-1 order-xl-last ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownLang" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-flag it" title="Italiano"></i>
<span class="d-inline d-xl-none">Italiano</span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownLang">
<a class="dropdown-item" href="#">
<i class="fas fa-flag-usa gb" aria-hidden="true"></i>
<span class="">English</span>
</a>
</div>
</li>
</ul>
<!-- 2nd collapse menu -->
<div class="collapse navbar-collapse collapsable flex-grow-0 order-last">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/Account/Login"><i class="fa fa-sign-in"></i> Log in</a>
</li>
</ul>
</div>
</nav>
https://codeply.com/p/ylDhhZtpiH
I did a small important fix to the good answer from Zim.
I fixed the "always visible" portion of the menu as he did not overlap the whole menu when it was collapsed.
Adding a custom .navbar-always-overlapped class, fixed it.
<nav class="navbar navbar-light navbar-expand-lg border-bottom mainmenu sticky-top justify-content-start">
<!-- same code from Zim here>
...
<!-- always visible portion. Note the 'navbar-always-overlapped' -->
<ul class="navbar-nav navbar-always-overlapped order-1 order-lg-last ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownLang" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-flag it" title="Italiano"></i>
<span class="d-inline d-lg-none">Italiano</span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownLang">
<a class="dropdown-item" href="#">
<i class="fas fa-flag-usa gb" aria-hidden="true"></i>
<span class="">English</span>
</a>
</div>
</li>
</ul>
<!-- 2nd collapse menu -->
<div class="collapse navbar-collapse collapsable flex-grow-0 order-last">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/Account/Login"><i class="fa fa-sign-in"></i> Log in</a>
</li>
</ul>
</div>
</nav>
and the code for .navbar-always-overlapped:
.navbar-always-overlapped .dropdown-menu {
position: absolute !important;
}
See https://codeply.com/p/9deLYTGblZ

How do I add a submenu to a horizontal HTML Navbar

I am trying make one of the navbar dropdowns into a sub-menu that unfolds either to the left or right depending on the space.
How do I do this? I've tried everything.
<nav class="navbar navbar-expand-sm bg-light navbar-light">
<!-- Brand -->
<a class="navbar-brand" href="#">PRODUCT NAME</a>
<!-- Links -->
<ul class="navbar-nav">
<!--A regular navbar link-->
<li class="nav-item">
<!--a class="nav-link" href="resources/pages/home.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown" >Images</a>
<div class="dropdown-menu">
<!--EDIT THESE ITEMS FOR FLOORS/PAGES-->
<a class="dropdown-item" href="resources/callout/tore.html" target="iframe1">PAGE NAME</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">Videos</a>
<div class="dropdown-menu">
<!--EDIT THESE ITEMS FOR VIDEOS-->
<a class="dropdown-item" href="resources/media/vids/" target="_blank">VIDEO NAME</a>
</div>
</li>
<!--Dropdown for additional documents-->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">Additional Documents</a>
<div class="dropdown-menu">
<!--EDIT THESE ITEMS FOR DOCUMENTS-->
<a class="dropdown-item" href="Resources/media/docs/" target="">DOCUMENT NAME</a>
</div>
</li>
</ul>
</nav>

Bootstrap Navbar Toggle Opens Upward? Toggle Won't Close On Click?

I am using MDBootstrap(essentially Bootstrap 4) Navbar Collapse, however the toggle button won't collapse when clicking on an element. I've reached out to MDBootstrap themselves and can't seem to get an answer. I've been told to use Angular, but I'm using JQuery and I have read that you shouldn't mix those two together. Also, I don't know how I would incorporate Angular into my website. I've used Bootstrap before and never had to use Angular just to get the toggle button to close once clicked on. Does this sound familiar to anyone?
Lastly, when I click the toggle button, it opens upward rather than down- can anyone help with that as well, I would like it to open up downward.
My code is here:
<nav class="navbar fixed-top navbar-expand-lg navbar-dark scrolling-navbar" style="height: 70px;">
<div class="container">
<!-- Brand -->
<a class="navbar-brand" href="index.php"><img src="img/andrea1.jpg" class="rounded-circle img-fluid">
</a>
<!-- Collapse -->
<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>
<!-- Links -->
<div class="collapse navbar-collapse text-center" id="navbarSupportedContent">
<!-- Left -->
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="#about">About
<span class="sr-only">(current)</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#port">Portfolio</a>
</li>
</ul>
<!-- Right -->
<ul class="navbar-nav nav-flex-icons">
<li class="nav-item">
<a href="https://www.linkedin.com/in/andreasanchez2/" class="nav-link" target="_blank" title="Follow on LinkedIn">
<i class="fa fa-linkedin"></i>
</a>
</li>
<li class="nav-item">
<a href="https://github.com/dec23rd1986" class="nav-link" target="_blank" title="Follow on Github">
<i class="fa fa-github"></i>
</a>
</li>
<li class="nav-item">
<a href="https://twitter.com/dec23rd1986" class="nav-link" target="_blank" title="Follow on Twitter">
<i class="fa fa-twitter"></i>
</a>
</li>
<li class="nav-item">
<a href="#contact" class="nav-link" title="Contact">
<i class="fa fa-envelope mr-3"></i>
</a>
</li>
</ul>
</div>
</div>
</nav>
Don't set height on the Navbar. Remove the style="height:70px;".
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark scrolling-navbar">
<div class="container">
...
</div>
</nav>
Working as expected