๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

TIL

CSS Modules ํด๋ž˜์Šค๋ช… ๋„ฃ๋Š” ๋ฒ•

Today I learned ์นดํ…Œ๊ณ ๋ฆฌ์— ์žˆ๊ธด ํ•˜์ง€๋งŒ Today์— ๋ฐฐ์šด๊ฑด ์•„๋‹ˆ๊ณ ... Next.js ๊ด€๋ จ ํŠœํ† ๋ฆฌ์–ผ๋ณด๋‹ค๊ฐ€ ์˜ˆ์ „์— ๊ณ ๋ฏผํ•˜๊ณ  ์ฐพ์•„๋ดค๋˜ ๋ถ€๋ถ„์ด ์ƒ๊ฐ๋‚˜์„œ ์–ผ๋ฅธ ๊ธ€๋กœ ๋‚จ๊ฒจ๋ณธ๋‹ค. 

์ง€๋‚œ ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ ๋•Œ CSS Modules๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ํด๋ž˜์Šค ๋ช…์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋„ฃ๋Š” ๊ฒƒ๊ณผ ๊ด€๋ จํ•˜์—ฌ ์ฐพ์•„๋ณธ ์ ์ด ์žˆ์—ˆ๋‹ค. (๊ธฐ์กด์— ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ์—์„œ ๋ณด๊ณ  ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋” ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” ๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๊ฑฐ๋‚˜ ์ถ”๊ฐ€์ ์œผ๋กœ ๊ฒ€์ƒ‰ํ•ด๋ณด๋Š” ๊ฒƒ์„ ๊ถŒํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค. )

1. ํด๋ž˜์Šค ๋ช…์ด ํ•˜๋‚˜ ์ผ ๋•Œ 

- style.className ๋ฐฉ์‹

<h2 className={cardStyles.cardTitle}>

- style['class-name'] ๋ฐฉ์‹

<h2 className={cardStyles['card-title']}>

 

2. ํด๋ž˜์Šค ๋ช…์ด ๋‘๊ฐœ ์ด์ƒ์ผ ๋•Œ

- {`${style.className1} ${style.className2}`}  /  {`${style['class-name1']} ${style['class-name2']}`}

// style.className ๋ฐฉ์‹
<span className={`${cardStyles.cardCategory} ${cardStyles.cardTag}`}>

// style['class-name'] ๋ฐฉ์‹
<span className={`${cardStyles["card-category"]} ${cardStyles["card-tag"]}`}>

// ์กฐ๊ฑด๋ฌธ๋„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
<span className={`${cardStyles.cardCategory} ${isClicked ? cardStyles.selected : ''}`}>

// ๋ฐฐ์—ด์— ๋„ฃ๊ณ  join ํ•˜๊ธฐ
<span className={[cardStyles.cardCategory, isClicked ? cardStyles.selected : ''].join('')}>

 

3. globals.css์™€ CSS Modules๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ

- ``(๋ฐฑํ‹ฑ) ์•ˆ์— ํ…์ŠคํŠธ๋กœ ๋„ฃ๊ธฐ

<div className={`body-text ${alertStyles.alert}`}>

- ๋ณ€์ˆ˜๋กœ ๋„ฃ๊ธฐ

const btnClass = `${btnStyles.btn} mt-16 ml-8`
// ์ฝ”๋“œ ์ƒ๋žต
<button className={`${btnClass} ${btnStyles['btn-default']}`}>

'TIL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์•„๋Š” ๊ฒƒ์ด๋ž€  (0) 2022.06.15
๋žœ๋ค ๋ฌธ์ž์—ด ์ƒ์„ฑํ•˜๊ธฐ  (0) 2022.05.24
npx prisma format  (0) 2022.05.08
MySQL ๋คํ”„ ๋œจ๊ธฐ  (0) 2021.10.22
MySQL ์ปฌ๋Ÿผ ์ถ”๊ฐ€ํ•˜๊ธฐ  (0) 2021.10.07