Bootstrap Transparent Navbar not Working - css

I am trying to get my bootstrap navbar to have a transparent background initially, but solid white when the user scrolls down. I have consulted many SO posts on this and tried the solutions suggested here and here, but to no effect. I am using Rails 4.2.5 and the bootstrap-sass gem for my bootstrap, imported via my application.scss file using:
#import "bootstrap-sprockets";
#import "bootstrap";
I think something is amok with my bootstrap because I can't achieve a transparent navbar by adding transparent as a class to the navigation menu, yet putting in navbar-inverse works to change the color scheme. Also, I have had to use !important in all of my css items on this stylesheet to get them to override bootstrap. Here's my navbar html.erb code:
<nav class="navbar" id="main_navbar">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/"><%= image_tag 'Flame.png', style: "height: 100%"%></a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav">
<li><%= link_to 'Workouts', workouts_path, class:"waves-effect waves-light" %></li>
<!-- <li>Generator</li> -->
<li><%= link_to 'Blog', posts_path, class:"waves-effect waves-light" %></li>
<li><%= link_to 'Weekly WOW', weeklies_path, class:"waves-effect waves-light" %></li>
<li><%= link_to 'Rants', rants_path, class:"waves-effect waves-light" %></li>
<li><%= link_to 'About Us', home_about_path, class:"waves-effect waves-light" %></li>
<% if current_user %>
<li><%= link_to('My Profile', edit_user_registration_path(current_user)) %></li>
<% else %>
<li><%= link_to('Sign In', new_user_session_path) %></li>
<% end %>
</ul>
</div>
</div>
</nav>
I know this post subject may be duplicating others', but I have read dozens of SO posts and none of them have fixed this issue. Any help would be appreciated.

To change the color to white on scroll, you'll need some javascript.
Here's a working fiddle.
$(document).ready(function(){
var scroll_start = 0;
var startchange = $('#startchange');
var offset = startchange.offset();
if (startchange.length){
$(document).scroll(function() {
scroll_start = $(this).scrollTop();
if(scroll_start > offset.top) {
$(".navbar-default").css('background-color', '#f0f0f0');
} else {
$('.navbar-default').css('background-color', 'transparent');
}
});
}
});
For the transparent navbar, you can edit some css on the navbar-default
.navbar-default {
background: transparent;
border: none !important;
}
UPDATE: https://jsfiddle.net/p7p9t0a0/3/
using affix as mentioned by #daniel

Related

Ruby on rails App not recognizing stylesheet in assets

I'm trying to add a simple border to some buttons and I can't seem to get it to display.
I have a button module in app/assets/stylesheets/modules/_buttons.scss that I've imported into the application.scss
#import "bootstrap-sprockets";
#import "bootstrap";
//modules
#import "modules/buttons";
button css:
.button {
border: 1x solid;
padding: 1em 1.5em;
}
And then I added the button class to my header file:
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="#">OpEd</a>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Share your opinion", new_post_path, class: "button" %></li>
<% if user_signed_in? %>
<li><%= current_user.first_name %></li>
<li><%=link_to "Sign out", destroy_user_session_path, method: :delete %></li>
<%else%>
<li><%= link_to "Sign in / Sign up", new_user_session_path, class: "button" %> </li>
<%end%>
</ul>
</div>
</nav>
Why is the border not being displayed?
perhaps try and designate a color in your css file?
Replacing...
border: 1x solid; with
border: 1px solid #e8e8e8;
Let me know of your result!

Adding a specific stylesheet to a single Rails page

I've got a Rails app that has a couple of 'static' pages and I plan to have more in the future.
Current file structure:
....
/assets/stylesheets/static.scss
....
/controllers/static_controller.rb
....
/views/static/home.html.erb
/views/static/feedback.html.erb
....
My Gemfile contains the 'bootstrap-sass' gem.
I want the home.html.erb view to have a different style from all the other 'static' pages, and specifically the following css:
html {
height: 100%;
background: url(assets/image.jpg) no-repeat center center;
background-size: cover;
font-family: Arial;
}
h1 {
color: black;
font-style: bold;
text-align: center;
}
How can/should this be achieved most efficiently? Would I need to create another controller/view/stylesheet for the home.html.erb?
CODE:
application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Batuk</title>
<%= Gon::Base.render_data({}) %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<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>
</button>
<%= link_to 'BATUK', root_path, class: "navbar-brand" %>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><%= link_to 'Summary', summary_path %></li>
<li><%= link_to 'Statement', statement_path %></li>
<li><%= link_to 'Balances', balances_path %></li>
<li><%= link_to 'Accounts', accounts_path %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to 'Feedback', feedback_path %></li>
<li><%= link_to 'Sign Out', root_path %></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
One way would be to target your controller and action using CSS classes.
In your app/views/layouts/application.html.erb, add controller_name and action_name method calls to generate the CSS classes.
<body class="<%= controller_name %> <%= action_name %>">
CSS
html { height: 100% }
body.home {
height: 100%;
background: url('assets/image.jpg') no-repeat center center;
background-size: cover;
font-family: Arial;
}
.home h1 {
color: black;
font-style: bold;
text-align: center;
}
Create a layout with matching name,for your static_controller, template name should be static.html.erb.
then specify the layout in your controller
class staticController < ApplicationController
layout 'static'
end

