Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for if/else condition in SCSS mixin

Hi I'm trying to learn SASS/SCSS and am trying to refactor my own mixin for clearfix

what I'd like is for the mixin to be based on whether I pass the mixin a width.

thoughts so far (pseudo code only as I will be including other mixins)

@mixin clearfix($width) {     @if !$width {      // if width is not passed, or empty do this     } @else {          display: inline-block;         width: $width;    } } 

here's how I thought I might call it, but it's not working.

@include clearfix();

or

@include clearfix(100%)

or

@include clearfix(960px)

I'd appreciate any help on the best or right way to do this!

like image 290
clairesuzy Avatar asked Mar 28 '11 08:03

clairesuzy


People also ask

Can I use if-else in SCSS?

In SASS we can make use of the if-else statement, and even else-if , just as we can do in programming languages.

Which directive executes code based on the condition @if @while @each @FOR?

The @if directive executes a set of statements a single time based on a Boolean expression. If, on the other hand, you want to execute the statements multiple times, but still control their execution based on a condition, you can use the @while directive.

What Is syntax in SASS?

SASS supports two syntaxes namely SCSS and Indented syntax. The SCSS (Sassy CSS) is an extension of CSS syntax. This means every valid CSS is a valid SCSS as well. SCSS makes much easier to maintain large stylesheets and can recognize vendor specific syntax, Many CSS and SCSS files use the extension .


2 Answers

You can assign default parameter values inline when you first create the mixin:

@mixin clearfix($width: 'auto') {    @if $width == 'auto' {      // if width is not passed, or empty do this    } @else {      display: inline-block;     width: $width;    } } 
like image 162
Ryan James Avatar answered Sep 17 '22 13:09

Ryan James


You could try this:

$width:auto; @mixin clearfix($width) {     @if $width == 'auto' {      // if width is not passed, or empty do this     } @else {         display: inline-block;         width: $width;    } } 

I'm not sure of your intended result, but setting a default value should return false.

like image 33
simplethemes Avatar answered Sep 20 '22 13:09

simplethemes