Animated 'X' icon for bootstrap toggle - html

I am attempting to animate the horizontal lines in the navbar to an 'X' when the page is reduced in size.
My navbar code is as follows:
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-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>
<a class="navbar-brand" href="index.html">Company</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
About<b class="caret"></b>
<ul class="dropdown-menu">
<li>
About Us
</li>
<li>
Certification
</li>
<li>
Download PDF
</li>
</ul>
</li>
<li>
Products
</li>
<li>
Inquiry
</li>
<li>
Events
</li>
<li>
Contact
</li>
<li>
Login
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
When I attempt to use the CSS shown here it does not work. Is there something I need to modify for my navbar specifically? I also notice that when I add the css, the :
&:hover {
background: transparent !important;
}
the closing curly brace does not recognize the opening one. What am i missing?

It's because the tutorial is using Less, a CSS pre-processor, extending the CSS language.
However, you just have to use the compiled CSS, and make a few changes.
In your html, add some classes to the bars in order to distinguish each of them :
<span class="icon-bar top-bar"></span>
<span class="icon-bar middle-bar"></span>
<span class="icon-bar bottom-bar"></span>
Also, init your button with the class collapsed, or it will first render as a 'X'.
Then add the CSS compiled :
.navbar-toggle {
border: none;
background: transparent !important;
}
.navbar-toggle:hover {
background: transparent !important;
}
.navbar-toggle .icon-bar {
width: 22px;
transition: all 0.2s;
}
.navbar-toggle .top-bar {
transform: rotate(45deg);
transform-origin: 10% 10%;
}
.navbar-toggle .middle-bar {
opacity: 0;
}
.navbar-toggle .bottom-bar {
transform: rotate(-45deg);
transform-origin: 10% 90%;
}
.navbar-toggle.collapsed .top-bar {
transform: rotate(0);
}
.navbar-toggle.collapsed .middle-bar {
opacity: 1;
}
.navbar-toggle.collapsed .bottom-bar {
transform: rotate(0);
}
And now it should work like a charm.
Here's a JsFiddle : Demo

This worked for me
Theory
CSS provides all the necessary animation tools. Basically what's happening is this:
The top and bottom lines must rotate to form the X
The middle line must disappear
The X will be taller an more narrow than the hamburger lines, so:
The top and middle lines must move out vertically and to the right to maintain its center
Application
/* Define the shape and color of the hamburger lines */
.navbar-toggler span {
display: block;
background-color: #4f4f4f;
height: 3px;
width: 25px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
left: 0;
opacity: 1;
transition: all 0.35s ease-out;
transform-origin: center left;
}
/* top line needs a little padding */
.navbar-toggler span:nth-child(1) {
margin-top: 0.3em;
}
/**
* Animate collapse into X.
*/
/* top line rotates 45 degrees clockwise and moves up and in a bit to close the center of the X in the center of the button */
.navbar-toggler:not(.collapsed) span:nth-child(1) {
transform: translate(15%, -33%) rotate(45deg);
}
/* center line goes transparent */
.navbar-toggler:not(.collapsed) span:nth-child(2) {
opacity: 0;
}
/* bottom line rotates 45 degrees counter clockwise, in, and down a bit to close the center of the X in the center of the button */
.navbar-toggler:not(.collapsed) span:nth-child(3) {
transform: translate(15%, 33%) rotate(-45deg) ;
}
/**
* Animate collapse open into hamburger menu
*/
/* top line moves back to initial position and rotates back to 0 degrees */
.navbar-toggler span:nth-child(1) {
transform: translate(0%, 0%) rotate(0deg) ;
}
/* middle line goes back to regular color and opacity */
.navbar-toggler span:nth-child(2) {
opacity: 1;
}
/* bottom line goes back to initial position and rotates back to 0 degrees */
.navbar-toggler span:nth-child(3) {
transform: translate(0%, 0%) rotate(0deg) ;
}
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Bootstrap Navigation -->
<nav class="navbar bg-light">
<a class="navbar-toggler collapsed border-0" type="button" data-toggle="collapse" data-target="#collapsingNavbar">
<span> </span>
<span> </span>
<span> </span>
</a>
<a class="navbar-brand" href="./">
Brand
</a>
<div class="collapse navbar-collapse" id="collapsingNavbar">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
<main class="container">
<h1>Content Here</h1>
<p>Shrink the viewport if to expose the hamburger menu.</p>
</main>
What makes it work
Specifically, since the top and bottom lines rotate by 45 degrees to form the X, their center lines take up 70% of the width, so they must move in by 15%. This can be calculated using pythagorean theorem.
As it happens, our hamburger menu is 26x21 px, or 24% wider than it is tall, but the X ends up being 20x20 square when you move the lines into place and you take into account the height of the lines (here defined as 3px).
In this particular implementation, we are defining the point of rotation of each line as being the center-left. This affects how much we move the lines up, since the lines are about 3px tall, they each add about (2.1/2)=1.05px to the height of the X, or about 33% of the height of the X.
Therefore 33% is how much they must move out vertically out so the two lines meet at the center of the X and form a 20x20px square.
Customizing
The X will always make a square, so to find out how much to move them by, you just need to know the width and height of your <span> bars and the height of the resulting hamburger icon.
Plug those numbers into this equation:
Or in code:
const line_width = 26; // px
const line_height = 3; // px
const hamburger_height = 21; // px
const x_width = x_height = 0.8 * line_width;
const line_move_y_percent = 100 * (line_width - x_width) / (2 * line_height)
const line_move_right_percent = 100 * (x_height - hamburger_height) / (2 * line_height)

