How to add animation to hamburger menu Bootstrap 4? - html

I am working on a website where I'd like to add an animation to my static navbar from Bootstrap. I want the 3 horizontal bars to change to an X when the button is toggled on mobile viewport.
Note: I am using Bootstrap 4, not 3. Some BS3 classes don't work in BS4
This is what I am trying to accomplish: example. However, I've found multiple tutorials but I am struggling with it. The reason for this is that all those tutorials are based on BS3. BS3 uses the navbar-toggle-class, but BS4 uses the navbar-toggler-class. It's major difference, since the BS4-version contains a viewbox in css:
.navbar-toggler {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(0, 0, 0, 0.5)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
border-color: rgba(0,0,0,.1);
}
And in the bootstrap.min.css the navbar-toggler-class is being used for 11 times. So if I modify this class, it will (probably) break.
HTML of menu:
<div class="container">
<div class="row hidden-lg-up">
<div class="wrapper-right">
<img src="img/placeholders/225x50.png" alt="Logo Company Mobile" class="mobile-only" />
</div>
</div>
<div class="row">
<nav class="navbar navbar-light navbar-full">
<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
<div class="collapse navbar-toggleable-md" id="navbarResponsive">
<a class="navbar-brand vc-parent" href="#">
<img src="img/placeholders/250x50.png" alt="Logo Company Desktop" class="img-fluid desktop-only" />
</a>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item active align-middle">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item align-middle">
<a class="nav-link" href="#">Item</a>
</li>
<li class="nav-item align-middle">
<a class="nav-link" href="#">Item</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
Could anyone point me in the right direction?

Change .navbar-toggler-icon background when menu is opened to
background-image:url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='white' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8l20 16M4 24L24 8'/%3E%3C/svg%3E");

Related

Hamburger menu not showing up on smaller screens for bootstrap 5 navabar

'I've put in the stylesheets for both bootstrap css and js in the file. It shows up when I retain default colour options but not when I customise the colour.'
'''
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">item</a>
</li>
</div>
</div>
</nav>
'''
CSS: I've added a background colour to the body and also made the nabber the same colour. I've made the text pink and added a green hover to navbar text.#
body{
background-color: #FFE57C;
}
.navbar{
padding-left: 350px;
}
.navbar a:hover{
color: #FF7CD8;
}
.navbar a {
color: #FF7CD8;
}
.nav navbar-nav .navbar-center {
color: ##FF7CD8;
}
Try retaining that navbar-light class in inside top nav tag, or you can use navbar-dark if you want. check the documentation here for more details. this class is associated with showing hamburger icon in bootstrap 5. see the code from bootstrap.css :
navbar-light .navbar-toggler-icon {background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%280, 0, 0, 0.55%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");}
but you can change the colors in navbar by adding an id for ul tag and add the properties for id in style, this way we can tweak the default properties given by the bootstrap. You might be able to achieve what you need by this code, check the code below:
html:
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0" id="nav-list">
<li class="nav-item">
<a class="nav-link active" aria-current="page"href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Item</a>
</li>
</ul>
</div>
</div>
</nav>
css:
body{
background-color: #FFE57C;
}
.navbar{
padding-left: 350px;
}
#nav-list a {
color: #FF7CD8;
}
#nav-list a:hover{
color: #FF7CD8;
}
for sample i just put 2 list items in navbar.

How to change bootstrap 5 default menu to off-canvas menu?

