F
F
ForestEsprit2016-05-27 13:01:13
Sass
ForestEsprit, 2016-05-27 13:01:13

How to create a condition in sass with a check for the presence of the data attribute?

Is it possible to write a condition like this in sass:

if(data-name) {
  .name { display:block; }
}

In fact, I need to set a rule: if such a data-attr exists, then show me this block, and if another data-attr exists, then show me the second block.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-05-27
@ForestEsprit

Your sass compiles to css and at this stage it does not see any "blocks".
It is also impossible to make such conditions in css itself.
But you can implement this behavior by hand using selectors:

.first {display: block }
.second { display: none }

[data-name] {
  .first {display: none }
  .second { display: block }
}

This method has restrictions on the structure of the element tree, like any other css selectors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question