Ask Your Question
3

How can SASS be used to add a prefix to all CSS selectors that are created?

asked 2021-12-13 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-12-08 20:00:00 +0000

bukephalos gravatar image

SASS provides a built-in function called "selector-extend" that can be used to add a prefix to all CSS selectors. Here's an example:

$prefix: ".my-prefix";

// Define a base selector
.my-selector {
  color: red;
}

// Extend the base selector with the prefix
#{$prefix}#{selector-nest(".my-selector", "&")} {
  @extend .my-selector;
}

This will compile to:

.my-selector, .my-prefix .my-selector {
  color: red;
}

The #{$prefix} interpolation is used to insert the prefix into the selector. The selector-nest function is used to concatenate the prefix with the base selector. Finally, the @extend directive is used to apply the styles from the .my-selector selector to the prefixed selector.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-13 11:00:00 +0000

Seen: 20 times

Last updated: Dec 08 '21