Modal window appears in my header instead of the main body

Here is the code i have for my modal window it is located in views/devise/registration/_newfile.html.erb folder
<li><a data-toggle="modal" href="#normalModal" id="secondtry" class="btn btn-success btn outline">sign up</a></li>
<div id="normalModal" class="modal fade" data-toggle="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="panel panel-default">
<div class="panel-heading">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h1 class="modal-title">Signup to sharebox</h1>
</div>
<div class="modal-body">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class: "form-control" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
This is the button that this form makes
When clicked it gives the proper modal window,which appears as a popup,and the screen in the background gets darker as it should,everything works fine.
Question: How to put this button in my navigation bar,in the header? which is located in views/layouts/_header.html.erb,(my
layouts/application.erb file calls this partial)
I tried to put in my _header partial this code
<nav>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "log in", user_session_path, id: "menu-overwritten-loggedout", class: "btn btn-success btn outline" %></li>
<li><%= render "users/registrations/newfile" %></li>
<% end %>
</ul>
</div>
</nav>
As you see i used <%= render "users/registrations/newfile" %>
and the form appeared literally in my navbar,when i click on sign up button
When it actually should appear in the main layout,so the button is in my header but the form must appear in the main body or layout.
how can i fix it?
thank you
I think you need to show the signup dialog anywhere in the site where the SignUp button exists in the navigation bar. So for this make these changes in the code:
<nav>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "log in", user_session_path, id: "menu-overwritten-loggedout", class: "btn btn-success btn outline" %></li>
<li><a data-toggle="modal" href="#normalModal" id="secondtry" class="btn btn-success btn outline">sign up</a></li>
<% end %>
</ul>
</div>
</nav>
And outside this nav where you body is rendered using <%= yield %> or in the container whatever your layout is using for the main body in that add this
<%= render "users/registrations/newfile" %>
And from this partial remove:
<li><a data-toggle="modal" href="#normalModal" id="secondtry" class="btn btn-success btn outline">sign up</a></li>
as you have added this in the nav bar.
I hope this will do the job. Basically what you are doing is writing the dialog code in the layout itself but outside the nav bar. In the body of you view. Not in header.

Customizing bootstrap navbar color

I'm following Ruby on Rails tutorial and trying to customize color of navbar. My header partial is
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container", id="container">
<%= link_to "Dog Park", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</div>
</div>
</header>
I copied to my custom.css.scss from bootstrap 3 sass source files
//navbar
$navbar-inverse-bg: #6dbcdb;
$navbar-inverse-border: #6dbcdb;
// Inverse navbar
.navbar-inverse {
background-color: $navbar-inverse-bg;
border-color: $navbar-inverse-border;
}
and nothing changes still a black navbar. Any help is appreciated. Thanks
I believe the background color for the navbar is actually set on the .container within .navbar-inner. Try this:
//navbar
$navbar-inverse-bg: #6dbcdb;
$navbar-inverse-border: #6dbcdb;
// Inverse navbar
.navbar-inverse .container {
background-color: $navbar-inverse-bg;
border-color: $navbar-inverse-border;
}

Unable to float a twitter bootstrap navbar item right with either class=pull-right or float:right

