Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in my Rails app is the Bootstrap CSS?

Learning rails... so I use the bootstrap-sass gem... Where are the actual bootstrap CSS files? Seems like there is some sort of magic going on to include them. What if I want to tweak a property of the CSS....

like image 604
slindsey3000 Avatar asked Aug 08 '13 12:08

slindsey3000


People also ask

Where is CSS file in rails?

Then we place it's corresponding CSS in this folder: /* app/assets/stylesheets/components/product_card. scss */ .

How do I access Bootstrap CSS?

In order to use Bootstrap CSS, you need to integrate it into your development environment. To do that, you simply need to create a folder on your computer. In that folder, save your compiled CSS and JS files and a new HTML file where you'll load Bootstrap.


2 Answers

You're using the bootstrap-sass gem which is preferred by many Rails developers. So the Twitter Bootstrap CSS files are in the gem (in vendor/assets/stylesheets/).

Developers use a gem to include Bootstrap files because it makes things easier to update when new Bootstrap versions are released by simply updating the gem (plus gems are the Rails way, eh?). The bootstrap-sass gem is popular because Sass is the default format for stylesheets in Rails. An alternative is the twitter-bootstrap-rails gem which uses the native Bootstrap LESS format for stylesheets.

Using the gem, you need to modify the file app/assets/javascripts/application.js:

#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

Best practice is to add a file named app/assets/stylesheets/bootstrap_and_overrides.css.scss:

# app/assets/stylesheets/bootstrap_and_overrides.css.scss
@import "bootstrap";
body { padding-top: 60px; }
@import "bootstrap-responsive";

This shows an example of overriding the Bootstrap body style to add padding to accommodate the Bootstrap navigation bar. Then just add application-specific CSS (or Sass) in additional files in app/assets/stylesheets/.

I've written an in-depth article:

Twitter Bootstrap and Rails

that goes into greater details and shows how to set up the application layout, navigation links, Rails flash messages, and form builders for Twitter Bootstrap. The article could be helpful if you want to know more.

like image 139
Daniel Kehoe Avatar answered Oct 17 '22 14:10

Daniel Kehoe


Unfortunately, if you use the gem, the css files are hidden from you. If you do a bundle show bootstrap-sass you'll see where the files for the gem are, and you can see the stylesheets being used in there.

If you view the gem's files, you'll see the stylesheets being used in vendor/assets/stylesheets/.

I'd recommend just not using the gem, and getting your own Bootstrap stylesheets and putting them in your application's vendor/assets/stylesheets/.

like image 27
varatis Avatar answered Oct 17 '22 12:10

varatis