[TIL] 220208

Lee Syong·2022년 2ė›” 8ėž
0

TIL

ëŠĐ록 ëģīęļ°
173/204
post-thumbnail

📝 ė˜Ī늘 한 ęēƒ

  1. SCSS - variables / nesting / mixins / @extend / responsive mixins - @content

📚 ë°°ėšī ęēƒ

1. SCSS

1) variables

src/scss íī더ė— _variables.scss 파ėžė„ 만든ë‹Ī.
파ėž ėīëĶ„ ė•žė— 밑ėĪ„ė„ ėķ”ę°€í•œ ęēƒė€ í•īë‹đ 파ėžė€ scss로 ë‚Ļė„ ëŋ css 파ėžëĄœ ëģ€í™˜í•˜ė§€ ė•Šė„ ęēƒėž„ė„ 나타ë‚īęļ° ėœ„í•œ ęēƒėīë‹Ī.

// _variables.scss

$bg: red;
// styles.scss

@import "_variables";

body {
  background: $bg;
}

2) nesting

h1 {
  color: red;
  &:hover: blue;
  span {
    color: orange;
    &:hover: green;
  }
}

3) mixins

mixinė— ėļėžëĨž ë„Ģė–īėĪŒėœžëĄœėĻ ęļ°ëģļė ėœžëĄœ ė ėšĐ할 CSS styleė— ëģ€ęē―ė„ 가하거나 scss 파ėžė—ė„œ @if ~ @else ęĩŽëŽļęģž 같ė€ 프로ę·ļ래밍ė„ ė‚ŽėšĐ하ęļ° ėœ„í•ī mixinsëĨž ė‚ŽėšĐ할 ėˆ˜ ėžˆë‹Ī.

<!-- index.html -->

<a>ėēŦ ëēˆė§ļ 타ėī틀</a>
<a>두 ëēˆė§ļ타ėī틀</a>
<a>ė„ļ ëēˆė§ļ 타ėī틀</a>
<a>ë„Ī ëēˆė§ļ 타ėī틀</a>
// _variables.scss

$red-color: red;
$blue-color: blue;

(1) mixin argument

// _mixins.scss

@mixin link($color) {
  display: block;
  margin-bottom: 10px;
  font-size: 50px;
  color: $color;
}

link는 colorëĨžėļėžëĄœë°›ė„ėˆ˜ėžˆë‹Ī.colorëĨž ėļėžëĄœ 받ė„ ėˆ˜ ėžˆë‹Ī.color는 ėž„ė˜ëĄœ ė •í•īėĪ€ ėīëĶ„ėž ëŋ ė–žë§ˆë“ ė§€ ë‹ĪëĨļ ėīëĶ„ė„ ė“ļ ėˆ˜ ėžˆë‹Ī.

// styles.scss

@import "_mixins";

a {
  &:nth-child(odd) {
    @include link(blue);
  }
  &:nth-child(even) {
    @include link(red);
  }
}

ė‹Īė œëĄœ linkė˜ ėļėžëĄœ blueė™€ redëĨž ë„Ģė–īėĪŽë‹Ī.

(2) @if ~ @else

ë‹ĪėŒęģž 같ėī ėž‘ė„ąí•ī도 ęē°ęģžëŠ” (1)ęģž 같ë‹Ī.

// _mixins.scss

@mixin link($word) {
  display: block;
  margin-bottom: 10px;
  font-size: 50px;
  @if $word == "odd" {
    color: blue;
  } @else {
    color: red;
  }
}

ėī때 == ëĨž ė‚ŽėšĐí•īė•ž 한ë‹Ī. (=== 는 ėœ íšĻ하ė§€ ė•Šë‹Ī.)

// _styles.scss

@import "_mixins";

a {
  &:nth-child(odd) {
    @include link("odd");
  }
  &:nth-child(even) {
    @include link("even");
  }
}

linkė˜ ėļėžëĄœ color 대ė‹  ëŽļėžė—īė„ ë„Ģė–īėĪŽë‹Ī.

4) @extend

ęļ°ėĄīė˜ CSS styleė„ ëģ€ęē― ė—†ėī ėžŽė‚ŽėšĐ 하ęģ ėž 할 때 @extendëĨž ė‚ŽėšĐ한ë‹Ī.
ėžŽė‚ŽėšĐ 하ęģ ėž 하는 ė„ íƒėž ė•žė— %ëĨž ëķ™ė—Žė•ž 한ë‹Ī.

ðŸ’Ą mixinsė€ ėƒí™Đė— 따띾 ë‹ĪëĨīęēŒ ė―”ë”Đ하ęģ  ė‹ķė„ 때 ė‚ŽėšĐ하는 반ëĐī, extends는 같ė€ ė―”ë“œëĨž ėĪ‘ëģĩí•īė„œ ė‚ŽėšĐ하ęģ  ė‹ķė§€ ė•Šė„ 때 ė‚ŽėšĐ한ë‹Ī.

