Micah Henning
Micah Henning

Reputation: 2185

Testing for Null Value in LessCSS

I would like to test for whether a variable is defined before writing its CSS property. For example:

#someElement {
    if (@variable) {
        background-image: url(@variable);
    }
}

The reason for this is to prevent an empty url() property value when the variable is null. I can't seem to find any way to do this from the LessCSS documentation.

Upvotes: 1

Views: 968

Answers (1)

seven-phases-max
seven-phases-max

Reputation: 11820

Here is how such things are resolved in "Less Way" (that's why the feature is marked with "low priority" at the Less repo):

library.less:

@variable: null; // or whatever meaningful non-string value

#someElement {
    & when (isstring(@variable)) {
        background-image: url(@variable);
    }
}

user.less:

@import "library.less";
@variable: "foo.bar/baz.png";

That's it.

Upvotes: 2

Related Questions