how to affix a boxed navbar - html

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/

Related

Bootstrap responsive navbar

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;
}
}

Header content disappearing when CSS applied for responsive layout (bootstrap file included in html)

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;
}

Bootstrap custom Nav, Adding max-height breaks collapse (removes background)

I'm using BS3 and a bunch of custom styling. I know the line of code that's causing the issue, but I can't think of a way to make the navbar not so tall without the height or max-height attribute. Click on the JSFiddle and you'll see that there is no color background when you look at the menu when collapsed (mobile). Simply remove the max-height line and it works, but the navbar is too tall for what I want.
There's a ton of HTML & CSS, so beware the following paste. Note that I'm linking to a custom bootstrap.css as well...
<div class="navbar-wrapper">
<div class="container-fluid">
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <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="#"><img id="logo" src="http://www.thinkliz.com/dev/midtownaustin/img/midtown-church-austin-logo-white-vector.svg" class="img-responsive"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</nav>
</div>
</div>
CSS:
/* CUSTOMIZE THE NAVBAR
-------------------------------------------------- */
.navbar-wrapper {
position: absolute;
top: 20px;
right: 0;
left: 0;
z-index: 20;
}
.logo {
margin-top: -42px;
}
.navbar-brand img {
height: 290px;
max-height: 130px;
margin-top: -55px;
}
/* RESPONSIVE CSS
-------------------------------------------------- */
#media (max-width: 768px) {
.navbar-brand img {
max-height: 70px;
margin-top: -60px;
}
/* THIS IS WHAT IS BREAKING THE NAV */
.navbar {
min-height: 80px;
max-height: 80px;
}
.navbar-toggle {
margin-top: 23px;
}
}
So are you trying to get the background of the mobile collapsed menu a different color so you can read the text? You could add a custom CSS rule such as:
.navbar-collapse ul {
background-color: #ff0000;
}
I doubt you want red at the background, but it's just an example.
Most often it breaks the layout if you use position absolute without knowing what you really do. Especially when you work with a CSS framework. You have to fix too much and there seems to be no end.
Customize as much as possible with bootstrap and as less as possible by your own. Only that which you can't do with bootstrap.
To make the navbar work with a bigger (custom) logo follow this instruction:
Instruction bootstrap navbar with bigger logo
with your logo and the size of it.
After you have done this, set the background colors for the
.navbar-header and .navbar-collapse. You have a perfect working navbar!

how to make nav brand centrally aligned

I have the following code, what I'm trying to do is get the nav logo centered with equal pixels either side so the link on the left and right have the same distance between them.
<nav class="navbar navbar-default 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-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<a class="navbar-brand" href='#Url.Action("Index", "Home")'></a>
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav navbar-left">
<li>#Html.ActionLink("PRODUCTS", "Products", "Products")</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>VISIT US: <b>f</b></li>
</ul>
</div>
</div>
</nav>
and this is my CSS
.navbar-brand {
background-image: url("/Content/Images/Brand-Background-New5.png");
background-repeat: no-repeat;
display: block;
height: 94px;
left: 42%;
margin: auto;
position: absolute;
text-align: center;
top: 16%;
width: 301px;
z-index: 9999;
}
now on large screens i.e dessktops the above works fine, but when I minimize the browser, or someone opens the website on say a 15 inch laptop the navbar-brand is no longer centered it appears more to the right? how can I lock it so it stays centered regardless of the screen size
You can use the CSS calc() function: css calc tutorial
What's happening is that on a small screen, 42% is the space to the left of the nav, the nav itself is taking up like 50% of the screen (since its so small) and you're left with 2% to the right.
What's the width of your nav? width: 301px;
What's the total width of a computer screen? 100%
So the space to the left of a perfectly centered nav should be: (100%-301px)/2
Solving the equation we get: 50%-150px
Just fix that one line in your CSS code to say:
left: calc(50% - 150px);
THE SPACES AROUND THE - (minus) SIGN ARE IMPORTANT
The resulting CSS should be:
.navbar-brand {
background-image: url("/Content/Images/Brand-Background-New5.png");
background-repeat: no-repeat;
display: block;
height: 94px;
left: calc(50% - 150px);
margin: auto;
position: absolute;
text-align: center;
top: 16%;
width: 301px;
z-index: 9999; }

Collapsed bootstrap menu bar not working

When my web page is shown on small devices the menu collapses as expected but when you click on the collapsed menu icon the menu does not appear. There is a slight adjustment of the vertical height of the page below the menu and the last menu item half appears along the top of the page but the rest of the menu is nowhere to be seen.
My Bootstrap code looks like this:
#myNavbar {
position: relative;
border:none;
}
#myNavbar .nav {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: -10px;
}
.navbar-nav.navbar-right:last-child {
margin-right: 0;
}
.navbar-default {
background:#FFFFFF;
border-left:none;
border-right:none;
border-top:none;
border-bottom:solid;
border-bottom-width:2px;
border-color:#01B6AC;
width:100% !important;
margin-right:0px;
padding-right:0px;
border-radius: 0 !important;
-moz-border-radius: 0 !important;
}
<nav class="navbar navbar-default">
<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>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img class="img-responsive" src="/assets/img/outa.png" width="120px;"/></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav pull-right">
<li>Saved searches</li>
<li>Settings</li>
<li>About</li>
<li>Sign In</li>
</ul>
</div>
</div>
</nav>
Anyone know what I am doing wrong?
You problem is here
#myNavbar .nav {
position: absolute;
}
When you use position:absolute; the element will be positioned absolutely from the first positioned parent div, if it can't find one it will position absolutely from the window. You can refer to THIS question for further info. So what you can do is add #media query and set a size over certain pixels where the position will be absolute. So something like
#media(min-width: 400px) {
#myNavbar .nav {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: -10px;
}
}
just give proper reference of bootstrap.css and bootstrap.js and your code will work fine.