All Articles

Useful SASS Mixin

Useful Mixin

유용한 scss 믹스인 모음

layout position

@mixin posCenter {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

@mixin betweenCenter {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

@mixin clear {
  &::after {
    content: "";
    display: block;
    clear: both;
  }
}

Usage

.parent {
    position: relative;

    .child {
        @include posCenter;
    }
}

.parent {
    @include beetweenCenter;

    .child1 {
        //...
    }

    .child2 {
        //...
    }
}

.parent {
    @include clear;

    .child1 {
        float: left;
    }

    .child2 {
        float: right;
    }
}

text

@mixin trim($line: 1) {
  @if ($line === 1) {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  } @else {
    display: -webkit-box;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: $line-height;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: $line;
  }
}

Usage

.some-text { 
    @include trim; 
}

.other-text { 
    @include trim(2); 
}

media query

to handle breackpoints

$sm-device: 480px;
$md-device: 1024px;
$lg-device: 1280px;

@mixin sm {
 @media (max-width: $sm-device) {
     @content;
 }
}

@mixin md {
 @media (max-width: $md-device) {
     @content;
 }
}

@mixin lg {
 @media (max-width: $lg-device) {
     @content;
 }
}

Usage

.tag {
    font-size: 14px;

    @include sm {
        font-size: 12px;
    }

    @include md {
        font-size: 14px;
    }

    @include lg {
        font-size: 16px;
    }
}