Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just started looking in to SASS. Mixins looked like a nice feature, so I made some simple ones to handle my responsive CSS:

$phone-width: 420px;
$desktop-width: 1024px;

@mixin phone {
  @media (max-width: #{$phone-width}) {
    @content;
  }
}

@mixin tablet {
  @media (min-width: #{$phone-width + 1px}) and (max-width: #{$desktop-width - 1px}) {
    @content;
  }
}

@mixin desktop {
  @media (min-width: #{$desktop-width}) {
    @content;
  }
}

I use them like this:

#header {
    width: auto;
    @include phone{
        width: 200px;
    }
}

I figured that I will probably need to define elements that should only be visible on some of the devices, so I made this implementation:

.phone, .tablet, .desktop { 
    display: none; 
} 

@include phone{
    .phone { display: block; } 
};

@include tablet{
    .tablet { display: block; } 
};

@include desktop{
    .desktop { display: block; } 
};
<div class="phone">visible on: phone</div>
<div class="tablet">visible on: tablet</div>
<div class="desktop">visible on: dekstop</div>
<div class="phone tablet">visible on: phone and tablet</div>

Works fine as far as I can tell. Any thoughts on improvements?

share|improve this question
    
If you are just starting with css preprocessors, its worth noting that LESS appears to be the betamax of that sector. Sass (SCSS) looks to be the better choice (for example, Bootsrap is moving to Sass) – Steve Jan 22 at 11:19
    
@Steve Yeah, I've read a lot of discussions about sass vs less. The reason I chose less was because it was easy to try out with a live compiler (winless in my case). I will reconsider using sass instead. Thanks for the input – Johan Jan 22 at 11:36
    
@Steve Decided to switch to SASS :) – Johan Jan 22 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.