I am creating one landing page in Bootstrap 5, where default menu should be replaced by off-canvas menu with a close icon.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Geometry Global</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/theme.css" rel="stylesheet">
<style>
#import url('https://fonts.googleapis.com/css2?family=Montserrat:wght#400;600;700;800;900&display=swap');
body, h1, h2, h3, h4, h5, article, section, div, p, ul, li, ol, a {
font-family: 'Montserrat', sans-serif;
}
h1{
font-size:5.3rem;
font-weight:900;
line-height:0.8;
}
h2{
font-size:2rem;
font-weight:700;
}
body{
max-width:1414px;
margin:0 auto;
}
[x-cloak] { display: none; }
button {vertical-align: middle;}
input#s {border-radius: 0;border-width: 0 0 1px 0;font-size: 1.5rem;}
input#s:focus {outline: none;box-shadow: none;}
.search-panel-wrap {
position: relative;
height: 1px;
}
.search-panel {
background-color: white;
position: absolute;
top: 100%;
left: 0;
width: 100%;
padding: 20px 0;
z-index: 9;
}
</style>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine#v2.8.0/dist/alpine.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body x-data="{s_open: false}">
<section>
<div class="container border-bottom border-1 border-dark">
<div class="row py-3">
<div class="col-sm-6 col-6">
Geometry Global
</div>
<div class="col-sm-6 col-6 d-block d-sm-none">
<div class="text-end">
Explore Geometry Global
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16"> <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/> </svg>
</div>
</div>
</div>
</div>
</section>
<section class="sticky-top bg-white">
<nav class="navbar container navbar-expand-lg navbar-light px-0">
<div class="container-fluid">
<a class="navbar-brand" href="#">Annual Report</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Reports 2020
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Report Architecture</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Report Planning</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Report Development</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
External Links
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="#">Link 1</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Link 2</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Link 3</a></li>
</ul>
</li>
</ul>
<span class="navbar-text d-none d-sm-block">
Download Geometry Global Report
<button class="btn" id="js-toggle-sp" #click="s_open = true" x-show="s_open === false"style="padding:0;height:24px;width:24px;">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search" viewBox="0 0 16 16">
<path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
</svg>
</button>
<button type="button" class="btn-close" aria-label="Close" x-show="s_open" x-cloak></button>
</span>
</div>
</div>
</nav>
<div class="search-panel-wrap">
<div class="search-panel" x-show="s_open" #click.away="s_open = false" x-cloak>
<div class="container search-panel__wrap">
<form action=""><input class="form-control" type="text" name="s" id="s" placeholder="Type here"></form>
</div>
</div>
</div>
</section>
<section class="py-3" style="background-color:#F9ECE4;">
<div class="container">
<div class="row">
<div class="col-sm-4 align-self-center">
<div><h2>STATISTICS <br/>2020</h2></div>
<div>An annual report is a comprehensive report on a company's activities throughout the preceding year. Annual reports are intended to give shareholders and other interested people information about the company's activities and financial performance.</div>
</div>
<div class="col-sm-8 text-center">
<img class="img-fluid" src="https://cdn.pixabay.com/photo/2019/09/28/10/38/medical-4510408_1280.png" alt=""/>
</div>
</div>
</div>
</section>
<section class="py-3">
<div class="container">
<div class="row">
<div class="col-sm-6 align-self-center">
<div class="py-3"><h1>Gross <br/>Income <br/>2020</h1></div>
<div class="py-3">An annual report is a comprehensive report on a company's activities throughout the preceding year. Annual reports are intended to give shareholders and other interested people information about the company's activities and financial performance.</div>
<div class="py-3">This doctype is meant to help our users create various business or project proposals and help them get approved. Proposals themes can be categorised per industry.</div>
</div>
<div class="col-sm-6">
<div class="py-3">An annual report is a comprehensive report on a company's activities throughout the preceding year. Annual reports are intended to give shareholders and other interested people information about the company's activities and financial performance.</div>
<div class="py-3">They may be considered as grey literature. Most jurisdictions require companies to prepare and disclose annual reports, and many require the annual report to be filed at the company's registry. Companies listed on a stock exchange are also required to report at more frequent intervals (depending upon the rules of the stock exchange involved).</div>
<div class="py-3">This doctype is meant to help our users create various business or project proposals and help them get approved. Proposals themes can be categorised per industry.</div>
</div>
</div>
<div class="row py-5">
<div class="col">
© geometry global inc. 2021
</div>
</div>
</div>
</section>
</body>
</html>
I searched some tutorial but was not able to implement close button. I need menu icon to be opened from right as a off-canvas menu with working dropdowns, Any help will be appreciated.
Thanks.
Bootstrap 5 Beta 3 (update 2021)
Introducing the new Bootstrap 5 Offcanvas Component
Bootstrap 5 Beta 2 (original answer)
Off-canvas menus are not an official Bootstrap component, but you can create one by using this "experimental" template as an example. You'll see that it requires additional CSS and JS.

Dropdown bootstrap move all item of my navbar

I've create a navbar for my website. But I don't understand very well how work the positionning items with bootstrap. And I have a problem, when I click on my dropdown button (profil image), all items of my navbar move and I don't want that :
https://www.codeply.com/p/OlyRp40cOH
HTML :
<div class="pos-f-t">
<nav class="navbar fixed-top navbar-custom" style="height: 80px;">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">
<img src="assets/img/logo.png" alt="LOGO" style="height: 50px"> Title
</a>
<form class="mx-2 my-auto d-inline w-50">
<input class="form-control mr-sm-6" type="search" placeholder="Search..." aria-label="Search" id="searchBar">
</form>
<ul class="navbar-nav ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<img src="https://mdbootstrap.com/img/Photos/Avatars/avatar-2.jpg" class="rounded-circle z-depth-0"
alt="avatar image" style="height: 50px; outline-width: 10px; outline-color: white">
</a>
<div class="dropdown-menu"
aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">My account</a>
<a class="dropdown-item" href="#">Disconnect</a>
</div>
</li>
</ul>
</nav>
</div>
CSS :
.navbar-custom {
background-color: #2c3e50;
color: white;
font-weight: bold;
font-variant-caps: small-caps;
}
.navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255,255,255, 1)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}
.navbar-toggler-icon:hover {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255,176,43, 1)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}
.navbar-brand {
margin-left: 2%;
font-size: 30px;
color: white
}
.navbar-brand:hover {
color: #ffb02b;
}
#searchBar {
border-radius: 20px;
font-family:Arial, 'Font Awesome 5 Free'
}
#searchBar:focus {
border-color: #ffb02b;
border-width: 2px;
}
you have a problem with "navbar-nav" class in <ul> change that class with "nav"
<ul class="nav ml-auto">

