โญ๏ธ css ์ ์ฒ๋ฆฌ๊ธฐ(Pre-Processor)
: ๋ชจ๋๋ณ๋ก ํน๋ณํ Syntax๋ฅผ ๊ฐ์ง๊ณ ์๊ณ ์ฌ๊ธฐ์ mixin, nesting selector, ์์ ๋ฑ ํ๋ก๊ทธ๋๋จธ์ ์ธ ์์๋ฅผ ์ ๋ชฉํด ๋ฐฉ๋ํด์ง๋ CSS ๋ฌธ์์ ์์ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ณ ๊ด๋ฆฌํด์ฃผ๋ ํตํฉ์ ์ธ ์ธ์ด
→ CSS ์ ์ฒ๋ฆฌ๊ธฐ ์์ฒด๋ง์ผ๋ก๋ ์น ์๋ฒ๊ฐ ์ธ์ ๋ชปํ๋ฏ๋ก, ๊ฐ CSS์ ์ฒ๋ฆฌ๊ธฐ์ ๋ง๋ ์ปดํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํ์ฌ CSS๋ฌธ์๋ก ๋ณํํด์ผํจ
์ฅ์
- ์ฌ์ฌ์ฉ์ฑ: ๊ณตํต ์์ ๋๋ ๋ฐ๋ณต์ ์ธ ํญ๋ชฉ์ ๋ณ์ ๋๋ ํจ์๋ก ๋์ฒด
- ์๊ฐ์ ๋น์ฉ ๊ฐ์: ์์ ํจ์ ๋ฐ Built-in ํจ์๋ก ์ธํด ๊ฐ๋ฐ ์๊ฐ์ ๋น์ฉ ์ ์ฝ
- ์ ์ง๊ด๋ฆฌ: ์ค์ฒฉ, ์์๊ณผ ๊ฐ์ ์์๋ก ์ธํด ๊ตฌ์กฐํ๋ ์ฝ๋๋ก ์ ์ง ๋ฐ ๊ด๋ฆฌ ์ฉ์ด
https://kdydesign.github.io/2019/05/12/css-preprocessor/
CSS ์ ์ฒ๋ฆฌ๊ธฐ(Pre-Processor) ๋ฐฐ์ฐ๊ธฐ!
์ด๋ฒ ํฌ์คํธ์์๋ Sass, Less, Stylus์ ๊ฐ์ CSS ์ ์ฒ๋ฆฌ๊ธฐ(CSS Pre-Processor)์ ๋ํด ์์๋ณด์. CSS ์ ์ฒ๋ฆฌ๊ธฐ(CSS Preprocessor)๋ CSS ์์ฑ ๋ชจ๋๋ณ๋ก ํน๋ณํ Syntax๋ฅผ ๊ฐ์ง๊ณ ์๊ณ ์ฌ๊ธฐ์ ๋ฏน์ค์ธ(mixin), ์ค์ฒฉ ์ ๋
kdydesign.github.io
Sass (Syntactically Awesome Style Sheets): ๋ฌธ๋ฒ์ ์ผ๋ก ๋ฉ์ง ์คํ์ผ์ํธ
→ CSS Pre-Processor
- ๋ณต์กํ ์์ ์ฝ๊ฒ ํ ์ ์๊ฒ ํด์ค
- ์ฝ๋์ ์ฌํ์ฉ์ฑ ๋์ฌ์ค
- ์ฝ๋์ ๊ฐ๋ ์ฑ ๋์ฌ์ฃผ์ด ์ ์ง๋ณด์ ์ฝ๊ฒ ํด๊ฒฐ
Sass์์๋ .scss ์ .sass ๋ ๊ฐ์ง์ ํ์ฅ์๋ฅผ ์ง์ํจ → ๋ฌธ๋ฒ์ด ๋ค๋ฆ
ex) ๋ง์ง๋ง์ ์ฝ๋ก (;)์ ๋ถ์ด๋๋ ์๋ถ์ด๋๋, ๊ดํธ๋ก ๊ฐ์ธ๋๋์ ๋ํ ์ฌ๋ถ
sass
$font-stack: Helvetica, san-serif
$primary-color: #333
body
font: 100% $font-stack
color: $primary-color
scss
$font-stack: Helvetica, san-serif;
$primary-color: #333;
body{
font: 100% $font-stack
color: $primary-color
}
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
$ cd styling-with-sass
$ yarn add node-sass
→ Sass๋ฅผ CSS๋ก ๋ณํํด์ฃผ๋ ์ญํ
ex)
$blue: #228be6; // ์ฃผ์ ์ ์ธ
.Button {
display: inline-flex;
color: white;
font-weight: bold;
outline: none;
border-radius: 4px;
border: none;
cursor: pointer;
height: 2.25rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1rem;
background: $blue; // ์ฃผ์ ์ฌ์ฉ
&:hover {
background: lighten($blue, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($blue, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
}
- ์คํ์ผ ํ์ผ์์ ์ฌ์ฉํ ์ ์๋ ๋ณ์ ์ ์ธ → $blue: #228be6
- ์์์ ๋ ๋ฐ๊ฒํ๊ฑฐ๋ ์ด๋ก๊ฒ ํด์ฃผ๋ ํจ์ → lighten() ๋๋ darken()
๋ฒํผ ์ฌ์ด์ฆ ์กฐ์
๋ฒํผ ํฌ๊ธฐ์ large, medium, small ์ค์ ํ ์ ์๋๋ก ๊ตฌํ
- className์ CSS ํด๋์ค ์ด๋ฆ์ ๋์ ์ผ๋ก ๋ฃ์ด์ฃผ๋ ๋ฐฉ๋ฒ
- className={['Button',size].join(' ')}
- className={`Button ${size}`}
- classnames ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ(๊ฐ์ฅ ํธํจ) → ์กฐ๊ฑด๋ถ ์คํ์ผ๋ง ์, ํจ์์ ์ธ์์ ๋ฌธ์์ด, ๋ฐฐ์ด, ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ์ฌ ์์ฝ๊ฒ ๋ฌธ์์ด ์กฐํฉ ๊ฐ๋ฅ
classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
classNames(['foo', 'bar']); // => 'foo bar'
// ๋์์ ์ฌ๋ฌ๊ฐ์ ํ์
์ผ๋ก ๋ฐ์์ฌ ์ ๋ ์์ต๋๋ค.
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
// false, null, 0, undefined ๋ ๋ฌด์๋ฉ๋๋ค.
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
classname๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
yarn add classnames
$blue: #228be6; // ์ฃผ์ ์ ์ธ
.Button {
display: inline-flex;
color: white;
font-weight: bold;
outline: none;
border-radius: 4px;
border: none;
cursor: pointer;
//์ฌ์ด์ฆ ๊ด๋ฆฌ
&.large {
height: 3rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1.25rem;
}
&.medium {
height: 2.25rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1rem;
}
&.small {
height: 1.75rem;
font-size: 0.875rem;
padding-left: 1rem;
padding-right: 1rem;
}
background: $blue; // ์ฃผ์ ์ฌ์ฉ
&:hover {
background: lighten($blue, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($blue, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
}
โญ๏ธ &์ ์๋ฏธ๋ ์์์ ์คํ์ผ ์ ์ฉ
.Button {
&. large {
}
}
/*๊ฐ ์๋ฏธํ๋ ๊ฒ์*/
.Button.large {
}
โญ๏ธ ์ฌ๋ฐฑ ์ค์ : & + &
& + & {
margin-left: 1rem;
}
๋ฒํผ ์์ ์ค์
https://yeun.github.io/open-color/
Open Color
Color scheme for UI design
yeun.github.io
$blue: #228be6; // ์ฃผ์ ์ ์ธ
$gray: #495057;
$pink: #f06595;
.Button {
display: inline-flex;
color: white;
font-weight: bold;
outline: none;
border-radius: 4px;
border: none;
cursor: pointer;
//์ฌ์ด์ฆ ๊ด๋ฆฌ
&.large {
height: 3rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1.25rem;
}
&.medium {
height: 2.25rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1rem;
}
&.small {
height: 1.75rem;
font-size: 0.875rem;
padding-left: 1rem;
padding-right: 1rem;
}
//์์ ๊ด๋ฆฌ
&.blue {
background: $blue;
&:hover {
background: lighten($blue, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($blue, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
}
&.gray {
background: $gray;
&:hover {
background: lighten($gray, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($gray, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
}
&.pink {
background: $pink;
&:hover {
background: lighten($pink, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($pink, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
}
& + & {
margin-left: 1rem;
}
}
โญ๏ธ ๋ฐ๋ณต๋๋ ์ฝ๋๋ mixin ๊ธฐ๋ฅ ์ฌ์ฉํ์ฌ ์ฌ์ฌ์ฉ
$blue: #228be6; // ์ฃผ์ ์ ์ธ
$gray: #495057;
$pink: #f06595;
@mixin button-color($color) {
background: $color;
&:hover {
background: lighten($color, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($color, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
}
.Button {
display: inline-flex;
color: white;
font-weight: bold;
outline: none;
border-radius: 4px;
border: none;
cursor: pointer;
//์ฌ์ด์ฆ ๊ด๋ฆฌ
&.large {
height: 3rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1.25rem;
}
&.medium {
height: 2.25rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1rem;
}
&.small {
height: 1.75rem;
font-size: 0.875rem;
padding-left: 1rem;
padding-right: 1rem;
}
//์์ ๊ด๋ฆฌ
&.blue {
@include button-color($blue);
}
&.gray {
@include button-color($gray);
}
&.pink {
@include button-color($pink);
}
& + & {
margin-left: 1rem;
}
}
outline ์ต์ ๋ง๋ค๊ธฐ
function Button({ children, size, color, outline }) {
return (
<button className={classNames("Button", size, color, { outline })}>
{children}
</button>
);
}
→ props๋ก ๋ฐ์์ ๊ฐ์ฒด ์์ ์ง์ด ๋ฃ์ ๋ค์ classNames()์ ํฌํจ ์์ผ์ฃผ๋ฉด, outline๊ฐ์ด true์ผ ๋๋ง, button์ css์ ์ฉ
@mixin button-color($color) {
background: $color;
&:hover {
background: lighten($color, 10%); // ์์ 10% ๋ฐ๊ฒ
}
&:active {
background: darken($color, 10%); // ์์ 10% ์ด๋ก๊ฒ
}
&.outline {
color: $color;
background: none;
border: 1px solid $color;
&:hover {
background: $color;
color: white;
}
}
}
์ ์ฒด ๋๋น ์ฐจ์งํ๋ ์ต์ : fullWidth
&.fullWidth {
width: 100%;
justify-content: center;
& + & {
margin-left: 0;
margin-top: 1rem;
}
}
function Button({ children, size, color, outline, fullWidth }) {
return (
<button
className={classNames("Button", size, color, { outline, fullWidth })}
>
{children}
</button>
);
}
...rest props ์ ๋ฌ
→ ์ปดํฌ๋ํธ์ onClick ์ค์ ํ๊ฑฐ๋ onMouseMove ๋ฑ ์ด๋ฒคํธ ๊ด๋ฆฌ ์ ์ฌ์ฉ
spread ์ rest ์ฌ์ฉ
function Button({ children, size, color, outline, fullWidth, ...rest }) {
return (
<button
className={classNames("Button", size, color, { outline, fullWidth })}
{...rest}
>
{children}
</button>
);
}
...rest ์ฌ์ฉํ์ฌ ์ง์ ํ props ์ ์ธํ ๊ฐ๋ค์ rest ๊ฐ์ฒด์ ๋ชจ์์ฃผ๊ณ , {...rest} ํด์ฃผ๋ฉด rest์์ ์๋ ๊ฐ์ฒด ์์ ์๋ ๊ฐ๋ค์ ๋ชจ๋ ํ๊ทธ์ ์ค์ ํด์ค
โ๏ธ px vs em vs rem
px: ๊ณ ์ ๋ ์ ๋๊ฐ ๋จ์
em, rem: ํ๊ฒฝ์ ๋ฐ๋ผ ๋ณํ๋ ๋จ์ → ๊ฐ๋ณ๋จ์๋ก์ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ px๋ก ๋ณํ๋จ
em: ๊ฐ์ ์๋ฆฌ๋จผํธ์์ ์ง์ ๋ ๊ฐ ๊ธฐ์ค์ผ๋ก px๋ก ๋ฐ๋์ด ํ๋ฉด์ ํ์๋จ
→ ๊ฐ์ ์๋ฆฌ๋จผํธ์ ์ค์ ๋ ํฌ๊ธฐ ๊ฐ์ด ์์ ๊ฒฝ์ฐ, ์์ ์์์ ์ฌ์ด์ฆ๊ฐ ๊ธฐ์ค์ด ๋จ
rem: ์ต์์ ์๋ฆฌ๋จผํธ์์ ์ง์ ๋ ๊ฐ ๊ธฐ์ค์ผ๋ก ๋ณํ๋จ(๋ณดํต HTML tag์์ ์ง์ ๋ ์ฌ์ด์ฆ ๊ธฐ์ค)
→ ๋ณ๋์ ์ฌ์ด์ฆ ์ค์ ์ ์ํ ๊ฒฝ์ฐ ๊ฐ ๋ธ๋ผ์ฐ์ ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์ ๋ ๊ฐ์ ์์ ๋ฐ์
์ฆ, rem์ ๊ธฐ์ค์ด ๋๋ ํฌ๊ธฐ ํ๋๋ก ๊ณ ์ ๋์ด ์๊ณ , em์ ๊ฐ์ ์๋ฆฌ๋จผํธ๋ ์ด๋์๋ผ๋ ๊ธฐ์ค์ด ๋ณ๊ฒฝ๋ ์ ์์ด ํฌ๊ธฐ ์์ธก ์ด๋ ค์

[CSS] px | em | rem ์ฐจ์ด๋ฅผ ์์๋ณด์
px | em | rem ์ฐจ์ด๋ฅผ ์์๋ณด์ px์ ๊ณ ์ ๋ ์ ๋๊ฐ์ ๋จ์๋ฉฐ, em๊ณผ remํ๊ฒฝ์ ๋ฐ๋ผ ๋ณํ๋ ๋จ์์ ๋๋ค. ๊ณ ์ ๋ px๊ณผ ๋ฌ๋ฆฌ, ํ๊ฒฝ์ ๋ฐ๋ผ ๋ณํ๋ em๊ณผ rem์ ์ฅ์ ์ ๋ฌด์์ด๋ฉฐ, ์ด๋ค ๊ฒฝ์ฐ์ ๊ฐ๊ฐ์ ๋จ์๋ฅผ
monkeydeveloper.tistory.com
'ํ๋ก ํธ์๋ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| styled-components (0) | 2022.12.08 |
|---|---|
| CSS Module (0) | 2022.12.08 |
| ๋ฆฌ์กํธ ๊ฐ๋ฐ ์ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํ ๋๊ตฌ (0) | 2022.12.02 |
| LifeCycle Method (0) | 2022.12.02 |
| ํด๋์คํ ์ปดํฌ๋ํธ (0) | 2022.12.02 |