I am using Twitter bootstrap and Rails and I can't seem to float a navbar item to the right.
I tried using something like this:
<li class="whoami pull-right"> <%= link_to ("Logged in as: " + current_user.name), edit_user_registration_path , :id=>"Edit account"%> </li>
... but the link stayed left and firebug showed "float:left;"
So I tried to overide the css float in bootstrap_and_overrides.css.less and in home.html.erb, but neither worked. Firebug indicated that it found two css float statements, but chose the bootstrap option over mine. So I am left stuck and wondering how to customize the navbar. I am probably doing something wrong here, but I just don't see it.
Here's my code:
application.html.erb:
<!DOCTYPE html>
<html>
<head>
... removed to shorten the length of this post
</head>
<body>
<header>
<%= render 'layouts/navigation' %>
</header>
<div id="main" role="main">
<div class="container">
<div class="content">
<div class="row">
<div class="span12">
<%= render 'layouts/messages' %>
<%= yield %>
</div>
</div>
<footer>
</footer>
</div>
</div> <!--! end of .container -->
</div> <!--! end of #main -->
</body>
</html>
_navigation.html.erb:
<div class="container span12">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<%= link_to "Cases", root_path, :class=>"brand"%>
<% if user_signed_in? %>
<li> <%= link_to "My Cases", cases_path %> </li>
<li> <%= link_to 'Dash', dash_path %> </li>
<% if current_user.has_role? :admin %>
<li> <%= link_to 'Admin', users_path, :id=>"Admin" %> </li>
<% end %>
<li> <%= link_to 'Logout', destroy_user_session_path, :method=>'delete', :id => "Logout" %> </li>
<li class="whoami"> <%= link_to ("Logged in as: " + current_user.name), edit_user_registration_path , :id=>"Edit account"%> </li>
<% else %>
<li> <%= link_to 'Login', new_user_session_path %> </li>
<li> <%= link_to 'Sign up', new_user_registration_path %> </li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
bootstrap_and_overrides.css.less:
#import "twitter/bootstrap/bootstrap";
body {
padding-top: 60px;
}
#import "twitter/bootstrap/responsive";
// Set the correct sprite paths
// ...REMOVED to shorten the length of this post
// Your custom LESS stylesheets goes here
//
// Since bootstrap was imported above you have access to its mixins which
// you may use and inherit here
//
// If you'd like to override bootstrap's own variables, you can do so here as well
// See http://twitter.github.com/bootstrap/less.html for their names and documentation
//
// Example:
// #linkColor: #ff0000;
.whoami {
float:right;
}
When I load a page, I see the navbar, but the last item is not floated to the right.
Looks like this:
And there is another screen cap here that shows some more info.
As you can see, firebug found a "float:right;" option, but indicates a "float:left;" option was used instead.
I have a gap in understanding here, but I am not sure where.
Please address responses to:
Use of pull-right and twitter bootstrap navbar formatting
Overriding css
Interpreting firebug output
EDIT:
I've posted my first ever jsfiddle here: http://jsfiddle.net/uCHQs/2/
It represents a copy/paste of a browser 'show source' and what I gather is the relevent css load and served by rails.
Thanks in advance!
Got it.
Getting it to work required filling the container nested in navbar-inner with more than one unordered list of nav elements. This jsfiddle showed me the way http://jsfiddle.net/N6vGZ/4/
This code works:
<div class="container span12">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "Cases", root_path, :class=>"brand"%>
<ul class="nav">
<% if user_signed_in? %>
<li> <%= link_to "My Cases", cases_path %> </li>
<li> <%= link_to 'Dash', dash_path %> </li>
<% if current_user.has_role? :admin %>
<li> <%= link_to 'Admin', users_path, :id=>"Admin" %> </li>
<% end %>
<li> <%= link_to 'Logout', destroy_user_session_path, :method=>'delete', :id => "Logout" %> </li>
</ul>
<ul class="nav pull-right"> <li> <%= link_to ("Logged in as: " + current_user.name), edit_user_registration_path , :id=>"Edit account"%> </li></ul>
<% else %>
<ul class="nav">
<li> <%= link_to 'Login', new_user_session_path %> </li>
<li> <%= link_to 'Sign up', new_user_registration_path %> </li>
</ul>
<% end %>
</ul>
</div>
</div>
</div>
</div>
have you tried being more specific with your CSS?
.navbar ul.nav li.whoami {
float:right;
}
Sometimes if the prior CSS (bootstrap in this case) is very specific, it requires the same level of specificity (or greater) to override it.
Might be helpful to post a jsfiddle. Screenshots are difficult to debug ;)
The code u have written is
class="nav navbar-nav" col-12
The above code can be replaced like this
class="nav navbar-nav col-12"
and also at next insert
class pull-right in your li button
Try commenting out the line
//...REMOVED
<li><span class="glyphicon glyphicon-chevron-right" style="float:right;"></span> Examples</li>
Add new class = "row"
what's happened ..in your code . when it is coming in mobile device then div is append on other div .
this is simple solution .

Resources