๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Syntax-02-nesting

Sass(SCSS) Syntax - 2. ์ค‘์ฒฉ#

โ—๏ธ ํ•ด๋‹น ๊ธ€์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜์—์„œ HTML & CSS, SASS(SCSS) Part์˜ ๋ฐ•์˜์›… ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜์ž๋ฃŒ(Sass(SCSS) ์™„์ „ ์ •๋ณต!)๋ฅผ ๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ค‘์ฒฉ(Nesting)#

Sass(SCSS)๋Š” ์ค‘์ฒฉ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ฃผ์–ด ์ƒ์œ„ ์„ ํƒ์ž์˜ ๋ฐ˜๋ณต์„ ํ”ผํ•˜๊ณ  ํŽธ๋ฆฌํ•˜๊ณ  ์ง๊ด€์ ์œผ๋กœ ๋ณต์žกํ•œ ๊ตฌ์กฐ์˜ CSS๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

.section {  width: 100%;  .list {    padding: 20px;    li {      float: left;    }  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.section {width: 100%;}.section .list {padding: 20px;}.section .list li {float: left;}

์ƒ์œ„ ์„ ํƒ์ž ์ฐธ์กฐ & (Ampersand)#

์ค‘์ฒฉ ์•ˆ์—์„œ & ํ‚ค์›Œ๋“œ๋Š” ์ƒ์œ„(๋ถ€๋ชจ) ์„ ํƒ์ž๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์น˜ํ™˜ํ•œ๋‹ค.

โ˜๏ธ & == โ€˜์ƒ์œ„(๋ถ€๋ชจ) ์„ ํƒ์žโ€™

.btn {  position: absolute;  &.active {    color: red;  }}
.list {  li {    &:last-child {      margin-right: 0;    }  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.btn {position: absolute;}.btn.active {color: red;}.list li:last-child {margin-right: 0;}

& ํ‚ค์›Œ๋“œ๊ฐ€ ์ƒ์œ„(๋ถ€๋ชจ) ์„ ํƒ์ž๋กœ ์น˜ํ™˜ํ•˜๋Š” ๊ฐœ๋…์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‘์šฉํ•˜๋Š”๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

.fs {  &-small { font-size: 12px; }  &-medium { font-size: 14px; }  &-large { font-size: 16px; }}

์ปดํŒŒ์ผํ•˜๋ฉด

.fs-small {font-size: 12px;}.fs-medium {font-size: 14px;}.fs-large {font-size: 16px;}

์ค‘์ฒฉ ๋ฒ—์–ด๋‚˜๊ธฐ @at-root#

@at-root ๋Š” ์ค‘์ฒฉ์—์„œ ๋ฒ—์–ด๋‚˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ์ด๋‹ค.

์ค‘์ฒฉ์—์„œ ๋ฒ—์–ด๋‚˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์˜ ํ•œ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๋ฉด,

.section {  $width: 100px;  $height: 200px;  width: $width;  height: $height;  .item {    width: $width;    height: $height;  }}

$width , $height ์™€ ๊ฐ™์ด ์ด๋ฆ„ ์•ž์— $ ๊ฐ€ ๋ถ™์€ ๊ฐ’์€ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ”— Sass(SCSS) Syntax - 3. ๋ณ€์ˆ˜ ์ฐธ์กฐ

๊ทธ๋Ÿฐ๋ฐ ์ด ๋ณ€์ˆ˜๋Š” ์œ ํšจ๋ฒ”์œ„๊ฐ€ ์žˆ์–ด์„œ ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋œ .section ์˜ ๋ธ”๋ก ( {} ) ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๋ฐ–์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํŒŒ์ผ๋˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋ž˜์„œ .section ์˜ ํ•˜์œ„ ์š”์†Œ๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด @at-root ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค.

.section {  $width: 100px;  $height: 200px;  width: $width;  height: $height;  .item {    width: $width;    height: $height;  }  @at-root .box {    width: $width;    height: $height;  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.section {width: 100px;height: 200px;}.section .item {width: 100px;height: 200px;}.box {width: 100px;height: 200px;}

์ค‘์ฒฉ๋œ ์†์„ฑ#

font- , margin- ๋“ฑ ๋™์ผํ•œ ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€๋Š” ์†์„ฑ๋“ค(๊ฐ™์€ ๋‹จ์ถ• ์†์„ฑ ์•„๋ž˜์— ์žˆ๋Š” ๊ฐœ๋ณ„ ์†์„ฑ๋“ค)์„ ์‚ฌ์šฉํ•  ๋•Œ ์ค‘๋ณต์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

.box {  font: {    weight: bold;    size: 10px;    family: sans-serif;  };  margin: {    top: 10px;    left: 20px;  };  padding: {    bottom: 40px;    right: 30px;  };}

โ˜๏ธ (์†์„ฑ์˜ ์ ‘๋‘์‚ฌ): {}

์ปดํŒŒ์ผํ•˜๋ฉด

.box {font-weight: bold;font-size: 10px;font-family: sans-serif;margin-top: 10px;margin-left: 20px;padding-bottom: 40px;padding-right: 30px;}

Reference#

ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong

Sass(SCSS) ์™„์ „ ์ •๋ณต!