How do I conditionally apply a mixin if --my-custom-var is present? For example:
.test {
#if var(--my-custom-var) {
#include someExampleMixin()
}
#if var(--another-custom-var) {
#include someExampleMixin()
}
}
I don't care what the value of the --my-custom-var is but just want to check its existence.
Sass has introduced the variable-exists() function already in alpha. Be aware that Sass can only check for Sass variables. Therefor if you'd really want to use CSS variables you need to define the content of your CSS variable inside a Sass variable, for example $sassVar: /* content */; --cssVar: $sassVar;. Be also aware that the #if statement must be inside a #mixin or a #function to work. I posted a working example below, but here is aslo my Codepen Example since Stack doesn't compile Sass.
Note:
I used "null" inside my $var which basically expresses that there
is no content within this variable, you can pass whatever you
want it won't affect the outcome unless you remove or change the actual
variable.
You can use multiple #if statements which I commented out in this example, but there should always follow an #else statement.
$var: null;
:root {
--someVar: $var;
}
#mixin checkForVariable {
#if variable-exists(var){
body {
background-color: red;
}
}
// #if variable-exists() {
// ...
// }
// #if variable-exists() {
// ...
// }
#else {
body {
background-color: blue;
}
}
}
#include checkForVariable;
I'm only opening this topic because I don't have the necessary 50 reputation points to ask my question directly into the topic I'm interested in.
So, my problem is that I need 25 bootstrap buttons of specific colors (that the user will use to choose among some Pantone variations etc). I found this great topic how to change bootstrap version 4 button color where Zim suggests a SASS solution that obviously saves a lot of CSS writing for the various button states/borders/etc...
However, as I'm new to SASS, I don't understand how it's supposed to be implemented in my webpage. More specifically, I don't know where to put the mentioned color variable and new button classes.
Currently I use this https://www.codeply.com/go/76H7JWg7Zl but of course the border/mouseover/etc use the primary color of the btn-primary class...
After studying the scss source files of bootstrap and also some online resources, I ended up that defining the following SASS mixins would solve my problem:
#import "bootstrap";
.btn-ffa300 {
#include button-variant(#ffa300, darken(#ffa300, 5%));
}
.btn-ffd700 {
#include button-variant(#ffd700, darken(#ffd700, 5%));
}
.btn-97d700 {
#include button-variant(#97d700, darken(#97d700, 5%));
}
.btn-012169 {
#include button-variant(#012169, darken(#012169, 5%));
}
.btn-001489 {
#include button-variant(#001489, darken(#001489, 5%));
}
.btn-ff8200 {
#include button-variant(#ff8200, darken(#ff8200, 5%));
}
.btn-c4d600 {
#include button-variant(#c4d600, darken(#c4d600, 5%));
}
.btn-84bd00 {
#include button-variant(#84bd00, darken(#84bd00, 5%));
}
.btn-0033a0 {
#include button-variant(#0033a0, darken(#0033a0, 5%));
}
.btn-c8c9c7 {
#include button-variant(#c8c9c7, darken(#c8c9c7, 5%));
}
.btn-ff6900 {
#include button-variant(#ff6900, darken(#ff6900, 5%));
}
.btn-78d64b {
#include button-variant(#78d64b, darken(#78d64b, 5%));
}
.btn-009ca6 {
#include button-variant(#009ca6, darken(#009ca6, 5%));
}
.btn-002d72 {
#include button-variant(#002d72, darken(#002d72, 5%));
}
.btn-7c878e {
#include button-variant(#7c878e, darken(#7c878e, 5%));
}
.btn-ff6a13 {
#include button-variant(#ff6a13, darken(#ff6a13, 5%));
}
.btn-ef3340 {
#include button-variant(#ef3340, darken(#ef3340, 5%));
}
.btn-69b3e7 {
#include button-variant(#69b3e7, darken(#69b3e7, 5%));
}
.btn-002f6c {
#include button-variant(#002f6c, darken(#002f6c, 5%));
}
.btn-63666a {
#include button-variant(#63666a, darken(#63666a, 5%));
}
.btn-fe5000 {
#include button-variant(#fe5000, darken(#fe5000, 5%));
}
.btn-c8102e {
#include button-variant(#c8102e, darken(#c8102e, 5%));
}
.btn-003da5 {
#include button-variant(#003da5, darken(#003da5, 5%));
}
.btn-002855 {
#include button-variant(#002855, darken(#002855, 5%));
}
.btn-4f2c1d {
#include button-variant(#4f2c1d, darken(#4f2c1d, 5%));
}
I even confirmed that it's working by rewriting my previous fiddle, and indeed the result ended up EXACTLY as I wanted it to be (https://www.codeply.com/go/uYPxiuNkbu).
Codeply is also "kind enough" to provide in the appropriate tab the compiled css of the sass code used in the fiddle...
But that's an indirect way of preprocessing the sass code... What would a proper way be? Isn't there some sort of online CSS preprocessor? Ie, I read that (https://themestr.app/customize) can be used to customize bootstrap code and then all customizations end up in a compiles custom.css file... How does that work? So far I haven't succeed in achieving this. Some help with this please?
If you want to learn SASS you can go through their official website. If you know css sass won't be a tough one to learn.
For installing gulp there are lots of tutorials you can find!
https://css-tricks.com/gulp-for-beginners/ Here is the one that you can go through.
For using bootstrap 4 theming if you wish you can just learn sass basics and gulp is not required much. You can use propos instead of gulp.
You can simply follow the below steps
To install bootstrap on any local project:
npm init -y
npm install
npm install bootstrap --save
To run manually (Without Gulp):
sass path/filename.sass/filename.scss filename.css
After creating new theme you can use it the same way you're using bootstrap theme. For example: <button class="btn btn-custom-color"></button>
Hope it'll help
/* Change theme color */
$theme-colors: (
"info": #7eff4b,
"danger": #ff50f0,
"primary": #0095ff,
"secondary": #28a745
);
/* Create your own theme */
$theme-colors: (
"custom-color": #900
);
I am trying to set global variables when a theme mixin is included since it seems much more straight-forward to use than this "themify" stuff I find from searching.
The idea is something like having a _themes.scss with
#mixin light-theme { $primary-color: #123456 !global; }
#mixin dark-theme { $primary-color: #654321 !global; }
body.light-theme { #include light-theme }
body.dark-theme { #include dark-theme }
The problem is it always uses the dark-theme value since it is declared last. Is what I am trying to do possible?
I'm trying to set up a mobile-first workflow with SASS and Compass.
Therefore I want to define for the navigation an ul>li horizontal-inline-list via http://compass-style.org/reference/compass/typography/lists/horizontal_list/
I included:
nav.mainnav ul {
#include horizontal-list(1rem);
}
Everything is working fine so far. But how can I get rid of this include when I'm targeting my breakpoint for larger screens?
#include breakpoint($large){
nav.mainnav ul {
// I want to delete the include here
}
}
Is there a simple way to do this or do I have to override the styles manually?
In this instance, mobile first is not your best option.
#media (max-width: 20em) { // whatever your desired breakpoint is
nav.mainnav ul {
#include horizontal-list(1rem);
}
}
Now you don't need to override the styles.
I'm using SASS to create a responsive site. At the moment, my code is structured into a number of partials:
Some default colours and sizes
Overall Layout
Partial for each element
As a result of this organisation I'm finding that I'm ending up with the media queries being declared numerous times through the resulting CSS - it just feels messy. As a result I've been working with a few ideas to keep the current structure, but end up with a simpler resulting code.
My idea goes something like this:
A variable contains a list of the partials to #import
A variable contains a list of the media query sizes (always using min-width, therefore this list is nothing more than a string of numbers)
Each partial (_footer.scss, _header.scss) would then contain a #mixin titles something like - content-footer, content-header, etc
Those #mixin's would take a single variable relating to the media-query and output the appropriate code for that condition.
style.scss would #import each partial from the list,
then cycle through each media-size and partial respectively, calling the function and media size.
The above process would result in something like this being effected...
#import 'footer';
#import 'header';
#include content-footer(0);
#include content-header(0);
#include content-footer(320);
#include content-header(320);
#include content-footer(640);
#include content-header(640);
etc..
My question is this - how do I call the dynamically titled #mixin? Something like content-#{$partial} seems like it should work, but doesn't.
I suppose if this doesn't work, then I could re-import the partial each time, but this seems overkill...
#import 'footer';
#include content(0);
#import 'header';
#include content(0);
#import 'footer';
#include content(320);
#import 'header';
#include content(320);
#import 'footer';
#include content(640);
#import 'header';
#include content(640);
Personally I find comfort in declaring media queries at many places throughout the document. It feels object oriented and it's really easy to keep track of what's actually going on. I'm usually interested in editing one module in particular, not the entire layout, so it makes more sense to me.
I have a mixin that looks something like this:
$mq-small: 30em;
$mq-medium: 50em;
$mq-large: 70em;
#mixin mq($size) {
#if $size == small { #media only screen and (min-width: $mq-small) { #content; } }
#else if $size == medium { #media only screen and (min-width: $mq-medium) { #content; } }
#else if $size == large { #media only screen and (min-width: $mq-large) { #content; } }
}
Which allows me to write media queries like this:
.element {
// generic rules
#include mq(medium) {
// size-specific rules
}
}
Which creates this output:
.element {
// generic rules
}
#media only screen and (min-width: 50em) {
.element {
// size-specific rules
}
}
Then, when the project is ready to be deployed, I merge the media queries manually in the output CSS into one place to remove bloat.
It's not perfectly clean, but it's also not necessary and the workflow is awesome. Last I heard, this merging is supposed to happen automatically in future versions of SASS. Perhaps that's just a rumor, though, but it would be nice.