The challenge: create a nav bar that has a brand logo on the left with the navbar-collapse menu and some other call to action/link above it on the right, and have them all vertically positioned.
Seems straight forward, until you want the mobile to look like this:
How can we achieve this with good responsive css (no negative margins/absolute positioning techniques) and no JS?
I've only come up with the option of printing the Phone number twice on the page, and hiding/showing for various screen sizes:
<header>
<div class="content-above-navbar container">
<div class="row d-md-none flex-row-reverse">
<div class="col-auto ml-3"><span class="navbar-text">800-BS4-WEBS</span></div>
<div class="col-auto p-0"><span class="navbar-text">Contact Us</span></div>
</div>
</div>
<nav class="navbar navbar-expand-md navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="Bootstrap 4 Navbar">Bootstrap 4 Navbar</a>
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#all-pages-176" aria-controls="all-pages-176" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
<div id="all-pages-176" class="navbar-collapse has-content-above collapse">
<div class="content-above-nav-menu">
<div class="float-md-right ml-md-3 d-none d-md-block"><span class="navbar-text">800-BS4-WEBS</span></div>
<div class="float-md-right d-none d-md-block"><span class="navbar-text">Contact Us</span></div>
</div>
<ul id="menu-all-pages" class="navbar-nav ml-auto">
<li id="menu-item-1700" class="nav-item active">Sample Page</li>
<li id="menu-item-1701" class="nav-item">Page Markup And Formatting</li>
<li id="menu-item-1702" class="nav-item">Page Image Alignment</li>
<li id="menu-item-1705" class="nav-item">Level 1</li>
</ul>
</div>
</div>
</nav>
</header>
There's a tiny bit of custom css to make the non-menu info stack up a row and move to the right. See the fiddle link:
Here's a Fiddle
Other markups work for one screen size, but not the other.
Is anybody seeing a solution to this that can be achieved w/ a relatively flat markup?
Echoing Raphael's comment to your question, I'm not entirely sure if the issue could be solved using Bootstrap's styles (other than with duplication of content, as you've shown).
Ignoring Bootstrap, if you only need to support IE11+ then CSS Grid can be used to achieve the layout you've described. I don't have enough experience with Bootstrap to know how well this solution would play with Bootstrap's styles, but here's a barebones example of your layout implemented with pure CSS grids (supporting IE would require adding additional styles that I left out for brevity):
.header {
margin: 1em;
padding: 1em;
display: grid;
grid-template-rows: auto auto;
grid-template-columns: 1fr auto;
grid-template-areas:
"c c"
"l t"
}
.header__logo {
grid-area: l;
}
.header__contact {
grid-area: c;
justify-self: end;
}
.header__menu {
display: none;
}
.header__toggle {
grid-area: t;
margin-left: 1em;
align-self: center;
}
#media (min-width:768px) {
.header {
grid-template-rows: auto auto;
grid-template-columns: auto 1fr;
grid-template-areas:
"l c"
"l m"
}
.header__toggle {
display: none;
}
.header__contact {
align-self: center;
}
.header__menu {
display: flex;
grid-area: m;
align-self: center;
justify-self: end;
}
}
/* debug styles */
.header {
border: 1px solid red;
}
.header > * {
outline: 1px dashed #cf0;
}
<div class="header">
<img src="http://via.placeholder.com/200x80&text=Brand+Logo"/>
<div class="header__contact">
Contact Us 800-BS4-WEBS
</div>
<button class="header__toggle">=</button>
<div class="header__menu">
Sample Page
Another Page
</div>
</div>
It can be done with the Bootstrap 4 grid and utility classes...
Demo: https://www.codeply.com/go/CBsBPCE4oV
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<div class="row w-100 no-gutters d-lg-block d-flex flex-wrap justify-content-between">
<a class="navbar-brand col-lg-3 col-6 py-lg-3 order-lg-1 order-2 float-left d-block h-100" href="Bootstrap 4 Navbar">
Logo
</a>
<div class="navbar-text col-lg-6 col-12 order-lg-2 order-1 text-right float-right align-self-end">800-BS4-WEBS</div>
<button class="navbar-toggler collapsed float-right order-3" type="button" data-toggle="collapse" data-target="#all-pages-176" aria-controls="all-pages-176" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="all-pages-176" class="navbar-collapse collapse float-right col-lg-8 order-4">
<ul id="menu-all-pages" class="navbar-nav ml-auto justify-content-lg-end w-100">
<li id="menu-item-1700" class="nav-item active">Sample Page</li>
<li id="menu-item-1701" class="nav-item">Page Markup And Formatting</li>
<li id="menu-item-1702" class="nav-item">Page Image Alignment</li>
<li id="menu-item-1705" class="nav-item">Level 1</li>
</ul>
</div>
</div>
</div>
</nav>
Related
This question already has answers here:
Mysterious whitespace in between Bootstrap2 Navbar and row underneath
(2 answers)
Closed 5 years ago.
I am creating a website with bootstrap, and I encountered a problem when I created a featured image with a parallax effect. It seems that something is creating a space between the navigation bar (Bootstrap) and the featured image.
.parallax {
background-image: url("../images/aboutHeader.jpg");
height: 100%;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.parallax h2 {
color: white;
font-size: 4em;
text-align: center;
padding-top: 100px;
}
<!-- NAVBAR
================================================== -->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
<img src="images/headerWhite.png" alt="Bird's Aerial Imaging LC" style="height:100%">
</a>
</div>
<div class="collapse navbar-collapse navbar-right" id="myNavbar">
<ul class="nav navbar-nav">
<li>Home</li>
<li class="active">About</li>
<li>Gallery</li>
<li>Services</li>
<li>Contact</li>
</ul>
</div>
</div>
</nav>
<!-- FEATURED IMAGE =============================================================== -->
<section style="height:250px;">
<div class="parallax">
<h2>About Us</h2>
</div>
</section>
What I understood from you that you mean the white vertical space between the parallax div and the navbar and my answer will depend on that, if your problem is different please let me know.
Your solution is all about removing:
1) The margin property from the "h2":
.parallax h2 {
margin-top: 0;
}
2) The padding property from the "navbar":
.navbar {
padding-bottom: 0;
}
Hope it works with you!
You can add position:absolute; top:0; to your section tag. That will definitely remove the space...
It could also be because of default margin or padding around the section. Try to add margin:0; padding:0; to that
Using bootstrap 3, I have:
<div class="container"> </div> {with specific width}
<div class="container-fluid">
<nav> <nav>
</div> {need container-fluid to make mnu background full width}
I would like to center first menu item with left edge of picture above and also last menu item to center with right edge of text above.
Here is a link to the JSFiddle demo.
Do maximize JS fiddle result window to see menu in full width
The idea is that space between all menu items is same. I try to get result with some padding of <a> tag and <li> tag, but for bigger screen I must then write queries. Hence, how do I have an "universal" solution?
How about using flexbox to add the property value space-between and justify your unordered list items, combined with a media query to prevent affecting your hamburger menu.
#media (min-width: 768px) {
.navbar-nav {
display: flex;
width: 100%;
justify-content: space-between;
align-items: center;
}
.navbar-nav > li {
display: flex;
}
.navbar-nav > li > a {
justify-content: center;
align-items: center;
}
}
jsfiddle
CODE SNIPPET:
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
.container {
width: 1100px;
padding: 0px;
}
.navbar-nav {
padding: 0;
margin: 0;
}
.menuCon {
background: grey;
}
#mainMenu > ul > li > a {
color: #000;
}
#media (min-width: 768px) {
.navbar-nav {
display: flex;
width: 100%;
justify-content: space-between;
align-items: center;
}
.navbar-nav > li {
display: flex;
}
.navbar-nav > li > a {
justify-content: center;
align-items: center;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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" />
<div class="container ">
<div class="row headRow">
<div class="col-xs-12 col-sm-12 col-md-4 borderRight width80">
<img src="https://placeholdit.imgix.net/~text?txtsize=20&txt=230%C3%9781&w=230&h=81" class="img-responsive logo" id="logo" alt="logo" />
</div>
<div class="hidden-xs col-sm-6 col-md-4 mobileMarginText ">
<div class="headerTextOne">
<div>top textttttt</div>text text</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 text-left ">
<p class=" headerPhone">12345678999 710</p>
<p class=" headerMail">hi#hello.com</p>
</div>
</div>
<!-- /row menu-->
</div>
<div class="container-fluid menuCont ">
<div class="row">
<div class="col-xs-12 col-md-12">
<nav class="navbar navbar-default noBorder height40 menuBox">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed " data-toggle="collapse" data-target="#mainMenu" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="mainMenu">
<ul class="nav navbar-nav">
<li>First
</li>
<li>Home1
</li>
<li>Home2
</li>
<li>Home3
</li>
<li>Home4
</li>
<li>Last
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
</div>
</div>
</div>
I am building a site where I use the Bootstrap Sticky Footer and Navbar. However, with Google's recent release of the Material Design Lite library, I want to use the cards for a portion of my site. However, as soon as I include the MDL library (just the CSS, no other changes), my sticky footer does something odd. The footer is set to the bottom of the viewport (no matter the height of that window) but then it just sticks there. If I scroll up, it scrolls up as well.
When I actually inspect the footer, body, etc., I see nothing that is affecting the layout from the material design CSS. Something obviously is, but I'm fairly stuck on what it might be. (To see it work, remove the reference to the material-lite.css. Currently it is broken within the snippet.)
html {
position: relative;
min-height: 100%;
}
body {
margin-bottom: 60px;
padding-bottom: 20px;
}
footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px;
color: white;
background-color: black;
}
#footer-content {
margin: 20px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.0/material.min.css" />
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a asp-controller="Home" asp-action="Index" class="navbar-brand">
<i class="fa fa-globe fa-lg visible-sm visible-xs"></i>
<i class="fa fa-globe fa-3x hidden-sm hidden-xs"></i>
</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav main-links">
<li><a asp-controller="Home" asp-action="Index">Home</a>
</li>
<li><a asp-controller="Home" asp-action="About">About</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<img src="http://placekitten.com/300/450" />
<br/>
<img src="http://placekitten.com/300/450" />
<br/>
</div>
<footer>
<div class="container">
<div id="footer-content" class="pull-right">
My Footer
</div>
</div>
</footer>
I found the correct solution to keeping footer stuck to bottom even if the content of a new tab is a different height.
https://github.com/google/material-design-lite/issues/913
This js fiddle specifically had useful html and css.
http://jsfiddle.net/eggbox/gk7u32b6/
In short add this to your html above the footer
<style>
.mdl-layout__content {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-flow: column nowrap;
-ms-flex-flow: column nowrap;
flex-flow: column nowrap;
}
.mdl-layout__content > *:not(.mdl-layout-spacer) {
-webkit-flex: none;
-ms-flex: none;
flex: none;
}
</style>
<div class="mdl-layout-spacer"></div>
<footer class="mdl-mini-footer">
<div class="mdl-mini-footer--middle-section">
<div class="mdl-logo">Title</div>
<ul class="mdl-mini-footer--link-list">
<li>Help</li>
<li>Privacy & Terms</li>
</ul>
</div>
</footer>
This should fix your problem. See plunker
html {
height: auto;
}
I want to show the glyphs as in example A for desktop monitors and as in example B for mobile when the menu is extended. (The code is the same in both examples, except for the css).
Example A: http://jsfiddle.net/bs773m84/4/
<div class="container">
<nav class="header-nav-wrapper container-nav-menu navbar navbar-default white-back">
<div class="container-fluid">
<div class="navbar-header">
<a alt="" rel="home" href="google.com">
<img class="logo" src="http://icons.iconarchive.com/icons/cornmanthe3rd/plex-android/48/app-drawer-icon.png"/>
</a>
<button data-target="#social-menu" data-toggle="collapse" class="navbar-toggle collapsed" type="button">
<span class="sr-only"><?php _e('Toggle navigation menu','justmakeit');?></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="col-sm-11 content">
<div class="container-fluid">
<div class="hidden-xs col-sm-6 blue-light-back"><span>Brand</span>
</div>
<div class="col-xs-12 col-sm-4 col-sm-push-4 col-md-push-4 red-dark-back">
<div id="social-menu" class="collapse navbar-collapse">
<ul class="menu nav navbar-nav navbar-default white-back">
<li class="facebook">
<a href="http://facebook.com" target="_blank">
<span class="glyphicon glyphicon-play-circle"></span><span class="social-name">facebook</span>
</a>
</li>
<li class="twitter">
<a href="http://twitter.com" target="_blank">
<span class="glyphicon glyphicon-download"></span><span class="social-name">twitter</span>
</a>
</li>
<li class="google-plus">
<a href="http://plus.google.com" target="_blank">
<span class="glyphicon glyphicon-download"></span><span class="social-name">google+</span>
</a>
</li>
<li class="pinterest">
<a href="http://pinterest.com" target="_blank">
<span class="glyphicon glyphicon-download"></span><span class="social-name">pinterest</span>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</nav>
</div>
Example A css:
#social-menu li{
display: inline-block;
padding: 0px;
}
#social-menu li a{
padding: 0px;
}
#social-menu .social-name{
display: block;
height: 1.4375em;
overflow: hidden;
text-indent: 100%;
transition: all 0.3s ease 0s;
white-space: nowrap;
width: 1.4375em;
}
Example B: http://jsfiddle.net/77m94xf4/4/
Html is the same as in Example A.
Example B css: No.
To do this you can use #media Query and set your css attribute for each size.
for Example :
#media screen and (max-width: 300px)
{
add your css for mobile
}
#media screen and (min-width: 800px)
{
add your css for desctop
}
by #media Query you can set css for each devices. eg:mobile,tablet,desktop.
Learn more about #media query
I added #media Query to revert CSS of the menu items for mobile.
#media screen and (min-width: 1px) and (max-width: 767px){
#social-menu li{
display:block
}
#social-menu .social-name{
display: inline;
height: none;
overflow: none;
text-indent: none;
white-space: none;
width: none;
}
}
I know I can push navbar items to the left and right, but how would I center them?
text-align:center;
doesn't work nor any of the other things I thought of.
You will need to modify some CSS rules for Navbar component. So add a class center to nav.navbar and the following rules:
.navbar.center .navbar-inner {
text-align: center;
}
.navbar.center .navbar-inner .nav {
display:inline-block;
float: none;
}
Working demo (Bootstrap 3.3.7)
To extend upon the selected answer, adding vertical align top will prevent the space from appearing under the nav items.
.navbar.center .navbar-inner {
text-align: center;
}
.navbar.center .navbar-inner .nav {
display:inline-block;
float: none;
vertical-align: top;
}
Demo
I had similar problem. What worked for me is adding rule 'justify-content' like this:
CSS:
.navbar-expand-lg .navbar-collapse {
display: flex!important;
justify-content: space-around;
flex-basis: auto;
}
HTML:
<nav id="hamburger" class="navbar sticky-top navbar-expand-lg navbar-light">
<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="navbar-nav center">
<div><a class="nav-item nav-link active" href="#">
Start <span class="sr-only">(current)</span></a>
</div>
<div><a class="nav-item nav-link" href="#">Portfolio</a></div>
<div><a class="nav-item nav-link" href="#">Blog</a></div>
<div><a class="nav-item nav-link" href="#">O mnie</a></div>
<div><a class="nav-item nav-link" href="#">Kontakt</a></div>
</div>
</div>
</nav>
Use text-center or mx-auto or container-fluid or use flexbox technique, flex justify-center
You could use this hack text-center (this does not only apply for text)
Or use the exact boostrap class mx-auto
example
class="text-center" or class="mx-auto"
in context
<div class="btn-group text-center"> or <div class="btn-group mx-auto">
TRY NOT TO USE inline styles (bad practice)
<div class="btn-group" style="margin:0 auto">
References here:
text-center =>
https://getbootstrap.com/docs/4.0/utilities/text/
mx-auto => https://getbootstrap.com/docs/4.0/utilities/spacing/#horizontal-centering
You can use
text-align:center;
to a wrapper element to center its childs, but only if the childs have
display: inline;
/* or */
display: inline-block;
Another option, if you know the width of the element you want to center, is
display: block;
width: /* something */;
margin: 0 auto;