I am having an issue with bootstrap navbar in terms of responsiveness.
I used data-spy="affix" on my navbar to make sure it stays fixed at the top while scrolling down and it works perfectly.
The problem however is when I resize the window to mobile dimension.
When I resize to mobile dimension the right part of my navbar will appear out of bounds. Meaning I can't see the content because my navbar is too wide for the screen. However, when I scroll down and the affix effect takes over, my navbar scales to mobile screen and the content that went out of screen will align perfectly for the mobile screen and the entire navbar will be responsive.
Here is my HTML code for navbar:
<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="0">
<div class="navbar-header">
<div class="navbar-brand">
Title
</div>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li>Page1</li>
<li>Page2</li>
<li>Blog</li>
<li>Contact</li>
<li>Shop</li>
<li><span class="glyphicon glyphicon-shopping-cart badge">5</span></li>
</ul>
</div>
</nav>
And this is my relevant CSS-code:
body {
font-size:large;
}
.glyphicon{
font-size: 95%;
}
.affix {
top:0px;
width:100%;
z-index:99999;
}
.affix + .container-fluid {
padding-top: 6.801%;
}
.navbar-header{
margin-left: 5% !important;
}
.navbar{
width:100% !important;
background-image:none;
background-color:black;
}
.navbar div a{
line-height:80px;
}
Also created a JSFiddle.
i have updated your codes in jsfiddle. can you please check now, and if you dont get that add bootstrap button to your navbar-header and i have a css code for you
#media (max-width:767px){
.navbar div a {
line-height: 20px;
}
.affix-top .collapse {
display:block !important;
}
}
Related
I'm trying to create a very simple navbar on my page. I'm having trouble aligning content on the right of the navbar on mobile devices. I'm not trying to do any kind of collapsing or anything, I really just want the mobile navbar to look just like the desktop version.
My problem is on mobile devices, my right-aligned content is appearing beneath my header like so:
Mobile
Desktop
How I have it on Desktop right now is how I pretty much want it. I'd like to vertically align the buttons in the navbar too but I can take care of that part:
Here's my code:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
Favorite List
</div>
<ul class="nav navbar-nav navbar-right">
<li><button type="button" class="btn btn-default">Default</button></li>
<li><button type="button" class="btn btn-default">Default</button></li>
<li><button type="button" class="btn btn-default">Default</button></li>
</ul>
</div>
</nav>
I am fixed this.
use this css in your responsive for mobile view:
.container-fluid>.navbar-collapse, .container-fluid>.navbar-header, .container>.navbar-collapse, .container>.navbar-header {
margin-right: -15px;
margin-left: -15px;
display: inline-block;
float: left;
}
.navbar-nav {
margin: 7.5px -15px;
display: block;
float: right;
}
.nav>li {
position: relative;
display: inline-block;
}
.navbar-default .navbar-brand {
color: #777;
font-size: 15px;
}
I do not know how I can make my navbar smaller on Ipad. As it is now, the navbar is hanging under my logo, and they should be on a row instead. I tried to set col-sm-12 because I thought everything in the div would take 12 col, but that does not work.
My website
Does anyone have an idea how I can do that?
HTML
<!-- begin nav -->
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<i class="fa fa-reorder" style="padding-left: 4px; color: red;"></i>
</button>
<!-- begin logo in navigation -->
<a class="navbar-brand" href="https://<?php echo $_SERVER['HTTP_HOST']?>/index.php">
<img src="https://<?php echo $_SERVER['HTTP_HOST']?>/images/graphic/vouzalis-webdesign.png">
</a>
<!-- end logo in navigation -->
</div>
<div class="navbar-collapse collapse">
<ul class="nav nav-pills nav-main pull-right">
<!-- begin navigation items -->
<li>
Hjem
</li>
<li>
Kompetencer
</li>
<li>
Portfolio
</li>
<li>
Om Mig
</li>
<li>
Artikler
</li>
<li>
Kontakt
</li>
<!-- end navigation items -->
</ul>
</div>
</div>
</div>
</div>
</nav>
<!-- end nav -->
CSS
#media (max-width: 767px) {
/* Navigation */
.nav-main {
float: left!important;
}
.navbar-default .navbar-collapse,
.navbar-default .navbar-form {
border: none;
}
.nav-main li:first-child {
border-top: 1px dashed #444;
}
.navbar .dropdown-menu>li>a {
padding: 15px 20px;
}
.nav-pills>li {
float: none;
}
.nav-main {
width: 100%;
}
.navbar-brand {
padding: 0px 0px 0px 15px;
}
.fixednav .nav-main {
background: #171717;
margin-top: 14px;
}
.fixednav .nav-pills>li+li {
margin-left: 0px;
}
.dropdown-menu {
float: none;
position: static;
}
You need to define your tablet breakpoint in your css.
use another media query like #media (min-width: 768px) and (max-width: 1024px) {}
define the spacing and height that you want there and it will take affect.
EDIT
I see you have a breakpoint define for min-width: 769px and max-width 991px
You can keep that breakpoint if you wish but after looking at your site the real issue is that you have your wrapping container class set to 750px and margin 0 auto to center it.
The reason your nav bar is nesting under your name element is because this container is not wide enough to display both of them inline.
You can make the container larger say 100% width and adjust your margins or make your navigation buttons smaller.
or a combination of both.
Im using affix to make fixed my navbar when scroll.
But when scroll navbar is not working fine.
I use <header class="container"> to give a boxed look to header website, an center content.
if I would use header without container, header and menu would fit screen side-to-side. In this case the affixed navbar would work fine, fitting screen in the same way (taking 1391px width).
However Im using <header class="container">. And affixed navbar runs out of screen because is taking 1391px width too. Like if it were a side-to-side looked website. It does not adapt to container width.
Do you know how to solve it?
or
is it that bootstrap affix is desing just for stretched sites and no boxed sites?
This is my HTML for nav
<header class="container">
<nav id="custom-bootstrap-menu" class="navbar navbar-default" role="navigation" data-spy="affix" data-offset-top="222">
<div class="container-fluid">
<div class="navbar-header"><a class="navbar-brand" href="#">Home</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-menubuilder"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse navbar-menubuilder">
<ul class="nav navbar-nav navbar-left">
<li class="dropdown" class="dropdown">Nosotros <b class="caret"></b>
<ul class="dropdown-menu" role="menu">
<li>Misión, Visión y Objetivos
</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
</header>
and this is css
header{
border-top-width: 8px;
border-top-style: solid;
border-top-color: #888;
}
.affix {
position: fixed;
top: 0;
width: 100%;
z-index: 1;
right: 10%;
left: 10%;
}
.affix + .container-fluid {
padding-top: 70px;
}
Take a look here it is similar to what is happening to me. Make result screen smaller and bigger to see what happens
Without codes, I can only assume how your codes look like.
Using twitter bootstrap's navbar fixed navbar example, you need to change .navbar-fixed-top right and left values. Using 0 sets it from side to side.
#media (min-width: 768px) {
.navbar-fixed-top {
position: fixed;
right: auto;
left: auto;
}
}
After that, to make them all align, you would have to overwrite bootstrap's margins (for eg .navbar-right of you have that, etc).
See the fiddle: https://jsfiddle.net/jennift/o7utddud/2/
On a larger screen, you want the #nav.affix to follow the container size. Using 0 on left and right, uses the container as a guide, "stretching" it only as wide as the container.
On a smaller screen, the container now has the width of "auto" and with a fixed position it will stretch left to right. Using a padding of 20px (as used by the bootstrap css), you can realign it to the length of the header container.
#nav.affix {
position: fixed;
top: 0;
left:0;
right:0;
}
#media (max-width: 767px) { /*controls on smaller screen*/
#nav.affix {
left:20px; /*20px to follow bootstrap body padding */
right:20px;
}
}
Fiddle: http://jsfiddle.net/jennift/hnkutxsn/3/
I am setting a responsive layout for my personal website, which has worked in a simple example. However I am not getting the same results. It may have something to do with including the Bootstrap files in my html. Below is the html and css code for the responsive layout:
HTML:
<header>
<a href="/" id="logo">
<h1>Brian Weber</h1>
<h2>Engineer | Python Programmer</h2>
</a>
<!-- Navigation bar -->
<nav class="navbar navbar-default" role="navigation">
<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="#">Brian Weber</a>
<p>Engineer</p> -->
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Portfolio</li>
<li>About</li>
<li>Resume</li>
<li>Contact</li>
</ul>
</div>
</div>
</header>
CSS:
#media screen and (min-width: 660px) {
/*********************************
HEADER
*********************************/
.navbar .navbar-default {
float: right;
background: none;
font-size: 1.125em;
margin-right: 5%;
text-align: right;
width: 45%;
}
#logo {
float: left;
margin-left: 5%;
text-align: left;
width: 45%;
}
#logo h1 {
font-size: 2.5em;
}
#logo h2 {
font-size: 1.7em;
margin-bottom: 20px;
}
header {
border-bottom: 5px solid #599a68;
margin-bottom: 60px;
}
}
When I check the code in a browser, the h1 and h2 text do not appear. However when I inspect the element there are placeholders for where the text should be. The text color is already set to black in the main.css. My question is how would I fix this problem so that the layout looks like the picture below? I am thinking maybe there is an issue with the bootstrap files where they are overriding the CSS code.
Correct format for header and navbar
h1 and h2 text disappearing
Add this in your style
#logo
{
z-index:1;
position:relative;
}
The z-index property specifies the stack order of an element.
An element with greater stack order is always in front of an element with a lower stack order. and in your style navbar-default hide the h1 and h2 that's why it's not looking
Make .navbar-default float right when in the larger views. It was covering up the left floating div that contained your name etc.
Add position:relative; and z-index for logo.
#logo {
float: left;
margin-left: 5%;
text-align: left;
width: 45%;
position: relative;
z-index: 1;
}
I have modified the navbar in bootstrap to use x-scroll instead of the hamburger/dropdown style. The problem I am having is .navbar-nav and .navbar-header are stacking on mobile and I need them to remain inline.
I have tried lots of methods but they're all extremely messy and inconsistent.
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#"></a>
</div>
<ul class="nav navbar-nav">
<li>Link</li>
...
</ul>
</div>
</nav>
CSS:
.navbar-nav {
float: none;
white-space: nowrap;
overflow-x: auto;
-webkit-overflow-scrolling: touch;
}
.nav li {
display: inline-block;
float: none;
}
Demo: http://jsfiddle.net/1u7zyehs/2/
I know the default behaviour of BS is to stack containers on mobile, is there a clean way to prevent it from happening?
In the media query targeting mobile devices, you can add the following...
Fiddle
.navbar-header{
float:left;
}