Media query with rails bootstraps-saas - ruby-on-rails

I m trying to make media queries with bootstraps. I put this in my application.css.scss file but that doesn't work :
/*
* 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 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 styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require 'masonry/transitions'
*= require_tree
*= require_self
*/
#import "bootstrap-sprockets";
#import "bootstrap";
#import "font-awesome";
$backgroundNav : #ffffff;
$backgroundBody : #E9E9E9;
$red : #FD0E48;
#media screen and (max-width: 770px){
.body {
background-color: red;
}
}
body {
background: $backgroundBody;
}
What should i do in order to make the query to be detected ?

Application.css.scss is a manifest file and you should define which files you need to require.
If you want to compile codes in your application.css.scss you should add = require_self in top of your application.css.scss

I believe you have the wrong selector.
.body {
background-color: red;
}
.body selects an elements with the class body, like <div class="body">foobar</div>
You probably meant to select the body element:
body {
background-color: red;
}
Also, put your #media styles after the "normal" ones.
body {
background: $backgroundBody;
}
/* responsive body styles */
#media screen and (max-width: 770px){
body {
background-color: red;
}
}
Here's the fiddle - resize the window to see the change.
And since RoR has SASS support, why not use it?
body {
background: $backgroundBody;
#media screen and (max-width: 770px){
background: $backgroundBody;
}
}

First of all your CSS in the media query is wrong.
It should be:
body {
background-color: red;
}
and not .body as it will search for element that has class "body", example:
<p class="body>
content
</p>
Secondly try to write your media queries (#media styles)after your general css towards the end of the files, i think that should fix the issue.
Hope that helps :)

Related

Adding bootstrap mixin into Rails project. rbenv error?

This is a continuation of rails, bootstrap, media-breakpoint-only but I don't think one needs to refer back to that.
I have a Rails project in which I am attempting to use the Bootstrap function media-breakpoint-only.
A search of my computer shows that media-breakpoint-only is defined in ~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-4.1.3/assets/stylesheets/bootstrap/mixins/_breakpoints.scss
My ~/app/assets/stylesheets/application.scss is
/*
* 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, 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
*/
// See
// https://www.youtube.com/watch?v=vcBXXOdPfgE&index=8&list=PLYM1n9xxMy4ClO2GjX73U3BLsXx9Z7wh5
$navbar-default-bg: red;
// Shnelvar
// See https://github.com/twbs/bootstrap-sass
#import "bootstrap-sprockets";
// Custom bootstrap variables must be set or imported *before* bootstrap.
#import "bootstrap";
// Shnelvar
// For ralph-shiny-button etc.
#import "ralph";
// Shnelvar
// See https://stackoverflow.com/questions/33404154/bootstrap-change-the-navbar-font-size
// See https://teamtreehouse.com/community/how-do-you-change-the-bootstrap-font-style
.nav a{
color: white !important;
// font-size: 3.8em !important;
font-size: 2.8em;
}
#import "bootstrap/breakpoints";
// See https://getbootstrap.com/docs/4.0/layout/overview/
h1 {
#include media-breakpoint-only(xs) {
color: red;
}
#include media-breakpoint-only(sm) {
color: green;
}
#include media-breakpoint-only(md) {
color: blue;
}
#include media-breakpoint-only(lg) {
color: yellow;
}
#include media-breakpoint-only(xl) {
color: orange;
}
}
Please note that I have
* require_tree .
* require_self
and not
*= require_tree .
*= require_self
so, I believe, the "requires" are commented out.
When I attempt to load my webpage I get the following error:
Sass::SyntaxError in StaticPages#root
Showing ~/app/views/layouts/application.html.erb where line #15 raised:
File to import not found or unreadable: bootstrap/breakpoints.
Load paths:
~/app/assets/config
~/app/assets/images
~/app/assets/javascripts
~/app/assets/stylesheets
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/coffee-rails-4.2.2/lib/assets/javascripts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/jquery-rails-4.3.3/vendor/assets/javascripts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/actioncable-5.2.0/lib/assets/compiled
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activestorage-5.2.0/app/assets/javascripts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/actionview-5.2.0/lib/assets/compiled
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/turbolinks-source-5.1.0/lib/assets/javascripts
~/node_modules
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/stylesheets
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/javascripts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/fonts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/images
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/stylesheets
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/javascripts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/popper_js-1.14.3/assets/javascripts
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/stylesheets
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-sass-3.3.7/assets/stylesheets
Apparently, ~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bootstrap-4.1.3/assets/stylesheets/bootstrap/mixins/ isn't in any of the load paths.
I'm thinking I might have an rbenv error but I'm not sure.
My environment is
Rails 5.2.0
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
Please further note that if I get rid of the #import "bootstrap/breakpoints"; and the h1 css that I am able to use a Bootstrap navbar successfully. Thus, I assume, I have installed bootstrap sucessfully.
Help!!!
in gemfile
gem 'bootstrap-sass', '~> 3.2.0’
then do file inside the stylesheet assets name it bootstrap_and_overrides.css.scss
#import "bootstrap”;
This video can help you about how to add bootstrap gem to your ruby on rails app
https://www.youtube.com/watch?v=mxYsMacHHtU&t=122s

