Stylesheets inclusion in Rails - css

I am trying to incorporate bootstrap 4 into my Rails 5.2 project. Currently, my app/stylesheets/application.css, has the following in it:
/*
*= require_tree .
*= require_self
*/
The instructions, in addition to renaming it application.scss, call for deleting:
*= require_tree .
*= require_self
And adding:
#import 'bootstrap'
Isn't "require_tree ." needed to include all the stylesheets in the stylesheets folder? and the "require_self", to include any styles I may decide to add in the application.scss file?
So if I remove these two statements, would I need to include every single stylesheet individually, using an #import statement. For instance, if I have: user.scss, I would add:
#import 'user'
I've seen some examples where the require_tree and require_self are kept. Your advice is appreciated.

use asterisk to path like
#import "path/to/*"
if you add all sxss in 'stylesheet' path (ex: stylesheet/a.sxss, b.sxss)
#import "*"
else you add all sxss in 'stylesheet/page' path (ex: stylesheet/page/a.sxss, b.sxss)
#import "page/*"

Related

Rails 5 - Load applications.css.scss as the last one in head

In my rails5 application, I have application.css.scss and controller specific stylesheets such as home.scss, about.scss etc.
When I check the inspect in my browser, it seems application.css is loaded before home.css and about.css. Because of this, most of the overrides used in applications.css were simply ignored.
application.css.scss
/*
*= require_self
*= require material-design-iconic-font/css/material-design-iconic-font.min
*= require nprogress-turbolinks5
*= require app
*/
How can I load applications.css.scss as the last one in head section?

Ruby on Rails require CSS error

I have a problem with Ruby on Rails rquire statements.
I want to add a reset.css file before the require_tree.
I have a application.css:
/*
*= require reset
*= require_tree
*= require_self .
*/
By doing this I am always gearing an error: wrong number of arguments (1 for 0). Where is the problem?
require_self should be without . argument. Here is specification of this directive. As you can see it doesn't have parameters.

Rails does not include styles using require_tree and scss

There is the following code application.css.scss:
#import "bootstrap-sprockets";
#import "bootstrap";
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require_self
*/
.table tbody > tr > td.vertical-align {
vertical-align: middle;
}
This file is in 'stylesheets' directory. There is also 'home.css.scss' file in the same dir. But if I move this style from 'application' file to 'home' file browser doesn't see this style. What's the trouble? How can I fix it? Thanks!
in my case I have imported bootstrap at the end of the file followed by styles, this must be the problem
*= require_tree .
*= require_self
*/
#import "bootstrap-sass-official/assets/stylesheets/bootstrap-sprockets";
#import "bootstrap-sass-official/assets/stylesheets/bootstrap";
.table tbody > tr > td.vertical-align {
vertical-align: middle;
}
also I'd say you should try to put require tree after require self:
*= require_self
*= require_tree .
this makes more sense.
From the Rails Asset Pipeline docs:
If you want to use multiple Sass files, you should generally use the Sass #import rule instead of these Sprockets directives. When using Sprockets directives, Sass files exist within their own scope, making variables or mixins only available within the document they were defined in.
http://guides.rubyonrails.org/asset_pipeline.html
So in your case you can move the styles you declared in application.css.scss into a separate file (say: table_styles.css.scss) and then rewrite your manifest file as:
#import "bootstrap-sass-official/assets/stylesheets/bootstrap-sprockets";
#import "bootstrap-sass-official/assets/stylesheets/bootstrap";
#import "table_styles";
// Import any other files that would have been imported by require_tree
The advantage of this approach is that you can use sass variables and mixins and you have better control over the load order of your stylesheets.

rails 4 and compass, how import only one time my sass files?

I have a rail 4 project with "stylesheets/application/index.css.scss" with my all css files:
/*
*= require jquery.ui.all
*= require_tree ../shared
*= require_tree ../design
*= require_tree ../layout
*= require_self
*= require_tree .
*/
rails compile all css in only one, minimized (in prod).
I need to import #import "shared/header" in many files.
exemple: in "stylesheets/layout/main.css.scss"
#import 'shared/header';
.header
{
#extend .header_common_overview;
[...]
}
but I #import 'shared/header' in others files too. The result is :
when rails compile in only one file, there are many times the same rules ".header_common_overview", because I import it in different files.
I tried to put the "import" instruction directly in index.css.scss, but it does't works.
So how can I import only one time a file, and be abble to call the content in all others files?
First, don't use require_tree . You lose control over the include order of your CSS files, potentially leading to cascading issues - styles being overwritten that really should not be.
I've learned to avoid sprockets' require lines in the main SASS files for reasons similar to what you describe.
It can lead to duplication, particularly when using =require_tree all over the place
Variables/mixins/etc... can't be included via sprockets (I'd love to be proven wrong about this though)
In your index.css.scss you might consider simply putting
#import "vendor";
#import "shared";
#import "design";
#import "layout";
// Your main styling here.
#import "another_file";
These #import lines correspond to other sass files. shared.css.scss for example might look like
/*
*=require ./shared/header
*/
The idea is to
Keep clean separation/organization of your asset includes
Explicitly define each asset include so you retain full control over include order
Use SASS #importinstead of Sprockets =require directive to keep variables, mixins, etc... present in an included file available throughout.
My solution is : create all.css.scss with :
/*
*= require jquery.ui.all => static, don't need import
*/
#import 'included/**/*'; //all files included (at first time)
#import 'all/**/*'; //all real css files which requires included file (in second times)
The order is respected and controlled.
The included files is present only one time
the included files are shared in each real css files.
thx for help.

bootstrap conflicting with font-awesome?

I have installed these gems : font-awesome-rails and bootstrap (gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails',
:github => 'anjlab/bootstrap-rails')
In my application.css.scss, I have
*= require twitter/bootstrap
*= require_self
*= require_tree .
*/
#import "font-awesome";
#import "font.css.scss";
where the last file contains #import url("//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700"); The problem, is that no icon is loaded in my html because fontawesome seems not to be fetched. So I installed the font-awesome gem, but it didn't change anything. Could someone help ?
Can you try this one in your Gemfile (outside of the assets group)? gem 'font-awesome-rails', '4.0.1.0' After you bundle install, make any change (i.e. add an empty line) to your application.css.scss to get it to re-compile.

Resources