Bootstrap Custom Variable Rails SASS - ruby-on-rails

In the stylesheets/bootstrap directory I have:
_variables.scss I have:
$black: #000 !default;
$grayDark: #333 !default;
...
$textColor: $grayDark !default;
This file is imported in the bootstrap.scss file:
// Core variables and mixins
#import "bootstrap/variables"; // Modify this for custom colors, font-sizes, etc
#import "bootstrap/mixins";
...
In the stylesheets directory I have bootstrap_include.scss
#import "bootstrap/bootstrap";
#import "bootstrap/responsive";
I want to override the default text to be black, so I create a custom variables file and imported it in my custom bootstrap file
_variables_custom.scss:
$black: #000 !default;
$textColor: $black !default;
and then in bootstrap_custom.scss
#import "variables_custom";
Finally in the application.css.scss
*= require bootstrap_custom
*= require bootstrap_include
When I refresh the page, bootstrap_custom is always empty, I'm a css newbie, and why it isn't working?
Thanks
NOT SURE IF THIS IS NICE BUT I SOLVED IT USING:
my bootstrap_custom now includes
$black: #000 !default;
$textColor: $black !default;
#import "bootstrap/bootstrap";
#import "bootstrap/responsive";
and my application.css.scss now only includes
*= require bootstrap_custom

in your stylesheet, remove !default
that is, in your file _variables_custom.scss - instead of:
$black: #000 !default;
$textColor: $black !default;
use this instead:
$black: #000;
$textColor: $black;
!default is a sass variable flag that allows for reassignment before or after the !default is declared, with the non !default value always winning.
so:
$foo: black;
$foo: red !default;
body { background: $foo; }
will result in a black body background, even though red is declared after black

While we are talking about variables, there's nothing related to CSS. Variables, in this case, are used by SASS/Compass a CSS preprocessor tool, used by Rails to make the use of CSS more easy (eg. by using variables).
So when you insert your variables in a file, and let it be processed alone, it wont produce any CSS code unless you make any use of then in a valid CSS attribute.
If you want to test it, add to your bootstrap_custom.css something like:
body {
background-color: $black;
}
And you will see your $black variable being used.

Related

How do you connect the 'hamburgers' gem in Ruby on Rails?

