Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How write *::selection{} in scss\sass?

I tried to do the following, but it does not work:

* {
    &::selection { text-decoration: underline; }
}
like image 726
Vitaly Batonov Avatar asked Jul 13 '11 15:07

Vitaly Batonov


2 Answers

That's the way I do it:

// define it
@mixin selection {
    ::-moz-selection { @content; }
    ::selection { @content; }
}

// use it
@include selection {
    color: white;
    background: black;
}

Update

I recommend to just use ::selection {} with autoprefixer instead of a mixin. This will make your code thinner and your brain lighter :)

In this case, autoprefixer will transform this:

::selection {
    color: white;
    background: black;
}

...(depending on your target browsers/configuration) into something like that:

::-moz-selection {
    color: white;
    background: black;
}

::selection {
    color: white;
    background: black;
}
like image 158
yckart Avatar answered Sep 28 '22 06:09

yckart


Mixins work with pseudo element selectors ;) see my mixin:

$prefixes: ("-moz-", "");
@mixin selection($color, $background) {
    @each $prefix in $prefixes {
        ::#{$prefix}selection {
            color: $color;
            background: $background;
        }
    }
}

how to use:

@include selection(white, black);

of course you can make it far more flexible, but it was sufficient for me ;)

like image 38
Peter Avatar answered Sep 28 '22 05:09

Peter