How can i make foundation global variables available to all my scss files in a rails app

I was wondering how i could make the color variables (e.g. $light-gray) available accross all my scss (i am using foundation 6).
I thought using *= require foundation_and_overrides at the top of my application.scss would make all variables available accross all scss files but i get the follwing error Undefined variable: "$light-gray".
Thanks for your help.
I do not know if this is the best practice. But I do it in such a way that I have another scss file called shared_variables.scss
// shared_variables.scss
$light-gray: #ccc;
Then inside foundation_and_overrides.scss, I import it (if you're going to use a variable in this file):
// foundation_and_overrides.scss
// ...
#import 'foundation-functions';
#import 'shared_variables'; // Add this line
// ...
Then import that as well for any other .scss file that depends on the global variables;
// posts.scss
#import 'shared_variables'; // Add this line
.posts-container {
background: $light-gray;
}
Basically, we can't use #import to make a variable globally. This is the reason:
Sass/Sass will take the file that you want to import and combine it
with the file you're importing into so you can serve a single CSS file
to the web browser.
Check this for more detail
Let's say you have a couple of Scss files, _reset.scss and base.scss. We want to import _reset.scss into base.scss.
_reset.scss
html,
body,
ul,
ol {
margin: 0;
padding: 0;
}
base.scss
#import 'reset';
body {
font: 100% Helvetica, sans-serif;
background-color: #efefef;
}
The base.scss will be generated to base.css like this:
base.css
html, body, ul, ol {
margin: 0;
padding: 0;
}
body {
font: 100% Helvetica, sans-serif;
background-color: #efefef;
}
So import works like replacing reset content to the position importing reset in base!

Bootstrap Custom Variable Rails SASS

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.

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/"

Change Bootstrap SASS border-radius (and other mixins) - Rails

I'm using the rails gem Bootstrap SASS, and was wondering how I can overwrite some of the mixins. Specifically border radius and the like.
I thought I could simply overwrite it like:
#import "bootstrap";
#import "bootstrap-responsive";
#mixin border-radius($radius: 1px) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
But that doesn't seem to do squat! Any ideas?
Bootstrap SASS - Mixins
That is actually a mixin with an argument, SASS calls it a "mixin" but it's really a parametric or dynamic mixin. But to clarify your question, do you want to change the default value for the border radius mixin radius, or do you want to do it on the fly on a class by class basis? If you are looking to change the default you should do it directly in the Bootstrap mixin.scss file.
But if you want the latter, you just add the mixin to any class the same as you would add a property such as font-size, but then you pass in the value you wish to declare each time you use the mixin. Like this:
.giant-box {
width: 400px;
height: 400px;
background-color: #900;
#include border-radius(20px);
}
.small-box {
width: 10px;
height: 10px;
background-color: #900;
#include border-radius(3px);
}
In LESS it would look like this:
.giant-box {
width: 400px;
height: 400px;
background-color: #900;
.border-radius(20px);
}
Check the Customize and Download page on the Bootstrap site. There are list of LESS variables listed on the page.
Usually the SASS variables are the same but with '$' instead of a '#'. To customize bootstrap without having to re-download everything after changing one or two variables you can instead do this in your .scss file:
$border-radius-base: 0;
#import "bootstrap";
If you want to overwrite the Bootstrap variables, you need to do so before the import. E.g.
$baseFontFamily: $serifFontFamily; // Use serif fonts instead of sans-serifs
#import "bootstrap";
#import "bootstrap-responsive";
This is particularly late to the game but in Bootstrap 4 you can overwrite the enable-rounded Sass variable like so:
#enable-rounded false;
#import "~bootstrap/scss/bootstrap" /*lots of ways to do this step, see other answers herein*/
Bootstrap Sass Options Documentation

Resources