I'm making a website for a school club for my first starter front-end project and I've been using the bootstrap library for CSS. I've ran into a bit of a problem though, my navbar isn't centred horizontally and more so leans to the left. I wanted the title and icon all to the left then the list items on the other side of the container so I played around with margins but it never really worked out. Thank you for helping.
<nav class="navbar navbar-expand-lg ">
<div class="container">
<a class="navbar-title" href="home.html">Title</span>
<img class="navbar-brand" src="https://i.imgur.com/qRujsE0.jpg"></img>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-btarget="#navmenu">
<span class="navbar-toggle-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navmenu">
<ul class="navbar-nav mx-auto">
<li class="nav-item">Home</li>
<li class="nav-item">About Us</li>
<li class="nav-item"><a href="contact.html" class="nav-link">Contact Us</a</li>
<li class="nav-item">Join Us</li>
</ul>
</div>
</div>
</nav>
First, I suggest to specify a width and a height to your image. Then, you have to use the class ".ms-auto" to get the navigation aligned on the right as per Boostrap documentation. You could have also used Bootstrap flexbox with the class ".justify-content-between".
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg ">
<div class="container">
<a class="navbar-brand" href="#">
<img src="https://i.imgur.com/qRujsE0.jpg" width="30" height="30" class="d-inline-block align-top" alt="" />
Title
</a>
<button class="navbar-toggler" 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>
<div class="collapse navbar-collapse" id="navmenu">
<ul class="navbar-nav ms-auto">
<li class="nav-item">Home</li>
<li class="nav-item">About Us</li>
<li class="nav-item"><a href="contact.html" class="nav-link">Contact Us</a</li>
<li class="nav-item">Join Us</li>
</ul>
</div>
</div>
</nav>
Related
I am using bootstrap 4 to make a simple theme.
My problem is, that when I view the page on mobile, and click the menu button, the menu shows up for 1 second, and then hide it self. If I click the button gain, it wont show up.
Demo site here
My menu source code from the browser (I don't use the sticky classes)
<header class="header header-sticky- header-transparent__">
<div class="container">
<div class="row">
<div class="col-12">
<nav class="navbar navbar-expand-lg">
<!-- Logo START -->
<a class="navbar-brand" title="IQ Logistics Kft." href="https://kozetgyapot-arak.hu">
<img class="img-fluid" src="https://themes.potenzaglobalsolutions.com/html/bulky/images/logo.svg" alt="IQ Logistics Kft.">
</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 m-auto_">
<li class="nav-item"><a class="nav-link" title="Kezdőlap" href="https://kozetgyapot-arak.hu/">Kezdőlap</a></li><li class="nav-item"><a class="nav-link" title="Rólunk" href="https://kozetgyapot-arak.hu/rolunk">Rólunk</a></li><li class="nav-item"><a class="nav-link" title="Szolgáltatások" href="https://kozetgyapot-arak.hu/szolgaltatasok">Szolgáltatások</a></li><li class="nav-item"><a class="nav-link" title="Elérhetőségeink" href="https://kozetgyapot-arak.hu/elerhetosegeink">Elérhetőségeink</a></li> </ul>
</div>
<div class="mr-5 mr-lg-0 d-sm-flex d-none align-items-center">
<a class="btn btn-primary" href="https://kozetgyapot-arak.hu/arajanlat-kerese">Árajánlat kérése <i class="fa fa-chevron-right ml-1"></i></a>
</div>
</nav>
</div>
</div>
</div>
</header>
Thank you, if someone can help me.
It seems to work fine with this html. Note that I did leave out the Font Awesome stylesheet in the snippet below. Also note that the popper script is bundled in the bootstrap script so you heave to remove it from your project as an extra script.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/js/bootstrap.bundle.min.js"></script>
<header class="header header-sticky- header-transparent__">
<div class="container">
<div class="row">
<div class="col-12">
<nav class="navbar navbar-expand-lg navbar-light">
<a class="navbar-brand" title="IQ Logistics Kft." href="https://kozetgyapot-arak.hu">
<img class="img-fluid" src="https://themes.potenzaglobalsolutions.com/html/bulky/images/logo.svg" alt="IQ Logistics Kft.">
</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 m-auto">
<li class="nav-item"><a class="nav-link" title="Kezdőlap" href="https://kozetgyapot-arak.hu/">Kezdőlap</a></li>
<li class="nav-item"><a class="nav-link" title="Rólunk" href="https://kozetgyapot-arak.hu/rolunk">Rólunk</a></li>
<li class="nav-item"><a class="nav-link" title="Szolgáltatások" href="https://kozetgyapot-arak.hu/szolgaltatasok">Szolgáltatások</a></li>
<li class="nav-item"><a class="nav-link" title="Elérhetőségeink" href="https://kozetgyapot-arak.hu/elerhetosegeink">Elérhetőségeink</a></li>
</ul>
</div>
<div class="mr-5 mr-lg-0 d-sm-flex d-none align-items-center">
<a class="btn btn-primary" href="https://kozetgyapot-arak.hu/arajanlat-kerese">Árajánlat kérése <i class="fa fa-chevron-right ml-1"></i></a>
</div>
</nav>
</div>
</div>
</div>
</header>
I am new to Angular/Bootstrap and currently trying to put an image on top of the navigation bar in the top left corner. Using padding/margin could fix the problem with a normal resolution/static screen size, changing the size put the image elsewhere.
I have tried to use padding and margin, but as I said, it was not possible, to make it "sticky" in the top left corner, when changing the browser sice.
Also using the bootstrap classes, for example "pull-right" (just for test-cases) did not work.
My current code looks like:
<header>
<div class="cloud">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark $green static-top">
<div class="container">
<a class="navbar-brand" href="#">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf" class="pull-right" alt="" width="431.27" height="55.35">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive"
aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<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="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Impressum</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
<div class="first">
<h1>TestSite</h1>
</div>
</header>
I expected an image in the top left corner that is responsively staying there while changing browsersizes/devices
Please check this code to see, whether this is the desired result. Fully working demo is available here
Main Changes made on your code is.
added CSS class bg-dark and removed $green static-top on nav. <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
fixed the width of brand image to 50%, using css. This can be changed to some other value.
removed width and height attributes from img tag.
.navbar-brand img{
width: 50%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="#">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive"
aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<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="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Impressum</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
aight hello there, so im using bootstrap 4 and trying to create a navbar menu
here is my code
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-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 mr-auto">
<li class="nav-item"><a class="nav-link" href="#">Home</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>
</ul>
</div>
<ul class="navbar-nav"><li class="nav-item">Login</li></ul>
</nav>
So what im trying here is I want a navbar menu and some items but I want some items not be collapsed and at the same time to be in the right.
I tried moving the item I want to be in the right moved to after toggler button and it's working exactly as I want, but in small devices and in desktop device it shows back to the left.
I tried moving the item I want to be in the right after the div collapse element but it collapses in small devices.
The problem was solved by separating the navbars into two separate divs, using bootstrap4 row and col, and giving a padding of 0 to the two separate divs. I used an in-line style to position the Login item but this is just for illustration purposes. It would be better to convert the in-line style into a class.
<div class="container-fluid">
<div class="row">
<div class="col-8 p-0">
<nav class="navbar navbar-expand-lg navbar-light bg-light" style="height:4rem;">
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-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 mr-auto">
<li class="nav-item"><a class="nav-link" href="#">Home</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>
</ul>
</div>
</nav>
</div>
<div class="col-4 p-0 ">
<nav class="navbar-nav navbar-light bg-light" style="height:4rem;">
<ul class=" list-unstyled mr-4" style="margin-top:1rem;"> <li class=" float-right">Login</li>
</ul>
</nav>
</div>
</div>
</div>
I wrote a code of a toggle button to be seen when the screen size of the
browser is reduced on the navbar using bootstrap4 but the button is not
showing please what is wrong with my code:
<nav class="navbar navbar-expand-lg">
<button class="navbar-toggler " data-toggle="collapse" data-
target="#navbarcollapse">
<span class="navbar-toggler-icon" ></span>
</button>
<div class="collapse navbar-collapse" id="navbarcollapse">
<ul class ="navbar-nav mr-auto">
<li class="nav-item active">Home</li>
<li>Services</li>
<li>Downloads</li>
<li>Projects</li>
<li>About Us</li>
<li>Contact</li>
</ul>
</div>
</nav>
Just added bg-light navbar-light and the button was visible.
<nav class="navbar navbar-expand-lg bg-light navbar-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarcollapse">
<span class="navbar-toggler-icon" ></span>
</button>
<div class="collapse navbar-collapse" id="navbarcollapse">
<ul class ="navbar-nav">
<li class="nav-item">Home</li>
<li class="nav-item">Services</li>
<li class="nav-item">Downloads</li>
<li class="nav-item">Projects</li>
<li class="nav-item">About Us</li>
<li class="nav-item">Contact</li>
</ul>
</div>
</nav>
I'm using the bootstrap navigation bar, but I want to float one of the buttons to the right instead of the left as it already is. Here's the HTML:
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
I've tried using nav navbar-nav navbar-right, float:right, and pull-right. I really don't know what else I could do. Any help is appreciated.
JSFiddle
In bootstrap 4 use:
<ul class="nav navbar-nav ml-auto">
This will push the navbar to the right. Use mr-auto to push it to the left, this is the default behaviour.
You would need to use the following markup. If you want to float any menu items to the right, create a separate <ul class="nav navbar-nav"> with navbar-right class to it.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>About</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Contact</li>
</ul>
</div>
</div>
</div>
</body>
</html>
Create a separate ul.nav for just that list item and float that ul right.
jsFiddle
Bootstrap 5 (update 2021)
In Bootstrap 5 (see this question), ml-auto has been replaced with ms-auto to represent start instead of left. Since the Navbar is still based on flexbox, auto margins OR flexbox utility classes are still used to align Navbar content.
<nav class="navbar navbar-expand-lg navbar-light bg-primary">
<div class="container-fluid">
<img src="images/logo.png" alt="" width="150"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav ms-auto">
<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="#">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">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</div>
</nav>