SASS #at-root directive not compiling - sass

I have the following code in an .scss file:
*[data-toggle="popover"]{
font-weight: bold;
#at-root .list-group-item & {
margin-left: 2rem;
}
}
On http://sassmeister.com it compiles as I want:
*[data-toggle="popover"] {
font-weight: bold;
}
.list-group-item *[data-toggle="popover"] {
margin-left: 2rem;
}
But locally it doesn't do anything with the #at-root directive, so I get:
*[data-toggle="popover"] {
font-weight: bold;
#at-root .list-group-item & {
margin-left: 2rem;
}
}
I'm not sure how to troubleshoot this. I've updated my sass and compass to the latest versions. What else can I do?

Related

Sass nesting of h1 within a .title selector

I have tried sass
.title {
font-weight: bold;
// more title styles
&h1 {
font-size: 30px;
}
}
Resulting css is:
.titleh1 {
font-weight: bold;
// more title styles
}
.titleh1 {
font-size: 30px;
}
Is there anyway I can keep the h1 nested in the .title to give css output like this?
.titleh1 {
font-weight: bold;
// more title styles
}
h1.title {
font-size: 30px;
}
Yes, you just need the #at-root directive to jump back out of your nested selector:
.title {
font-weight: bold;
// more title styles
#at-root {
h1#{&} {
font-size: 30px;
}
}
}

How to extract common scss code from multiple files?

After converting a lot of redundant crappy css files into scss files, I have a bunch of scss files. I'm pretty sure there is a lot of common css repeated among these files and I would like to extract this code.
As an example, let's say I have this block of scss code (let's call it block A) :
.test {
color: white;
.toto {
background: red;
font-size: 12px;
}
}
And another block (that we'll call block B) :
.test {
color: black;
.toto {
background: blue;
font-size: 12px;
text-align: center;
}
}
I want to be able to extract the following common scss code from block A and B :
.test {
.toto {
font-size: 12px;
}
}
It seems like a simple task to do, but with a large list of long scss files, it's really painful to do it manually. After searching for a while I didn't find any tool for that.
An intermediary solution could be to convert sass code to a multi-dimensionnal associative array and to process arrays to find intersections, but I could not find any simple solution to do that either, so any help would be appreciated.
There are a few approaches but in this instance, I would opt for a variable:
$base-font-size: 12px;
.test {
color: white;
.toto {
background: red;
font-size: $base-font-size;
}
}
.test {
color: black;
.toto {
background: blue;
font-size: $base-font-size;
text-align: center;
}
}
Or you could add a toto mixin with some defaults and use that:
#mixin toto($background: red, $text-align: left, $font-size: 12px) {
.toto {
background: $background;
text-align: $text-align;
font-size: $font-size;
}
}
.test {
color: white;
#include toto();
}
.test {
color: black;
#include toto(blue, center);
}
EDIT: or use extend:
.font-size-12 {
font-size: 12px;
}
.test {
color: white;
.toto {
#extend .font-size-12;
background: red;
}
}
.test {
color: black;
.toto {
#extend .font-size-12;
background: blue;
text-align: center;
}
}

Appending the parent selector to the end generates the incorrect result with Elixir/Libsass

I have the following SCSS:
.btn {
color: #000;
#at-root {
a#{&} {
display: inline-block;
}
}
}
I'm expecting the following CSS:
.btn { color: #000; }
a.btn { display: inline-block; }
But when I compile it using gulp-sass, I get this instead:
.btn { color: #000; }
.btn a.btn { display: inline-block; }
This appears to be a bug with Libsass, which is what gulp-sass compiles with. If you want to get the correct results, you'll need to switch to using the Ruby compiler for Sass.

how to inheritance multiples class through sass

I have a scenario in sass
.A{
background-color: red;
Padding :20px;
h4{ padding-bottom :20px;}
}
// another class
.B{
background-color : blue;
padding : 20px
h4{ padding-bottom:20px}
}
Question: how can i combine padding and h4 together in SASS without to repeating padding and h4 properties
The most straight forward way is to use #extend.
%common_properties {
padding: 20px;
h4 {
padding-bottom: 20px;
}
}
.a {
#extend %common_properties;
background-color: red;
}
.b {
#extend %common_properties;
background-color: blue;
}
You really don't save much by using sass/scss for this small of a redundancy
A solution with scss:
.a, .b {
padding: 20px;
background-color: red;
& h4 {
padding-bottom: 20px;
}
}
.b{
background-color:blue;
}
That solution in plain css:
.a, .b {
padding: 20px;
background-color: red;
}
.a h4, .b h4 {
padding-bottom: 20px;
}
.b {
background-color: blue;
}
Here is what that will look like:
http://codepen.io/cawoelk/pen/Ciqyw

Something between #include and #extend with sass

Is it possible to include a css rule in sass without duplicate the code?
With extend we are extending the code, but i dont want that eiter. I want include it, without duplicating code.
For example
SCSS:
.heading {
font-size: 16px;
font-family: my-cool-font;
}
.box {
background: red;
h1 {
#extend .heading;
color: white;
}
}
.my-other-box {
.heading {
color: black;
}
}
HTML
<div class="box">
<h1>My heading</h1>
</div>
<div class="my-other-box">
<h1 class="heading">My heading</h1>
</div>
CSS
.heading, .box h1 {
font-size: 16px;
font-family: my-cool-font;
}
.box {
background: red;
}
.box h1 {
color: white;
}
.my-other-box .heading,
.my-other-box .box h1,
.box .my-other-box h1 {
color: black;
}
So the two last rules there are because its extending (I understand the benifits of it).
But if i want to both use classes, and extends i dont want it to extend, just include it. But i dont want it to duplicate the code.
I want:
CSS
.heading, .box h1 {
font-size: 16px;
font-family: my-cool-font;
}
.box {
background: red;
}
.box h1 {
color: white;
}
.my-other-box .heading {
color: black;
}
If you use an extend class (or use a class name that differs from one you're repeating elsewhere), you can get the output you're looking for:
%heading, .heading {
font-size: 16px;
font-family: my-cool-font;
}
.box {
background: red;
h1 {
#extend %heading;
color: white;
}
}
.my-other-box {
.heading {
color: black;
}
}
Output:
.box h1, .heading {
font-size: 16px;
font-family: my-cool-font;
}
.box {
background: red;
}
.box h1 {
color: white;
}
.my-other-box .heading {
color: black;
}

Resources