๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Development/CSS

[Sass]Sass ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ์ •๋ฆฌ

by DevIseo 2022. 9. 7.

Sass ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ์ •๋ฆฌ

1) ๋ณ€์ˆ˜์— ๋ฐ์ดํ„ฐ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ

$๋ณ€์ˆ˜๋ช… : ๋ณ€์ˆ˜์— ๋„ฃ์„ ๊ฐ’;

$mainColor : #ff0000;

.red {
    color: $mainColor;
}

 

2) ํŒŒ์ผ import ํ•˜๊ธฐ

@import 'ํŒŒ์ผ ๊ฒฝ๋กœ';

\- ์ฃผ๋กœ ๋ชจ๋“  ํŽ˜์ด์ง€์— ํ•„์š”ํ•œ CSS reset์„ ํŒŒ์ผ์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์ž‘์„ฑํ•˜๊ณ  import

//src/reset.scss 
body {
    margin: 0;
}

div {
    box-sizing: border-box;
}

\- Detail.scss์— reset.scss ํŒŒ์ผ์„ import ํ•ฉ๋‹ˆ๋‹ค.

@import './reset.scss';

 

3) Nesting

- ์…€๋ ‰ํ„ฐ ๋Œ€์‹  ์‚ฌ์šฉ ๊ฐ€๋Šฅ

// Selector
div.container h4 {
    color: blue;
}

div.container p {
    color: green;
}

// Nesting
div.container {
    h4 {
        color: blue;
    }
    p {
        color: green;
    }
}

 

4) Extend

@extend #์•„์ด๋””๋ช…; OR @extend .ํด๋ž˜์Šค๋ช…;

- ์šฐ์„  Detail.js์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ alert๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค.

<div className='my-alert'>
   <p>์žฌ๊ณ ๊ฐ€ ์–ผ๋งˆ ๋‚จ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค!</p>
</div>

 

\- Detail.scss์— my-alert์— ๋Œ€ํ•œ css ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

.my-alert {
    background: #eeeeee;
    padding: 20px;
    border-radius: 5px;
    max-width: 500px;
    width: 100%;
    margin: auto;
    p {
        margin-bottom: 0;
    }
}

 

\- ๋‹ค๋ฅธ ์ƒ‰์ƒ์˜ alert UI๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด?

: ์ด๋Ÿฐ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜๋ฉด ์ฝ”๋“œ ์ˆ˜๊ฐ€ ๊ธธ์–ด์ง€๊ณ  ๊ฐ€๋…์„ฑ๋„ ๋–จ์–ด์ง‘๋‹ˆ๋‹ค.

.my-alert {
    background: #eeeeee;
    padding: 20px;
    border-radius: 5px;
    max-width: 500px;
    width: 100%;
    margin: auto;
    p {
        margin-bottom: 0;
    }
}

.my-alert-red {
    background: red;
    padding: 20px;
    border-radius: 5px;
    max-width: 500px;
    width: 100%;
    margin: auto;
    p {
        margin-bottom: 0;
    }
}

 

\- ์•„๋ž˜์™€ ๊ฐ™์ด my-alert๋ฅผ extendํ•˜์—ฌ ์‚ฌ์šฉ

.my-alert {
    background: #eeeeee;
    padding: 20px;
    border-radius: 5px;
    max-width: 500px;
    width: 100%;
    margin: auto;
    p {
        margin-bottom: 0;
    }
}

.my-alert-red {
    @extend .my-alert;
    background: #fa8072;
}

\- Detail.js์—์„œ <div>์˜ ํด๋ž˜์Šค๋ช…์„ 'my-alert-red'๋กœ ์ ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šคํƒ€์ผ์ด ์ ์šฉ๋œ ๊ฒƒ์„ ํ™•์ธ.

 

 

5) @mixin / @include

@mixin ํ•จ์ˆ˜๋ช…() {} / @include ํ•จ์ˆ˜๋ช…();

\- SASS์— ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ @mixin์œผ๋กœ ์„ ์–ธํ•˜๊ณ , @include๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

@mixin func() {
    background: #eeeeee;
    padding: 20px;
    border-radius: 5px;
    max-width: 500px;
    width: 100%;
    margin: auto;
    p {
        margin-bottom: 0;
    }
}

.my-alert {
    @include func();
}

 

Reference

https://dori-coding.tistory.com/entry/React-SASS-๊ฐœ๋…-์„ค์น˜-๋ฐ-๊ธฐ๋ณธ-๋ฌธ๋ฒ•?category=1038534

๋Œ“๊ธ€