[Tailwind]More about Tailwind

ยท

3 min read

๐Ÿ’ก
์ด ๊ธ€์€ Tailwind์— ๋Œ€ํ•œ ๊ธฐ์ดˆ์ง€์‹์„ ์ „์ œ๋กœ ์“ฐ์ธ ๊ธ€์ž…๋‹ˆ๋‹ค.

TailwindCSS๋Š” html/xml ๊ตญ๋ฉด์—์„œ style๋ฅผ ์ผ๊ด„์ ์œผ๋กœ ํŠธ๋ž˜ํ‚นํ• ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ณ , ๋ณด๋‹ค ๊ฐ„ํŽธํ•œ ๋ฐ˜์‘ํ˜• ์ œ์ž‘, ํ”„๋ฆฌ์…‹ ์ œ๊ณต ๋“ฑ ์—ฌ๋Ÿฌ ์ด์ ์ด ์žˆ์ง€๋งŒ, ํ”„๋กœ์ ํŠธ์˜ ๋ณต์žก๋„๊ฐ€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ํ”„๋ ˆ์ž„์›Œํฌ์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ ์˜๋ฌธ ๋˜๋Š” ํ•œ๊ณ„๋ฅผ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋‹ค. ๋ณธ๋ฌธ์—์„œ๋Š” ํ•„์ž๊ฐ€ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๊ณต๋ถ€ํ•œ ๋ช‡ ๊ฐ€์ง€ Tailwind ์ƒ์‹๊ณผ ์ฃผ์˜์ ์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

๋™์  className

  • Tailwind className ์ผ๋ถ€๋ฅผ ์น˜ํ™˜ํ•˜๋ฉด ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.
const len = 10;
<div className='w-screen h-screen relative'> //Container
    <div className={`w-10 h-${len}`}></div> //box
</div>
//not working

๊ณต์‹๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด, ๋™์  ํด๋ž˜์Šค๋ช…์€ ์™„์ „ํ•œ ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

-> className ์ „์ฒด๋ฅผ ์น˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋œ๋‹ค.

const height = 'h-10';
<div className='w-screen h-screen relative'> //Container
    <div className={`w-10 ${height}`}></div> //box
</div>

-> template literal๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑํ•œ ๋™์  ํด๋ž˜์Šค๋ช…์€ arbitrary value์˜ ํŠน์ • ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ์ž‘๋™ํ•œ๋‹ค.

  • Preset value ํ…Œ์ŠคํŠธ
const color = 'white';
const colorCN0 = `bg-white`;
const colorCN1 = 'bg-'+color; //string concatenation
const colorCN2 = `bg-${color}`; //template literal

<div className=" ... ">   
   <div className={`w-10 h-10 bg-${color} absolute top-10 `}></div>
   //working
   <div className={`w-10 h-10 bg-${color} absolute top-20 `}></div>
   //working
   <div className={`w-10 h-10 ${colorCN0} absolute top-30 `}></div>
   //working
   <div className={`w-10 h-10 ${colorCN1} absolute top-40 `}></div>
   //not working
   <div className={`w-10 h-10 ${colorCN2} absolute top-50 `}></div>
   //working
</div>

result

  • Arbitrary value ํ…Œ์ŠคํŠธ
const color = '#888888';
const colorCN0 = `bg-[#888888]`;
const colorCN1 = `bg-[${color}]`;
const colorCN2 = `bg-${`[${color}]`}`;

<div className=" ...">
   <div className={`w-10 h-10 bg-[${color}] absolute top-10 `}></div>
    //working
   <div className={`w-10 h-10 bg-${`[${color}]`} absolute top-20 `}></div>
    //working
   <div className={`w-10 h-10 ${colorCN0} absolute top-30 `}></div>
    //working
   <div className={`w-10 h-10 ${colorCN1} absolute top-40 `}></div>
    //not working
   <div className={`w-10 h-10 ${colorCN2} absolute top-50 `}></div>
    //working
</div>

result

๊ฒฐ๋ก 

ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋ช…์„ string concatenation์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์—†์Œ์„ ์ฃผ์˜ํ•˜์ž.


className concatenation

  • ์„œ๋กœ ๋‹ค๋ฅธ ์„ฑ๊ฒฉ์˜ ํด๋ž˜์Šค๋ช…์€ ๋”ฐ๋กœ ์„ ์–ธํ•ด ๋ชจ๋“ˆํ™”ํ•˜๋ฉด ์šฉ์ดํ•˜๋‹ค.
