주의: mask-image는 ie 브라우저 미지원!!! 

 

mixins.scss

// mixin
@mixin svg($path, $width, $height, $color) {
	width: $width;
    height: $height;
    background-color: $color;
    -webkit-mask-image: url($path);
    mask-image: url($path);
    background-size: cover;
    -webkit-background-size: cover;
    mask-size: contain;
    -webkit-mask-position: center;
    mask-position: center;
    -webkit-mask-repeat: no-repeat;
    mask-repeat: no-repeat;
}

 

사용하기 (@include)

// 노랑색 배경의 검정 로고
.logo {
	width: 92px;
    height: 92px;
	background: #ffe31d;
    span {
    	@include svg('~@/assets/images/logo.svg', 78%, 21px, #000);
    }
}

 

호환성

https://caniuse.com/svg-css

 

SVG in CSS backgrounds | Can I use... Support tables for HTML5, CSS3, etc

Method of using SVG images as CSS backgrounds

caniuse.com

 

반응형

@mixin 

재사용할 스타일의 선언그룹을 만들면 불필요한 중복을 줄일 수 있다.

css에서 다양한 브라우저를 지원하기위해 vendor prefix를 여러차례 재사용할 경우가 있는데 이때 재사용할 CSS 선언그룹을 만들 수 있다. 

 

_mixins.scss

//----------------------------------
//      Vendor Prefix
//----------------------------------
// @param {*} $property Property
// @param {*} $value Value
@mixin prefix($property, $value) {
  -webkit-#{$property}: $value;
  -moz-#{$property}: $value;
  -ms-#{$property}: $value;
  -o-#{$property}: $value;
  #{$property}: $value;
}

// @param {Size} $radius [5px] - Radius
// @require {mixin} prefix

@mixin border-radius($radius: 5px) {
  @include prefix("border-radius", $radius);
}

//----------------------------------
//      Animation & keyframes
//----------------------------------
// Keyframes
// @param {*} $animation-name - Animation name
// @content [Animation css]

@mixin keyframes($animation-name) {
  @-webkit-keyframes #{$animation-name} {
    @content;
  }
  @-moz-keyframes #{$animation-name} {
    @content;
  }
  @-ms-keyframes #{$animation-name} {
    @content;
  }
  @-o-keyframes #{$animation-name} {
    @content;
  }
  @keyframes #{$animation-name} {
    @content;
  }
}

// Animation
// @param {*} $str - name duration timing-function delay iteration-count direction fill-mode play-state ([http://www.w3schools.com/cssref/css3_pr_animation.asp](http://www.w3schools.com/cssref/css3_pr_animation.asp))
// @require {mixin} css3-prefix

@mixin animation($str) {
  @include prefix("animation", $str);
}

//------------------
//   placeholder
//------------------

@mixin optional-at-root($sel) {
  @at-root #{if(not &, $sel, selector-append(&, $sel))} {
    @content;
  }
}

@mixin placeholder {
  @include optional-at-root("::-webkit-input-placeholder") {
    @content;
  }

  @include optional-at-root(":-moz-placeholder") {
    @content;
  }

  @include optional-at-root("::-moz-placeholder") {
    @content;
  }

  @include optional-at-root(":-ms-input-placeholder") {
    @content;
  }
}

//  float  clearfix
@mixin clearfix {
  &::before {
    content: "";
    display: block;
    clear: both;
    overflow: hidden;
   // visibility: hidden;
    height: 0;
  }
}

@mixin ghostVerticalAlign() {
  &:before {
    content: "";
    display: inline-block;
    vertical-align: middle;
    height: 100%;
    width: 0.1px;
  }
}

// generic transform
@mixin transform($transforms) {
  -moz-transform: $transforms;
  -o-transform: $transforms;
  -ms-transform: $transforms;
  -webkit-transform: $transforms;
  transform: $transforms;
}