I tried doing their Ruby on Rails instruction for installation but when I included #import 'hamburgers' it doesn't know where to search for the file. So what I tried to do was copy the files from the '_sass/hamburgers' directory from the 'hamburgers' gem into my project.
files inside the _sass/hamburgers directory pasted in my stylesheets folder.
Even after pasting the files in my project's stylesheet folder an error still shows up.
Error states
Error: Undefined variable: "$hamburger-padding-y".
on line 4:12 of app/assets/stylesheets/_base.scss
padding: $hamburger-padding-y $hamburger-padding-x;
If I understand correctly _base.scss is connected to hamburgers.scss so there shouldn't really be any issues. Hope to get some help!! :>
EDIT:
Here's my application.scss
/*
* This is a manifest file that'll be compiled into application.scss, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's
* vendor/assets/stylesheets directory 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 other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
#import "hamburgers";
Here is also hamburgers.scss
#charset "UTF-8";
/*!
* Hamburgers
* #description Tasty CSS-animated hamburgers
* #author Jonathan Suh #jonsuh
* #site https://jonsuh.com/hamburgers
* #link https://github.com/jonsuh/hamburgers
*/
// Settings
// ==================================================
$hamburger-padding-x : 15px !default;
$hamburger-padding-y : 15px !default;
$hamburger-layer-width : 40px !default;
$hamburger-layer-height : 4px !default;
$hamburger-layer-spacing : 6px !default;
$hamburger-layer-color : #000 !default;
$hamburger-layer-border-radius : 4px !default;
$hamburger-hover-opacity : 0.7 !default;
$hamburger-active-layer-color : $hamburger-layer-color !default;
$hamburger-active-hover-opacity: $hamburger-hover-opacity !default;
// To use CSS filters as the hover effect instead of opacity,
// set $hamburger-hover-use-filter as true and
// change the value of $hamburger-hover-filter accordingly.
$hamburger-hover-use-filter : false !default;
$hamburger-hover-filter : opacity(50%) !default;
$hamburger-active-hover-filter: $hamburger-hover-filter !default;
// Types (Remove or comment out what you don’t need)
// ==================================================
$hamburger-types: (
3dx,
3dx-r,
3dy,
3dy-r,
3dxy,
3dxy-r,
arrow,
arrow-r,
arrowalt,
arrowalt-r,
arrowturn,
arrowturn-r,
boring,
collapse,
collapse-r,
elastic,
elastic-r,
emphatic,
emphatic-r,
minus,
slider,
slider-r,
spin,
spin-r,
spring,
spring-r,
stand,
stand-r,
squeeze,
vortex,
vortex-r
) !default;
// Base Hamburger (We need this)
// ==================================================
#import "base";
// Hamburger types
// ==================================================
#import "types/3dx";
#import "types/3dx-r";
#import "types/3dy";
#import "types/3dy-r";
#import "types/3dxy";
#import "types/3dxy-r";
#import "types/arrow";
#import "types/arrow-r";
#import "types/arrowalt";
#import "types/arrowalt-r";
#import "types/arrowturn";
#import "types/arrowturn-r";
#import "types/boring";
#import "types/collapse";
#import "types/collapse-r";
#import "types/elastic";
#import "types/elastic-r";
#import "types/emphatic";
#import "types/emphatic-r";
#import "types/minus";
#import "types/slider";
#import "types/slider-r";
#import "types/spin";
#import "types/spin-r";
#import "types/spring";
#import "types/spring-r";
#import "types/stand";
#import "types/stand-r";
#import "types/squeeze";
#import "types/vortex";
#import "types/vortex-r";
// ==================================================
// Cooking up additional types:
//
// The Sass for each hamburger type should be nested
// inside an #if directive to check whether or not
// it exists in $hamburger-types so only the CSS for
// included types are generated.
//
// e.g. hamburgers/types/_new-type.scss
//
// #if index($hamburger-types, new-type) {
// .hamburger--new-type {
// ...
// }
// }
Per the comments, the OP requested this info though it does not answer the primary question.
Don't do any of the "ruby on rails" stuff from the official documentation.
Create app/assets/stylesheets/hamburgers.css and get the code from here (or just copy in the file). This is the dist/hamburgers.css file the documentation mentions.
In your application view (e.g., application.html.erb), in the <head> put in the stylesheet reference: <%= stylesheet_link_tag "hamburgers" %>
Put in some html markup (get from the documentation example here). Also, be sure to put the <script> code from the example (it's at the end).
When I did this, I got a fresh rails site to work exactly the same as how the hamburger author's example.html works.
As to why you get that hamburger-padding-y error, I don't think you are supposed to need to put the css AND scss files. The yarn install combined with the rails gem is probably supposed to take care of this, but I couldn't recreate the error because I couldn't get a rails server to work if I install the gem (hamburgers, which resolved to v1.1.3). I was using Ruby 2.6.2 / Rails 6.0.3.3
RESULT:

Compass: Why do I need to do a double import for it to work?

Using Rails 3.2, compass-rails.
I have the following files:
screen.css.scss
#import "compass";
application.css.scss
/*
*= require_self
*= require_tree .
*/
#import "screen";
.content {
background-color: #eee;
padding: 20px;
margin: 0 -20px;
#include border-radius(6px, 6px);
#include box-shadow(0,0,0,.15);
}
application.html.erb (layout, just the relevant line):
<%= stylesheet_link_tag "application", :media => "all" %>
This works fine for my app layout.
Next, for the specific page I'm trying to style (home) I have home.css.scss:
#import url(http://fonts.googleapis.com/css?family=Oleo+Script+Swash+Caps:700);
#import "compass/css3/text-shadow"; <<-- This is the line I don't understand
.welcome {
text-align: center;
h1 {
font-family: 'Oleo Script Swash Caps', cursive;
font-weight: 700;
font-size: 110pt;
line-height: 130px;
#include single-text-shadow;
}
}
As soon as I drop #import "compass/css3/text-shadow";, my code breaks with Undefined mixin 'single-text-shadow'. The same thing happens if I move the text-shadow import line into application.css.css.
My question is why? I've included the entire compass framework in application.css.css via screen.css.scss. That file is loaded for sure because I see it working. So why do I need to do a double-include like this?
Unless home.css.scss is importing application.css.scss, this makes sense as expected behavior. You only need to import it once per compiled document, it will be available to any partials that are included in that document (and come after the import in your code).
It's common to import Compass once into a _base.scss partial (along with any other plugins you want available), and then import that partial anywhere you need your base settings.
i don't know if i understood your question correctly, but there is no double import here.
compass is a modular framework. you are not forced to use all it's parts. instead you can include only the parts you want. for simplicity, you can also require sub-parts like:
# application.sass
#import "compass"
#import "compass/css3/"
#import "compass/utilities/"

Unable to overwrite bootstrap-sass's font with that of font-awesome

I am using bootstrap-sass and the font-awesome ( https://github.com/littlebtc/font-awesome-sass-rails) gems. I would like to override the bootstrap font setting from that of font-awesome.
From font-awesome's site I can override the bootstrap defaults, if I just import if after bootstrap's import.
#import 'bootstrap';
#import 'font-awesome';
I have done the above, but font-awesome's font is not overriding. I have pushed my project on github - https://github.com/murtaza52/rails-base. The url is accessible on localhost:3000/posts
I will appreciate if someone can help me overriding bootstraps's default font with those of font-awesome's
Modify your application.css.scss to look like below
#import "font-awesome";
$baseFontFamily: 'FontAwesome';
#import "bootstrap";
...
#import "bootstrap-responsive";
//#import "scaffolds";
#import "posts";
WHY?
You move import "font-awesome" at the top and then define baseFontFamily because that's what bootstrap uses to define font-family for all the elements. Check Typography and links block in the middle. If you import bootstrap after this, FontAwesome will be used by default.
You should remove import "scaffolds"; line because scaffolds.css.scss will reset your font family for body element which will be inherited by every other element.
If you can't avoid importing it before bootstrap. I hope that helps.
For those of you guys using Bootstrap 3.2+ (I guess), here's the list of SASS variables you can modify:
https://github.com/twbs/bootstrap-sass/blob/master/assets/stylesheets/bootstrap/_variables.scss
In our case, we want to make sure to define $font-family-base before doing #import "bootstrap".
By setting $font-family-base before the line below is reached, Bootstrap uses our $font-family-base instead (otherwise, it defaults to $font-family-base-serif, also defined in the variables.scss above).
$font-family-base: $font-family-sans-serif !default;
This is how my application.css.sass looks like
/*
*= require_tree .
*= require_self
*/
#import "fonts"
#import "compass"
#import "bootstrap"
And I have the following in _fonts.css.sass (You don't have to have it in a separate file)
$font-family-sans-serif: 'Roboto', verdana, arial, sans-serif
I don't know if this helps you but at least sometimes when template code seems valid you need to force refresh your browser with ctrl+shirt+r to see changes (works at least in mozilla).

How to use twitter bootstrap-sass mixins

Using Rails 3.
Right now in each new .css.scss file that I am creating, I want to be able to use the mixins, but I just couldn't seem to use it.
Here is my bootstrap_import.css.scss:
// Import bootstrap
// --------------------------------------------------
#import "bootstrap";
#import "bootstrap-responsive";
#media (min-width: 1200px) {
.span12, .container {
width: 1170px;
}
}
#import "base";
When I have another file called a.css.scss and I try to #include border-radius(12px);, but it just gives this error:
Undefined mixin 'border-radius'.
Same goes to variables, I would like to change some colors on some variables so that I can use it on any file without needing to include in each CSS file.
Thank you.
Only one import works in my project, probably a bug or a mistake from my side.
My solution with bootstrap-sass v2.0.3.1 to achieve a solution is:
(not 100% what do you expect, I know..)
I create a new scss file like: myAppBase.css.scss
/* override bootstrap default variables */
$linkColor: #FF0000;
$linkColorHover: #000;
#import "bootstrap";
/* App variables */
$bgImage: url('bg.jpg');
$radius: 4px;
$maxHeight:600px;
$minHeight:400px;
$bSize:1px;
and if i need bootstrap and my variables in an other *.css.scss file.
I include this line on the top:
#import "myAppBase";
I followed this guidelines and I was able to fix the error "undefined border-radius".
https://github.com/thomas-mcdonald/bootstrap-sass

Color Variables with bootstrap-sass

Getting around to using sass in my latest rails project, specifically the bootstrap-sass gem to get all of twitter bootstrappy goodness.
Is it possible to reference the variables already defined? The vendor /assets/stylesheets/bootstrap/_variables.scss file contains declarations for colors.
$blue: #049cdb !default;
$blueDark: #0064cd !default;
$green: #46a546 !default;
$red: #9d261d !default;
Do I need to redefine these in the top of my base application.css.scss file or can I import/reference the variables file? I've tried a couple of approaches like this:
$bodyBackground: $black;
#import 'bootstrap'
But that errors out with undefined variable $black.
First, define the variable $black:
$black: #000;
$bodyBackground: $black;
#import "bootstrap";
or change it to:
$bodyBackground: #000;
#import "bootstrap";
Remove the below two lines from application.scss file:
*= require_self
*= require_tree .

Resources