How to make a collapsible navbar with the links on the right hand side for big screens in Bootstrap?

I have made the navbar in Bootstrap with the links on the right hand side of the screen. Now I want to make the navbar responsive by adding the menu button to it. I have tried making a toggler class with breakpoint -md but by doing so my links appear on the left hand side on the big screens. Also, the button is not visible. Why, what am I doing wrong?
My code:
<nav class="navbar navbar-expand-md sticky-top fixed-top bg-white navbar-toggelable-md">
<div class="container">
<strong><a class="navbar-brand" href="#">Bhawna</a></strong>
<button class="navbar-toggler navbar-toggler-right" type="button"
data-toggle="collapse" data-target="#navbarContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse navbar-right" id="navbarContent">
<ul class=" navbar-nav navbar-right">
<li class="nav-item active"><a class="nav-link active"
href="index.html">Work</a> </li>
<li></li>
<li class="nav-item"><a class="nav-link" href="about.html">About
Me</a> </li>
</div>
<li> <label class="switch">
<input type="checkbox" checked data-toggle="toggle"
onclick="location.href='darkindex.html'">
<span class="slider round"></span>
</label>
</li>
</ul>
</div>
</nav>
You had the following block outside the <ul class=" navbar-nav navbar-right">... </ul>
<li> <label class="switch">
<input type="checkbox" checked data-toggle="toggle" onclick="location.href='darkindex.html'">
<span class="slider round"></span>
</label>
</li>
Rest, you can style it your way... I have applied a border to the toggler button to show where it is
working snippet below
.navbar-toggler-icon {
border: 1px solid red;
color: blue;
}
span {
background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000…p='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<nav class="navbar navbar-expand-md sticky-top fixed-top bg-light ">
<div class="container">
<strong><a class="navbar-brand" href="#">Bhawna</a></strong>
<button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#navbarContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse navbar-right" id="navbarContent">
<ul class=" navbar-nav navbar-right">
<li class="nav-item active"><a class="nav-link active" href="index.html">Work</a> </li>
<li></li>
<li class="nav-item"><a class="nav-link" href="about.html">About
Me</a> </li>
<li> <label class="switch">
<input type="checkbox" checked data-toggle="toggle" onclick="location.href='darkindex.html'">
<span class="slider round"></span>
</label>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h3>Collapsible Navbar</h3>
<p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).</p>
<p>Only when the button is clicked, the navigation bar will be displayed.</p>
<p>Tip: You can also remove the .navbar-expand-md class to ALWAYS hide navbar links and display the toggler button.
</p>
</div>

images sometimes default to 0 width

I am building a site for a client using bootstrap and for some reason the images I use in my nav-icons don't always appear.
When I first load the page, I notice that the images don't appear and have a width of 0. If you re-load the page the images show up.
Is there something going on here or something I can do to make sure they are always there and don't have that hiccup?
Here is my code for the nav-bar:
<nav class="navbar navbar-collapse text-center navbar-expand-lg mt-4 navbar-light">
<button class="navbar-toggler nav-btn" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon float-right"></span>
</button>
<div class="collapse navbar-collapse px-0" id="navbarSupportedContent">
<ul class="navbar-nav algin-items-center d-flex ml-5 pl-5">
<li class="nav-item col-2 text-center expand-lg">Home</li>
<img src="images/shapeimage_1.png" width="26" height="26" class="nav-ico after my-auto" />
<li class="nav-item col-2 text-center" id="about">About Us</li>
<img src="images/dogpaw_1.png" width="26" height="26" class="nav-ico after my-auto" />
<li class="nav-item col-2 text-center" id="services">Services</li>
<img src="images/icon-bowl_46.png" width="26" height="26" class="nav-ico after my-auto" />
<li class="nav-item col-2 text-center id="adopt"">Adopt</li>
<img src="images/heart2_3.png" width="26" height="26" class="nav-ico after my-auto" />
<li class="nav-item col-2 text-center" id="donate">Donate</li>
<img src="images/icon-house_46_1.png" width="26" height="26" class="nav-ico after my-auto" />
<li class="nav-item col-2 text-center" id="events">Events</li>
</ul>
</div>
</nav>
The images between the li are what sometimes change width.
I typically open in chrome and the developer tools show that the image is there and unbroken, but has a width of 0.
Reload the page and they are there without any problems and generally remain there for each re-load unless I Ctrl + F5 and force a full load from the server.
Ok, after looking at the comments and doing some trial and error, it seems simply adding <li> to the img tag is enough to make them consistently stay put. They still collapse at my breakpoint and work as I want them to without any obvious side effects. Thank you #Bogdan Lapchenkov for the idea and the suggestion. I'd up your answer if it were an answer mate.