const size = 'w-10 h-10 ';
const color = 'bg-white ';
const position = 'absolute top-10 left-10';

<div className={`${size+color+position}`}></div>

์ฃผ์˜) ๊ฐ string๋งˆ๋‹ค ๋งˆ์ง€๋ง‰์— ๊ผญ white space๋ฅผ ๋‚จ๊ฒจ๋‘์–ด์•ผ ๊ฐ๊ฐ์˜ className์„ ์ธ์‹ํ•œ๋‹ค.


CSS module + Tailwind

TailwindCSS๊ฐ€ ๊ฐ„ํŽธํ•œ ๊ฒƒ์€ ์‚ฌ์‹ค์ด์ง€๋งŒ, ์• ๋‹ˆ๋ฉ”์ด์…˜, ์ปดํฌ๋„ŒํŠธ ๊ฐ„ ์ƒํ˜ธ์ž‘์šฉ ๋“ฑ์—์„œ๋Š” ๊ธฐ์กด CSS๋ณด๋‹ค ๋ฒˆ๊ฑฐ๋กญ๊ณ  ๊ตฌํ˜„์ด ์–ด๋ ค์›Œ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ๊ธฐ์กด ์‚ฌ์šฉํ•˜๋˜ CSS module๊ณผ ํ˜ผ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

import styles from './a.module.css';

<div className={`${styles.glow} w-10 h-10 absolute top-12 left-12 bg-white`}></div>
/* a.module.css */
.glow{
    animation: glow_kf 1s ease-in-out infinite;
}

@keyframes glow_kf{
    0%{
        opacity: 1;
    }
    50%{
        opacity: 0;
    }
    100%{
        opacity: 1;
    }
}

์ฃผ์„ ์ฒ˜๋ฆฌ

Tailwind๋Š” ๊ธฐ์กด CSS์™€ ๋‹ฌ๋ฆฌ ์ค„๋ฐ”๊ฟˆ์ด ์—†์–ด์„œ attribute๋ณ„๋กœ ์ฃผ์„์ฒ˜๋ฆฌ ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค.

๊ทธ๋Ÿด ๋• ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์ชผ๊ฐœ์ง„ className์„ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ์ค‘๊ฐ„์— space ํ•œ๊ฐœ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

const ThumbNail = ({item}:{item: albumProps}) =>{
    const {thumbnail,len,url} = item;
    return <>

    <Image src={thumbnail[0]} alt='pic' className='object-fill rounded-md relative z-11' />
    {len >=2 && <Image src={thumbnail[1]} alt='anotherpic' 
    className="object-fill r ounded-md a bsolute left-0 top-0 z-10 rotate-6"/>}
    </> // rounded_md์™€ absolute๋ฅผ ์ฃผ์„ํ•œ ์ƒํƒœ
}

์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„

Tailwind๋Š” ํด๋ž˜์Šค๋ช…์— ์ปดํฌ๋„ŒํŠธ์˜ ๋ช…์‹œ์ ์ธ ์ด๋ฆ„์„ ์ ์ง€ ์•Š์•„์„œ ์–ด๋–ค ์—ญํ• ์ธ์ง€ ์•Œ๊ธฐ ์–ด๋ ค์šธ ๋•Œ๊ฐ€ ์žˆ๋‹ค.

์ธ์‹์ด ๋˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค๋ช…์€ ์ปดํŒŒ์ผ ๊ณผ์ •์—์„œ ์ง€์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์—, ๋ณธ์ธ๋งŒ์˜ ๊ทœ์น™์„ ์ •ํ•ด ํด๋ž˜์Šค๋ช… ๋‚ด ์ด๋ฆ„ ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฉ”๋ชจ๋ฅผ ์ฒจ๋ถ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

<div className="(outer container) w-10 ...">
   <div className="(inner container) w-full ...">
      <div className="(block) w-full ...">
          <p className="(content) ..."></p>
      </div>
  </div>
</div>

์ž˜๋ชป๋œ ์ •๋ณด๋Š” ์ œ๋ณด๋ฐ”๋ž๋‹ˆ๋‹ค.

edited by ์ •์ •ํ™˜

ย