Bootstrap 4 flexbox navigation with centered logo and items either side - css

I'm trying to create a simple bootstrap 4 navigation navbar with my logo centered and a fontawesome menu icon aligned to left and a button aligned to the right.
In my code I have created 3 columns (5, 2 and 5). It works fine if I remove the nav part, but within the nav, it collapses and doesn't take up the full width. I've read that it is because bootstrap 4 uses flex. I have tried adding the class w-100 to the row, which nearly works, but it then has a small padding on the right of the navigation which causes the logo to not be exactly centered.
<nav class="navbar bg-dark">
<div class="row">
<div class="col-5 text-left bg-warning">
<i class="fas fa-bars text-primary pointer" style="font-size: 28px;" ></i>
</div>
<div class="col-2 bg-success text-center">
Logo
</div>
<div class="col-5 text-right bg-info">
<a class="btn btn-primary" href="#" role="button">Sign Up</a>
</div>
</div>
Does anyone know how to get this to work using flex? I want to be able to possibly add another button or icon in the right column as well, but still have the logo exactly in the center of the screen.
************* UPDATE **************
I have now got it to work using flex, but I have had to add the style such as "flex: 1 0 40%;" to each of the 3 divs, 40% to the left and right div and 20% to the middle/logo div. The code doesn't look ideal but it does work.
<nav class="navbar bg-light">
<div class="d-flex w-100">
<div class="my-auto" style="flex: 1 0 40%;">
<i class="fas fa-bars text-primary pointer" style="font-size: 28px;" ></i>
</div>
<div class="text-center" style="flex: 1 0 20%;">
Logo
</div>
<div class="my-auto text-right" style="flex: 1 0 40%;">
<a class="btn btn-sm btn-watermelon" href="#" role="button">Sign Up</a>
</div>
</div>
</nav>

The .navbar class adds padding: .5rem 1rem; style rule to the nav element. The "Logo" text is not centered, because of the .navbar-brand class, that adds margin-right: 1rem; Adding content to a column does not change the size of it.
Add these classes to the elements:
<nav class="navbar bg-dark pr-0 pl-0">
...
Logo
...
</nav>
The class pl-0 and pr-0 will set the left and right paddings to 0, while m-0 will remove all margins.
Here is a very nice cheat-sheat for bootstrap 4

THis is it:
<div class="container">
<nav class="navbar bg-dark">
<a href="" data-toggle="modal" class="navbar-toggler" data-
target="#exampleModal"><i
class="fas fa-bars text-primary pointer btn btn-warning" style="font-
size: 28px;"></i></a>
Logo
<a class="btn btn-info" href="#" role="button">Sign Up</a>
</div>
check:https://jsfiddle.net/sugandhnikhil/gn89rj5p/

Related

CSS to fit content height but not go off screen

I would like to define a div that has the following characteristics:
It expands vertically to fit content.
It does not expand off screen but rather shows a scrollbar.
I'm using this "List Group" example here as a base:
https://getbootstrap.com/docs/5.2/examples/sidebars/
However, when I add something above the scrolling list, the list doesn't shrink down vertically to accommodate the header, it just pushes the scroll list down and now I have two scroll bars: one for the screen (since the whole list got pushed down) and another scroll bar for the list box (since its still the same size and has to scroll its internal elements).
<div class="d-flex flex-column">
<div>
<header class="mb-1 py-2 w-100">
<div class="align-items-center container-fluid d-grid gap-3" style="grid-template-columns: 1fr 2fr;">
<div class="m-0 p-0">
<div class="align-items-center d-flex mb-3 mb-md-0 me-md-auto text-dark text-decoration-none" href="/">
<span class="fs-4">Task Administration</span>
</div>
</div>
<div class="m-0 p-0">
<button class="btn btn-primary float-end">Save all changes</button>
<button class="btn btn-danger float-end me-4">Discard all changes</button>
</div>
</div>
</header>
<div class="bh-divider"></div>
</div>
<main class="d-flex flex-nowrap" >
<div class="d-flex flex-row" >
<div class="align-items-stretch bg-white d-flex flex-column flex-shrink-0" style="width: 380px;">
<a class="align-items-center border-bottom d-flex flex-shrink-0 link-dark p-3 text-decoration-none" href="/">
<span class="fs-5 fw-semibold">Categories</span>
<button class="btn" style="opacity: 0" type="button">Hidden for alignment</button>
</a>
<div class="border-bottom list-group list-group-flush scrollarea" id="contentArea">
<data-items-list></data-items-list>
</div>
</div>
</div>
</main>
</div>
If I don't fix the height on id="contentArea", to something like height:800px, then the list always bumps off the screen and creates 2 scrollbars.
I'd rather not fix the height since different people have different sized screens.
I've been racking my brains trying all sorts of css shenanigans, but nothing seems to work and writing my own javascript to do this seems like a bad idea.
Thanks all for your time and attention.

expand height of jumbotron to cover whole column height

Using bootstrap 4.0.0, I have the following code:
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="jumbotron mt-3 container-fluid" style="padding: 0.6em 1.6em;">
<h5>Title</h5>
<p style="font-size: 14px;">text text text</p>
<button type="button" class="btn btn-primary"> Click me </button>
</div>
</div>
... repeated many times
</div>
</div>
I am trying to create a grid of 3 by x jumbotrons. I am not able to expand the height of the jumbotrons as to cover 100% of the given row in which they are. Each one is with their own height. The ideal is that the <h5> and the <p> are top-aligned, while the <button> is bottom-aligned.
How can I achieve the above? Thanks
Use the flexbox utils for the jumbrotrons.
<div class="col-md-4">
<div class="jumbotron mt-3 h-100 d-flex flex-column align-items-start" style="padding: 0.6em 1.6em;">
<h5>Title</h5>
<p style="font-size: 14px;">text texttext</p>
<button type="button" class="btn btn-primary mt-auto"> Click me </button>
</div>
</div>
https://www.codeply.com/go/4uafEjOFdn
h-100 - make jumbotron fill height of col
d-flex flex-column - display:flex the jumbotron the content vertically
align-items-start - align content down the left side
mt-auto - align bottom on the bottom
Related: Bootstrap 4 cards - align content at the bottom