// _buttons.scss

%button {
  border-style: none;
  border-radius: 10px;
  margin: 10px;
  padding: 20px;
  color: white;
  background: darkcyan;
  font-size: 20px;
}
@import "_buttons";

a {
  @extend %button;
  text-decoration: none;
}

button {
  @extend %button;
  cursor: pointer;
}

5) responsive mixins

(1) @content

@contentëĨž ė‚ŽėšĐ하ëĐī mixinė˜ contentëĨž ė§€ė •í•  ėˆ˜ ėžˆë‹Ī.
ėī는 mixinė— ėļėžëĨž ė „닎하는 ęēƒęģžëŠ” ë‹ĪëĨīë‹Ī.

mixin argumentė™€ @contentëĨž í•Ļęŧ˜ ė‚ŽėšĐ하ëĐī 반ė‘형 디ėžėļė„ ęĩŽí˜„í•  때 ë§Īėš° ėœ ėšĐ하ë‹Ī.

ė—Žęļ°ė„œëŠ” 간ë‹Ļ하ęēŒ ëļŒëžėš°ė €ė˜ 큎ęļ°ëĨž ëģ€ęē―하ëĐī ė§€ė •í•œ ęē―ęģ„ëĨž ęļ°ėĪ€ėœžëĄœ H1 텍ėŠĪíŠļė˜ ėƒ‰ėƒėī 바뀌도록 ė―”ë“œëĨž ėž‘ė„ąí•īëģīë Īęģ  한ë‹Ī.

<h1>타ėī틀</h1>
// _mixins.scss

// ėž„ė˜ëĄœ ė•„ëŽī렇ęēŒë‚˜ ė§€ė •í•œ 값ë“Īėīë‹Ī
$minIphone: 320px;
$maxIphone: 480px;
$minIpad: $maxIphone + 1;
$maxIpad: 640px;
$minTablet: $maxIpad + 1;
$maxTablet: 960px;

@mixin responsive($device) {
  @if $device == "iphone" {
    @media screen and (min-width: $minIphone) and (max-width: $maxIphone) {
      @content;
    }
  }

  @if $device == "ipad" {
    @media screen and (min-width: $minIpad) and (max-width: $maxIpad) {
      @content;
    }
  }
  
  @if $device == "tablet" {
    @media screen and (min-width: $minTablet) and (max-width: $maxTablet) {
      @content;
    }
  }
}

responsive mixinė˜ ėļėžëĄœ iphone, ipad, tabletėī ė „닎된 ęē―ėš°ëĨž 나눠ė„œ 각각 ë‹ĪëĨļ ëŊļ디ė–ī ėŋžëĶŽëĨž 만ë“Ī도록 했ë‹Ī.
ëŊļ디ė–ī ėŋžëĶŽė˜ 표현ė‹ ëķ€ëķ„ė€ @content로 ėž‘ė„ąí•˜ė—Ž styles.scssė—ė„œ ęē―ėš°ė— 따띾 ė„Īė •í•  ėˆ˜ ėžˆë„록 했ë‹Ī.

// styles.scss

@import "_mixins";

h1 {
  font-size: 50px;
  color: indianred;
  
  @include responsive("iphone") {
    color: yellowgreen;
  }
  
  @include responsive("ipad") {
    color: gray;
  }
  
  @include responsive("tablet") {
    color: darkmagenta;
  }
}

responsive mixinė˜ ėļėžëĄœ iphoneėī ė „닎하ė—Ž ėƒė„ąëœ ëŊļ디ė–ī ėŋžëĶŽė˜ ėĄ°ęąī(min/max-width 큎ęļ°)ė— 맞는 ęē―ėš° colorëĨž yellowgreenėœžëĄœ 바ęūžë‹Ī.

responsive mixinė˜ ėļėžëĄœ ipadėī ė „닎하ė—Ž ėƒė„ąëœ ëŊļ디ė–ī ėŋžëĶŽė˜ ėĄ°ęąī(min/max-width 큎ęļ°)ė— 맞는 ęē―ėš° colorëĨž grayėœžëĄœ 바ęūžë‹Ī.

responsive mixinė˜ ėļėžëĄœ tabletėī ė „닎하ė—Ž ėƒė„ąëœ ëŊļ디ė–ī ėŋžëĶŽė˜ ėĄ°ęąī(min/max-width 큎ęļ°)ė— 맞는 ęē―ėš° colorëĨž darkmagentaėœžëĄœ 바ęūžë‹Ī.


âœĻ ë‚īėž 할 ęēƒ

  1. 프로토타ėž…, í•īė‰Ž 테ėīëļ”

  2. CSS PROJECTS

profile
ëŠĨ동ė ėœžëĄœ ė‚īėž, 행ëģĩ하ęēŒðŸ˜

0개ė˜ 댓ęļ€