I managed to adopt Adonis' excellent answer (go and upvote) to work on one of Bootstrap 5's navbar examples with some notable changes:
With Bootstrap 5's existing navbar-toggler-icon span, there only needs to be two extra spans for the icon bars, not three
The button needs to have the collapsed class in order to avoid initialising as an X instead of the menu
Made the animation a bit faster
With those changes in mind, here is the reflected markup for an animated hamburger menu animation in Bootstrap 5:
<nav class="navbar navbar-expand-lg navbar-light bg-light collapsed" aria-label="Eighth navbar example">
<div class="container">
<a class="navbar-brand me-6 me-xl-7" href="#">Brand</a>
<button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse" data-bs-target="navbarsExample07" aria-controls="navbarsExample07" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
<span></span>
<span></span>
</button>
<div class="collapse navbar-collapse" id="navbar-home">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Find My Car</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Frequently Asked Questions</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Testimonials</a>
</li>
<li class="nav-item mx-sm-1 mx-lg-2 mx-xl-3">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</div>
</nav>
And the CSS:
.navbar-toggler span {
display: block;
background-color: #000;
height: 3px;
width: 25px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
left: 0;
opacity: 1;
transition: all 0.2s ease-out;
transform-origin: center left;
}
.navbar-toggler span:nth-child(1) {
margin-top: 0.3em;
}
.navbar-toggler:not(.collapsed) span:nth-child(1) {
transform: translate(15%, -33%) rotate(45deg);
}
.navbar-toggler:not(.collapsed) span:nth-child(2) {
opacity: 0;
}
.navbar-toggler:not(.collapsed) span:nth-child(3) {
transform: translate(15%, 33%) rotate(-45deg) ;
}
.navbar-toggler span:nth-child(1) {
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggler span:nth-child(2) {
opacity: 1;
}
.navbar-toggler span:nth-child(3) {
transform: translate(0%, 0%) rotate(0deg) ;
}

.navbar-toggle .icon-bar {
width: 26px;
height: 2px;
transition: all 0.2s;
}
This should do it, if not just try to adjust the width value in the range of 20px-26px.

Based on the answers above, my adaptation for Bootstrap 5.1. Working with booth
collapse & offcanvas. I used jQuery, but is not required.
Button:
<button class="navbar-toggler" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasNavbar" aria-controls="offcanvasNavbar">
<span></span><span></span><span></span>
</button>
JavaScript:
let $btn = $('.navbar-toggler');
$($btn.data('bs-target')).on('show.bs.collapse hide.bs.collapse show.bs.offcanvas hide.bs.offcanvas', function(){
$btn.toggleClass('opened');
});
Css:
.navbar-toggler {
border: 0;
box-shadow: none;
}
.navbar-toggler:focus {
box-shadow: none;
}
.navbar-toggler span {
display: block;
background-color: #4f4f4f;
height: 3px;
width: 25px;
margin-top: 5px;
margin-bottom: 5px;
position: relative;
left: 0;
opacity: 1;
transition: all 0.2s ease-out;
transform-origin: left center;
}
.navbar-toggler span:nth-child(1) {
margin-top: 0.3em;
}
.navbar-toggler:not(.opened) span:nth-child(1){
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggler:not(.opened) span:nth-child(2) {
opacity: 1;
}
.navbar-toggler:not(.opened) span:nth-child(3) {
transform: translate(0%, 0%) rotate(0deg) ;
}
.navbar-toggler span:nth-child(1) {
transform: translate(15%, -33%) rotate(45deg);
}
.navbar-toggler span:nth-child(2) {
opacity: 0;
}
.navbar-toggler span:nth-child(3) {
transform: translate(15%, 33%) rotate(-45deg) ;
}

Related

one icon in front of another, aligned to right of parent div

I can currently place 2 icons in front of one another, however with position: fixed, I cannot seem to get them justified to the right hand side of my parent div...
I have attempted using right: 0, but this seems to justify with respect to the screen rather than div itself. Is there something I'm missing?
.settings,
.clear {
position: fixed;
// positioning to right?
transition: all .2s cubic-bezier(.4, 0, .2, 1);
}
.hide {
opacity: 0;
transform: rotate(225deg);
}
<!-- Just for material icon purposes -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<div class="expandPanel" #panel [ngClass]="{expanded: isExpanded}" style="width: 300px; height: 300px; background: lightgrey;">
<div class="expandButton" (click)="togglePanel()">
<a class="material-icons clear" [ngClass]="{hide: isExpanded}">clear</a>
<a class="material-icons settings" [ngClass]="{hide: !isExpanded}">settings</a>
</div>
</div>
First position fixed property will position the element relative to view port.
So that is not what you are looking for. To align you child element relative to the parent element first give your parent position relative property and your child a position absolute property. I think this is what you want:
.settings,
.clear {
transition: all .2s cubic-bezier(.4, 0, .2, 1);
-webkit-transition: all .2s cubic-bezier(.4, 0, .2, 1);
-moz-transition: all .2s cubic-bezier(.4, 0, .2, 1);
-ms-transition: all .2s cubic-bezier(.4, 0, .2, 1);
}
.hide {
opacity: 0;
transform: rotate(225deg);
-webkit-transform: rotate(225deg);
-moz-transform: rotate(225deg);
-ms-transform: rotate(225deg);
}
.expandPanel {
position:relative;
}
.expandButton {
position:absolute;
right:0;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<div class="expandPanel" #panel [ngClass]="{expanded: isExpanded}" style="width: 300px; height: 300px; background: lightgrey;">
<div class="expandButton" (click)="togglePanel()">
<a class="material-icons clear" [ngClass]="{hide: isExpanded}">clear</a>
<a class="material-icons settings" [ngClass]="{hide: !isExpanded}">settings</a>
</div>
</div>

Change navbar color when viewport size change Bootstrap v4

I just migrate to Bootstrap V4. Im customizing the starter template: https://v4-alpha.getbootstrap.com/examples/starter-template/
I want the navbar to be transparent but change to dark when the viewport is sm and xs.
Im trying to change it using a breakpoint and adding a 'navbar-custom' class, but i doesn't change
html {
overflow: hidden;
height: 100%;
}
body {
padding-top: 5rem;
overflow: auto;
height: 100%;
background: linear-gradient(-45deg, #E8D1E2, #7CC4FF, #FFECBD, #BF9BFA);
background-size: 400% 400%;
-webkit-animation: Gradient 15s ease infinite;
-moz-animation: Gradient 15s ease infinite;
animation: Gradient 15s ease infinite;
}
#-webkit-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#-moz-keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
#keyframes Gradient {
0% {
background-position: 0% 50%
}
50% {
background-position: 100% 50%
}
100% {
background-position: 0% 50%
}
}
.navbar {
border-top: 2px solid #C66FF4;
width: 100%
}
#media (min-width: 768px) {
nav .navbar .navbar-custom {
background: rgba(0,0,0,0);
}
}
.starter-template {
padding: 3rem 1.5rem;
text-align: center;
}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
<meta name="description" content="Full-Stack Graphic Designer, Illustrator, Front-End Development">
<meta name="author" content="Liliana Orozco">
<meta name="keywords" content="Designer, Creativity, Design, CSS, HTML5, Branding, Logotype, Illustration, Web, Advertising">
<title>Randomood says: Welcome!</title>
<!-- Bootstrap 4.0 cdn -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<!--custom styles for this page -->
<link href="css/home.css" rel="stylesheet">
</head>
<body>
<!--navigation-->
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top navbar-custom">
<!--Brand in navigation menu-->
<a class="navbar-brand " href="#">Randomood</a>
<!--Brand image in navigation menu-->
<a class="navbar-brand" href="#"><img></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">Book</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</div><!-- /.container -->
<!--JS, Popper and JQuery-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="jquery-slim.min.js"><\/script>')</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
</body>
Easy with scss in your bootstrap theme overrides:
.bg-dark {
background-color: #343a40;
#include media-breakpoint-up(sm) {
background-color: transparent;
}
}
You are using Bootstrap's .bg-dark class in your <nav>. CSS declaration of this class is :
.bg-dark {
background-color: #343a40!important;
}
So, you are not allowed to override background-color property. In order to change the background-color property at particular screen break point, remove .bg-dark class using Javascript or Jquery and add your custom class which changes the background-color property of <nav>. I hope my explanation is clear to you.

How to make a hamburger menu button on mobile web?

I am trying to create a hamburger menu button on a navbar inside a mobile web. This is my first time to build mobile web. And this code only show a huge square kind button instead of a nice hamburger menu. Please help.
<nav class="navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand ui-link" onclick="window.location.href='/'"href</a>
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#popup">
<span class="text">Menu</span>
<span class="icon-bars">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</span>
</button>
</div>
</div>
</nav>
I'm not quite sure what you're trying to accomplish, but one thing you are missing in your "hamburger menu" is a color for the "bars".
So if you just add for example a background-color to your icon-bar elements it will show the hamburger menu.
.icon-bar {
background-color: black;
}
JSFiddle
You can just embed this code where you want a hamburger menu and it will automatically display the menu:
<svg>
<path fill="black" d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path>
</svg>
div {
width: 35px;
height: 5px;
background-color: black;
margin: 6px 0;
}
<p>A humburger icon:</p>
<div></div>
<div></div>
<div></div>
The problem in your code is that you used a span instead of a div, which has made all the difference, because span is an inline element and div is a block level element.
Using divs with some CSS as mentioned in this answer can help, but there are many ways through it.
You can use SVG, if you want a simple icon. It has been mentioned in this answer, but I'll still quote it:
<svg>
<path fill="black" d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path>
</svg>
You can also create an animated icon, like this:
function myFunction(x) {
x.classList.toggle("change");
}
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
/* Rotate first bar */
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
transform: rotate(-45deg) translate(-9px, 6px) ;
}
/* Fade out the second bar */
.change .bar2 {
opacity: 0;
}
/* Rotate last bar */
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px) ;
transform: rotate(45deg) translate(-8px, -8px) ;
}
<div class="container" onclick="myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
Hope I could help