// rotate
@mixin rotate($deg) {
  @include transform(rotate(#{$deg}deg));
}

// scale
@mixin scale($scale) {
  @include transform(scale($scale));
}
// translate
@mixin translate($x, $y) {
  @include transform(translate($x, $y));
}
@mixin translateX($x) {
  @include transform(translateX($x));
}
@mixin translateY($y) {
  @include transform(translateY($y));
}

//transform origin
@mixin transform-origin($origin) {
  moz-transform-origin: $origin;
  -o-transform-origin: $origin;
  -ms-transform-origin: $origin;
  -webkit-transform-origin: $origin;
  transform-origin: $origin;
}

// Flexbox display
@mixin flexbox {
  display: -webkit-box;
  //@debug: -webkit-flex;
  display: -moz-flex;
  display: -ms-flexbox;
  display: flex;
}

@mixin justify-content($justify) {
  -webkit-justify-content: $justify;
  -moz-justify-content: $justify;
  -ms-justify-content: $justify;
  justify-content: $justify;
  -ms-flex-pack: $justify;
}
@mixin align-items($align) {
  -webkit-algin-items: $align;
  //algin-items: $align;
}

//Get the value for justify-content
@function getJustify($arg) {
  $justify: (
    "sa": space-around,
    "sb": space-between,
    "se": space-evenly,
    "center": center,
    "fs": flex-start,
    "fe": flex-end,
  );

  @each $key, $value in $justify {
    @if ($key == $arg) {
      @return $value;
    }
  }
}

//Get the value for align-items
@function checkAlign($arg) {
  $align: (
    "b": baseline,
    "s": stretch,
    "sa": space-around,
    "sb": space-between,
    "center": center,
    "fs": flex-start,
    "fe": flex-end,
  );

  @each $key, $value in $align {
    @if ($key == $arg) {
      @return $value;
    }
  }
}

// Display flex
@mixin display-flex() {
  @include flexbox;
  @content;
}


@mixin hover-support {
  @media not all and (pointer: coarse) {
    &:hover {
      @content;
    }
  }
}

 

_common.scss

input { 
  @include prefix(box-sizing, border-box);
  @include prefix(appearance, none);
  @include placeholder {
    color: #aeaeae;
    opacity: 1;
  }
}
반응형

Variables (변수)

$를 사용하여 변수를 지정한다. 중복된 단어를 변수로 치환하여 유지보수가 쉬워진다.

 

_variables.scss


// font-family
$font-bk: "NotoBK", san-serif;
$font-b: "NotoB", san-serif;
$font-m: "NotoM", san-serif;
$font-r: "NotoR", san-serif;
$font-dl: "NotoDL", san-serif;
$font-l: "NotoL", san-serif;

// txt-colors
$txt-gray: #747473;
$txt-def: #333;
$txt-disabled: #b8b8b8;
$txt-pink: #f34176;
$txt-red: #ea6575;
$txt-navy: #24364f;
$txt-blue: #2b40ab;
$txt-yw: yellow;

 

_common.scss

- 등록한 변수 사용하기

body {
	font-family: $font-r;
    color: $txt-def;
}

 

반응형

Nesting (중첩)

계층적으로 상속되는 CSS를 더욱 계층적으로 보이게 만드는 기능으로 중괄호가 겹쳐져서 작성됩니다.

관련 요소들을 그룹화 하여 보다 깔끔해지고 중복을 최소화합니다.

// scss
nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  
  li {
  	display: block;
  }
  
  a {
    display: blick;
    padding: 6px 12px;
    text-decoration: none;
  }
}
반응형

CSS Pre-Processor란?

자신만의 특별한 구문(Syntax)을 가지고 CSS를 생성하는 프로그램이다.

CSS의 문제점을 프로그래밍 방식, 즉 변수·함수·상속 등 일반적인 프로그래밍 개념을 사용하여 보완한다.

CSS 전처리기에는 다양한 모듈이 존재하는데 그 중에서도 Sass·Less·Stylus가 가장 많이 사용된다.

CSS 전처리기는 공통 요소 또는 반복적인 항목을 변수 또는 함수로 대체할 수 있는 재사용성, 임의 함수 및 내장 함수로 인해 개발 시간과 비용 절약, 중첩·상속과 같은 요소로 인해 구조화된 코드 유지 및 관리 용이 등의 장점이 있다.

반면 전처리기를 위한 도구가 필요하고 다시 컴파일하는데 시간이 소요된다는 단점도 존재한다.

 

CSS Pre-Processor 사용률

 

SASS
- 가장 오래된 전처리기. 활발히 개발되고 있으며, 가장 많은 개발자들이 선택한 라이브러리
- Ruby, Node-sass 

Less
- 브라우저에 내장된 JS인터프리터만으로 컴파일 가능하므로 디펜던시에서 자유로움
- Node.js 기반으로 구동되며 Sass다음으로 활발히 개척되고 있어서, 유용한 라이브러리나 mixin 구현물들을 쉽게 찾을 수 있다.

PostCSS
- Sass, Less 와 같은 전처리기는 내부에서 제공하는 문법이 정해져있고 한정적이지만 PostCSS는 플러그인이 굉장히 다양하다. 이 때문에 최근 Sass와 Less보다 PostCSS사용에 대한 높은 만족도로 선호도가 올라감.

Stylus
- 상대적으로 프로그래밍 언어의 특징을 많이 포함하고 있습니다.
- CSS 프로퍼티 내에서 연산자나 함수, 루프 등을 비교적 자유롭게 사용할 수 있습니다.
- 반대로 위 특징때문에 문법에 혼재가 있어서 처음 전처리를 시작하는 사람에게는 상대적으로 장벽이 높습니다.

 

 

반응형

 

 

반응형

+ Recent posts