Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use variables for selectors?

I have this variable:

$gutter: 10; 

I want to use it in a selector like so SCSS:

.grid+$gutter {     background: red; } 

so the output becomes CSS:

.grid10 {     background: red; } 

But this doesn't work. Is it possible?

like image 204
Johansrk Avatar asked Oct 05 '12 09:10

Johansrk


People also ask

Can you store selector in variable?

variableName = $("selector here"); does. It does not "store the selector." It runs the selector you give against the current elements in the DOM, creates a jQuery object, puts the matching elements in the jQuery object, and gives you a reference to that jQuery object.

Can you store a selector in a variable quizlet?

You cannot. By using the select from screen tool in Ui Automation activities. By using the UiExplorer tool.


2 Answers

$gutter: 10;  .grid#{$gutter} {     background: red; } 

If used in a string for example in a url:

background: url('/ui/all/fonts/#{$filename}.woff') 
like image 186
glortho Avatar answered Oct 10 '22 17:10

glortho


From the Sass Reference on "Interpolation":

You can also use SassScript variables in selectors and property names using #{} interpolation syntax:

$gutter: 10;  .grid#{$gutter} {     background: red; } 

Furthermore, the @each directive is not needed to make interpolation work, and as your $gutter only contains one value, there's no need for a loop.

If you had multiple values to create rules for, you could then use a Sass list and @each:

$grid: 10, 40, 120, 240;  @each $i in $grid {   .g#{$i}{     width: #{$i}px;   } } 

...to generate the following output:

.g10  { width: 10px; } .g40  { width: 40px; } .g120 { width: 120px; } .g240 { width: 240px; } 

Here are some more examples..

like image 44
fk_ Avatar answered Oct 10 '22 16:10

fk_