animate background only

I have an image in a class called logo. The container .logo has an image background which I want to animate so it rotates 360 degrees indefinitely.
When I run the code it animates both images. I only want it to animate the css background property. Is this possible?
Here is my css so far.
a.navbar-brand {
background: url('http://i.imgur.com/3PL0u4Q.jpg');
background-repeat: no-repeat;
height: 180px;
width: 180px;
-webkit-animation: spin 16s linear infinite;
-moz-animation:spin 16s linear infinite;
animation:spin 16s linear infinite;
}
#-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
#-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
#keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
and the html
<nav class="navbar navbar-default navbar-fixed-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 src="http://i.imgur.com/3PL0u4Q.jpg" alt="logo" class="logo"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>Default</li>
<li>Static top</li>
<li class="active">Fixed top <span class="sr-only">(current)</span></li>
</ul>
</div><!--/.nav-collapse -->
</div>
<div class="navbar-attatch"></div>
</nav>
Fiddle here
I know with css transitions there is an 'all' or 'css-property', but I am not sure how to insert with my example. I have tried multiple ways and it breaks the animation completely.
You can't rotate just the background. You can choose which properties animate, but you cannot say that only the background is going to have the transform applied to it.
In your case, you can rotate the image instead of the link. https://jsfiddle.net/mendesjuan/t8auvq39/1/
a.navbar-brand {
background: url('http://i.imgur.com/3PL0u4Q.jpg');
background-repeat: no-repeat;
height: 180px;
width: 180px;
}
a.navbar-brand img {
animation:spin 16s linear infinite;
}
#-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }
#-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }
#keyframes spin { 100% { -webkit-transform: rotate(360deg); transform:rotate(360deg); } }
<a class="navbar-brand" href="#"><img src="http://i.imgur.com/3PL0u4Q.jpg" alt="logo" class="logo"></a>
For future reference
Please notice how much code I've added to my answer and compare it to how much code you have in the question. Please make sure you remove unnecessary code in your questions

