Sass @Extend Base/Default Without Also Extending Pseudo-Classes

How to extend a class and ignore the pseudoclass in Sass

When you @extend a selector, you extend every instance of it. That includes matching pseudoclasses (.foo:before) and compound selectors (.bar .foo .baz).

If this is not the behavior you want, then you will need to create an additional selector to extend from:

.sprite {
// stuff
}

%foo, .sprite-icon-thumbs-up {
@include sprite(-130px, -239px, 51px, 22px);
}

.sprite-icon-thumbs-up:hover {
@include sprite(-185px, -239px, 51px, 22px);
}

.some-class {
@extend .sprite;
@extend %foo;
}

Including another class in SCSS

Looks like @mixin and @include are not needed for a simple case like this.

One can just do:

.myclass {
font-weight: bold;
font-size: 90px;
}

.myotherclass {
@extend .myclass;
color: #000000;
}

Why overriding Spree default styles is inconsistent?

Selectors have different specificity value. It affects overriding priority:

From spec:

A selector's specificity is calculated as follows:

count the number of ID selectors in the selector (= a) count the
number of class selectors, attributes selectors, and pseudo-classes in
the selector (= b) count the number of type selectors and
pseudo-elements in the selector (= c) ignore the universal selector
Selectors inside the negation pseudo-class are counted like any other,
but the negation itself does not count as a pseudo-class.

Concatenating the three numbers a-b-c (in a number system with a large
base) gives the specificity.

Example:

*               /* a=0 b=0 c=0 -> specificity =   0 */
LI /* a=0 b=0 c=1 -> specificity = 1 */
UL LI /* a=0 b=0 c=2 -> specificity = 2 */
UL OL+LI /* a=0 b=0 c=3 -> specificity = 3 */
H1 + *[REL=up] /* a=0 b=1 c=1 -> specificity = 11 */
UL OL LI.red /* a=0 b=1 c=3 -> specificity = 13 */
LI.red.level /* a=0 b=2 c=1 -> specificity = 21 */
#x34y /* a=1 b=0 c=0 -> specificity = 100 */
#s12:not(FOO) /* a=1 b=0 c=1 -> specificity = 101 */

You can calculate selector Specificity with this tool

Is it possible to include the parent properties in an extended Sass class?

What you are looking for is the @extend directive. @extend allows you share a set of CSS properties from one selector to another. This means that you would only need to use the container-featured class.

Example

.container {
margin: 10%;
background: #eee;

&-featured {
@extend .container;
border: 2px solid #999;
}
}

compiles to:

.container,
.container-featured {
margin: 10%;
background: #eee;
}

.container-featured {
border: 2px solid #999;
}

Does LESS have an extend feature?

Yes, Less.js introduced extend in v1.4.0.

:extend()

Rather than implementing the at-rule (@extend) syntax used by SASS and Stylus, LESS implemented the pseudo-class syntax, which gives LESS's implementation the flexibility to be applied either directly to a selector itself, or inside a statement. So both of these will work:

.sidenav:extend(.nav) {...}

or

.sidenav {
&:extend(.nav);
...
}

Additionally, you can use the all directive to extend "nested" classes as well:

.sidenav:extend(.nav all){};

And you can add a comma-separated list of classes you wish to extend:

.global-nav {
&:extend(.navbar, .nav all, .navbar-fixed-top all, .navbar-inverse);
height: 70px;
}

When extending nested selectors you should notice the differences:

nested selectors .selector1 and selector2:

.selector1 {
property1: a;
.selector2 {
property2: b;
}
}

Now .selector3:extend(.selector1 .selector2){}; outputs:

.selector1 {
property1: a;
}
.selector1 .selector2,
.selector3 {
property2: b;
}

, .selector3:extend(.selector1 all){}; outputs:

.selector1,
.selector3 {
property1: a;
}
.selector1 .selector2,
.selector3 .selector2 {
property2: b;
}

,.selector3:extend(.selector2){}; outputs

.selector1 {
property1: a;
}
.selector1 .selector2 {
property2: b;
}

and finally .selector3:extend(.selector2 all){};:

.selector1 {
property1: a;
}
.selector1 .selector2,
.selector1 .selector3 {
property2: b;
}

Can a CSS class inherit one or more other classes?

There are tools like LESS, which allow you to compose CSS at a higher level of abstraction similar to what you describe.

Less calls these "Mixins"

Instead of

/* CSS */
#header {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}

#footer {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}

You could say

/* LESS */
.rounded_corners {
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
}

#header {
.rounded_corners;
}

#footer {
.rounded_corners;
}


Related Topics



Leave a reply



Submit