Vertical center a span with font-awesome 5 SVG icon in Bootstrap Panel Heading

I have this code:
<div class="panel-heading clearfix">
<div class="col-md-11 left">
<button style="display: inline-block;" type="button" class="btn btn-success btn-circle"> <i class="fab fa-twitter"></i></button>
<h3 style="display: inline-block;" class="panel-title uppercased green"> Twitter</h3>
</div>
<div class="col-md-1">
<span class="clickable right"><i class="fa fa-2x fa-chevron-circle-up"></i></span>
</div>
</div>
I have a problem to center vertically col-md-1 and span with font-awesome chevron icon.
This is an image
This is beacuse the button inside the left div has a certain height that affects also the element on the right.
Just add a line-height equals to the height of the green button:
.clickable i {
line-height: 34px;
}
I also fixed your typo in icon class. There's an extra 'b' that causes the non-rendering of the twitter icon.
<i class="fa fa-twitter"></i>
I have updated the codepen.
I add a wrapper and background color, so you can easily see that now it's aligned correctly.

Bootstrap 4 center header elements on mobile

Here is my header setup (bootstrap 4):
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="navbar-brand"><img src="..."></div>
</div>
<div class="col-md-6 text-right">
<div class="header-btn-grp">
<div class="header-call-us">Get a Quote, Call Today!</div>
<a role="button" class="btn btn-danger btn-lg header-btn" href="tel:123">Ph : <strong>...</strong></a>
<div class="header-address">XXX</div>
</div>
</div>
</div>
As expected on desktop the logo sits on the left and the button sits on the right.
When on smaller devices I would like the logo and button to align in the center.
I have tried to add a .text-md-center class to both columns but this caused both elements to center in there columns at all widths (desktop and mobile).
What is the correct way to do this?
An alternate to #cwanjt answer is using the text-center. You just then need to use text-md-left and text-md-right to keep the desired alignment on larger widths.
<div class="container">
<div class="row">
<div class="col-md-6 text-md-left text-center">
<div class="navbar-brand"><img src="//placehold.it/140x30"></div>
</div>
<div class="col-md-6 text-md-right text-center">
<div class="header-btn-grp">
<div class="header-call-us">Get a Quote, Call Today!</div>
<a role="button" class="btn btn-danger btn-lg header-btn" href="tel:123">Ph : <strong>...</strong></a>
<div class="header-address">XXX</div>
</div>
</div>
</div>
</div>
https://www.codeply.com/go/Ijl31XHfRT
#TimothyAURA, if I'm understanding you question correctly, it's how to center the content of your header on smaller screens. If that's the case, you can look at the code in this codeply project to get an idea of how to do that. It seems like you have some familiarity with Bootstrap, but here's a reference to Bootstraps utilities for justifying content.
It uses a justify-content-center class for device sized medium and below, and a justify-content-lg-between on larger displays.
<header class="d-flex justify-content-center justify-content-lg-between">
<a class="navbar-brand" href="#">
<img src="http://via.placeholder.com/65x65" alt="">
</a>
<div class="header-btn-grp">
<div class="header-call-us">Get a Quote, Call Today!</div>
<a role="button" class="btn btn-danger btn-lg header-btn" href="tel:123">Ph : <strong>...</strong></a>
<div class="header-address">XXX</div>
</div>
</header>

Place button group at the far right side of a header with Bootstrap

I've managed to place a button group at the far side of a header (H1/H2/H3). However, I don't know how to make the button float vertically in the middle. It just stays at the top. I tried using margin-top but since it's a fixed value it won't be dynamically placed in the middle.
Here's the screenshot:
Here's the code:
<div class="container">
<section>
<div class="page-header">
<h1 class="pull-left">
The header
</h1>
<div class="pull-right">
<div class="btn-group">
<button class="btn btn-primary">
Actions
</button>
<button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu pull-right">
<li>
Action
</li>
<li>
Another Action
</li>
</ul>
</div>
</div>
<div class="clearfix"></div>
</div>
Contents
</section>
</div>
jsFiddle: http://jsfiddle.net/aurorius/PKrUC/
This how to get a button lined up with the header in Bootstrap 3.2, 3.3 without any additional CSS in case anyone is coming here from Google.
Option 1
http://jsfiddle.net/ypaL5nko/
<div class='page-header'>
<div class='btn-toolbar pull-right'>
<div class='btn-group'>
<button type='button' class='btn btn-primary'>Button Text</button>
</div>
</div>
<h2>Header Text</h2>
</div>
The button group is optional if you're only using a single button.
Option 2
http://jsfiddle.net/sLdnae3q/
<h1>
<span>Header Text</span>
<button class='btn btn-primary pull-right'>Button Text</button>
</h1>
<hr/>
Try this css code
.btn-group.my-class{
bottom: 15px;
position: relative;
top: 15px;
}
Dynamic header on H1/H2/H3 and may be H4
Demo: http://jsfiddle.net/PKrUC/2/
Using a float disables te display:inline-block; property, making it float to the top.
Just add the bootstrap class text-right to the container to keep the vertical align intact.

Resources