Responsive navbar breaks in some tablets

I have designed the site and when I resize the browser window or use the tools Firefox provides for responsiveness, I don't see navigation bar breaking and I don't own a tablet but a few friends do and they say the navigation bar is not showing up correctly.
I don't understand what I am doing wrong!
This is the site
Edited:
The navigation bar li item should be floating right. Apple users say that when they open the site, it opens fine. But once they scroll down and back up, the li items do not line up on the right but come in between.
I think #khilley made a particularly valid point about using standard Bootstrap markup and built-in javascript components. Here's some reasons why:
Your current approach uses duplicative markup (for your menu). It's not DRY or accessible and it requires more effort to maintain.
If you don't need to write your own javascript, you save time in development and testing, and a couple of download bytes for your users.
Twitter Bootstrap is used on millions of websites, so it gets field tested by millions of people on millions of different device/OS/resolution combinations everyday around the world. When you use the standard markup and javascript component plugins, you get the benefit of knowing it's just going to work.
More to the point, you can easily reproduce all of the behaviors of your navigation, with just some styling, including using the built-in affix markup to handle the change in your navbar. Pretty sweet!
DEMO
There were only a few changes made to your markup and css to accomplish this. You can eliminate all of your custom javascript now except for the one click handler that you use for scrolling to your different sections.
Replace all of your navigation styles and markup with the following:
HTML:
<nav class="navbar nav-custom navbar-fixed-top" data-spy="affix" data-offset-top="80" role="navigation">
<div class="container-fluid">
<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="glyphicon glyphicon-th-list"></span>
</button>
<span class="rc">RC</span>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active">
Home
</li>
<li>
Design
</li>
<li>
Develop
</li>
<li>
Contact
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
CSS:
.nav-custom {
width: 100%;
height: 80px;
z-index: 999;
padding: 25px;
background-color: #f87f73;
border-bottom: 1px solid rgba(0, 0, 0, 0.18);
font-size: 150%;
color: #fff;
-webkit-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
}
.nav-custom.affix {
width: 100%;
background-color: rgba(255, 255, 255, 1);
color: #f87f73;
height: 60px;
padding: 14px;
-webkit-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
}
.nav-custom .rc {
background-color: #fff;
color: #f87f73;
padding: 5px;
border-radius: 50% 0% 50% 0%;
float: none;
}
.nav-custom.affix .rc {
background-color: #f87f73;
color: #fff;
}
.nav-custom.affix .navbar-collapse {
top: 60px;
}
.nav>li>a:hover, .nav>li>a:focus {
background-color: transparent;
}
button{
outline: none;
}
.navbar-toggle {
padding: 0;
margin: 0;
}
#media (min-width: 768px) {
.nav-custom a::before,
.nav-custom a::after {
display: inline-block;
opacity: 0;
-webkit-transition: -webkit-transform 0.3s, opacity 0.2s;
-moz-transition: -moz-transform 0.3s, opacity 0.2s;
transition: transform 0.3s, opacity 0.2s;
}
.nav-custom a::before {
margin-right: 10px;
content: '[';
-webkit-transform: translateX(20px);
-moz-transform: translateX(20px);
transform: translateX(20px);
}
.nav-custom a::after {
margin-left: 10px;
content: ']';
-webkit-transform: translateX(-20px);
-moz-transform: translateX(-20px);
transform: translateX(-20px);
}
.nav-custom a:hover::before,
.nav-custom a:hover::after,
.nav-custom a:focus::before,
.nav-custom a:focus::after {
opacity: 1;
-webkit-transform: translateX(0px);
-moz-transform: translateX(0px);
transform: translateX(0px);
}
.nav-custom a:link, .nav-custom a:visited, .nav-custom a:hover, .nav-custom a:active {
text-decoration: none;
color: #fff;
outline: none;
}
.nav-custom.affix a:link, .nav-custom.affix a:visited, .nav-custom.affix a:hover, .nav-custom.affix a:active {
text-decoration: none;
color: #f87f73;
outline: none;
}
}
#media (max-width: 767px) {
.navbar-collapse {
width: 250px;
position: fixed;
right: -250px;
top: 80px;
overflow-x: hidden;
background-color: rgba(255, 255, 255, 0.8);
border-top: none;
-webkit-box-shadow: none;
box-shadow: none;
-webkit-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
}
.navbar-collapse.in {
right: 0px;
width: 250px;
-webkit-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
}
.navbar-collapse.collapsing {
height: auto !important;
}
.navbar-nav {
margin: 0 -15px;
}
.navbar-nav>li>a {
padding: 0;
line-height: 3em;
text-align: center;
}
.navbar-collapse ul {
border-left: 1px solid rgba(0, 0, 0, .18);
}
.navbar-collapse ul li {
border-bottom: 1px solid rgba(0, 0, 0, .18);
}
.navbar-collapse ul li:hover {
background-color: #f87f73;
color: #fff;
-webkit-transition: all .35s ease;
-o-transition: all .35s ease;
transition: all .35s ease;
}
.navbar-collapse ul a:link,
.navbar-collapse ul a:visited {
color: #f87f73;
}
.navbar ul a:hover {
text-decoration: none;
color: #fff;
}
}
How it works:
The navbar-fixed-top class is used instead of custom styles.
Instead of writing your own javascript to handle changing your navbar when you scroll, you can use Bootstrap Affix. This built-in javascript plugin can use the data-offset-top attribute to set the point at which the affix class is automatically appended to the element where the data-spy attribute is set. Using the data attributes, you don't need a single line of javascript, just add the styles for how you want your element to look once the affix class is applied. Now all of your styles are in your CSS and none need to be in your javascript.
Use the built in navbar-toggle, which uses the built in Bootstrap Collapse plugin to transition to and from the mobile navigation. This eliminates the need to write/test/maintain your own javascript and eliminates the duplicative markup you have for the menu. The collapse plugin applies the in class to the element with the collapse class when the menu is expanded, so you can just use this for styling your menu.
Use media queries to change the layout of the menu based on the viewport size. The "mobile" menu is only displayed at viewports less that 767px, so you can easily target how you want the mobile menu to be styled.
Overall, the CSS is virtually identical to your CSS, I just changed the selectors to reflect the changes in the markup. Aside from a few minor tweaks, it was pretty much cut and pasted from your site.
When the page opens your nav is insisde the HTML block
<div class="row nav-custom nav-custom"> ... </div>
And on scrolling the nav is inside the block
<div class="row nav-custom nav-custom-2"> ... </div>
In your stylesheet http://rohanchhabra.in/css/custom.css you have padding:25px; and padding:14px; for .nav-custom and .nav-custom-2 which breaks the Bootstrap grid because there isn't enough width to place your col-sm-3 and col-sm-9 columns without breaking the row into two lines.
You could either remove or edit the padding for these 2 classes so that you aren't adding any extra horizontal space e.g.
.nav-custom{
padding-top:15px;
padding-bottom:15px;
}
or place the nav-custom class inside your col class, e.g.
<div class="row">
<div class="col-xs-3 col-sm-3">
<div class="nav-custom"> ... </div>
</div>
...
</div>
The key thing here is that you never want to had horizontal padding to any of the bootstrap row or column elements or you risk breaking the Bootstrap grid
Good luck!
It's possible you've hit upon a Safari web browser bug, since this issue seems to only get recreated on Macs. I can recreate the issue on OSX 10.8, running Safari 6.1. A browser refresh, or clicking on any of the nav values clears the issue and resets the nav display to the desired position. You could look into filing a bug for this potential Safari display issue.
That being said, my suggestion to resolve this issue is to refactor your HTML to better leverage the core Bootstrap navigation components. Currently you've got the navigation split in different div containers, and have it separate for wide screen and mobile. A more standard Bootstrap navigation element would look like:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Static navbar -->
<nav class="navbar navbar-custom navbar-inverse navbar-fixed-top" role="navigation">
<div class="container" id="nav-container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<a class="navbar-brand" href=""><img src="" class="Logo" alt="Rohan Chhabra Designer and Developer"></a>
<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>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>Home</li>
<li>Design</li>
<li>Develop</li>
<li>Contact</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<!-- End Static navbar -->
You'll of course need to add some amount of custom.css styling to this code example, and it assumes your logo is an image (so rework that accordingly), BUT I think that by using this more 'standard' navigation implementation of Bootstrap navigation you'll be in better shape in the long run. I've tested out Boostrap websites using this style navigation and they do not have the Safari display issue seen in your example.
I have validate your site with w3c validator.
There is some critical issues found need to be correct.
<meta http-equiv="X-UA-Compatible" content="IE=edge">
I recommend this line should come with IE conditional comments like below line.
<!-- [if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"> -->
And Anchor tag is not allowed as child of element ul
Please current those issues and check. It will definitely fix your site issues.
You may try using media queries for different window sizes..
https://developers.google.com/web/fundamentals/layouts/rwd-fundamentals/use-media-queries?hl=en.
This might be helpful for you.