โœ๏ธ What I Learned/TIL

[TIL] Tailwind CSS ์žฅ๋‹จ์ , ์—ฐ์Šต์˜ˆ์ œ

Jiwon() 2023. 8. 19. 20:55

๐Ÿ’ก TIL 20230819 - Tailwind CSS ์žฅ๋‹จ์ , ์—ฐ์Šต์˜ˆ์ œ


1. Tailwind CSS๋ž€

Tailwind CSS๋Š” utility-first ์ปจ์…‰์„ ๊ฐ€์ง„ CSS ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

  • ๋ถ€ํŠธ์ŠคํŠธ๋žฉ๊ณผ ๋น„์Šทํ•˜๊ฒŒ m-1(.m-1 {margin: 0.25rem}), flex(.flex {display: flex})์™€ ๊ฐ™์ด ๋ฏธ๋ฆฌ ์„ธํŒ…๋œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ HTML ์ฝ”๋“œ ๋‚ด์—์„œ ์Šคํƒ€์ผ๋ง์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • tailwind๋Š” ์ œ๋กœ ๋Ÿฐํƒ€์ž„์œผ๋กœ ๋™์ž‘ํ•˜๋ฉฐ ๋น ๋ฅด๊ณ , ์œ ์—ฐํ•˜๋‹ค.

 

1) ์žฅ์ 

  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€ ๊ฐ™์€ ์ถ”์ƒํ™” ์ˆ˜์ค€์„ ์ œ๊ณตํ•ด ์Šคํƒ€์ผ์„ ๋น ๋ฅด๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์ฝ”๋“œ์˜ ๊ธธ์ด๊ฐ€ ์ค„๊ณ , css ์ž‘์—… ์‹œ๊ฐ„์ด ์ ˆ์•ฝ๋œ๋‹ค.
  • ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑ์ด ์‰ฝ๊ณ , ๊ด€๋ฆฌ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ํŽธํ•˜๋‹ค.
  • ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์ด ์‰ฌ์›Œ ์›ํ•˜๋Š” ๋Œ€๋กœ ๋””์ž์ธ์„ ๋ฐ”๊พธ๊ธฐ ์‰ฝ๊ณ , ๋ฐ˜์‘ํ˜• ๋””์ž์ธ ๊ตฌํ˜„๋„ ํŽธํ•˜๋‹ค.

 

2) ๋‹จ์ 

  • ์ฝ”๋“œ๊ฐ€ ๋ชป์ƒ๊ฒผ๋‹ค(?) - ์•„๋ž˜ ์ฝ”๋“œ ์ฐธ๊ณ 
<figure class="md:flex bg-slate-100 rounded-xl p-8 md:p-0 dark:bg-slate-800">
  <img class="w-24 h-24 md:w-48 md:h-auto md:rounded-none rounded-full mx-auto" src="/sarah-dayan.jpg" alt="" width="384" height="512">
  <div class="pt-6 md:p-8 text-center md:text-left space-y-4">
    <blockquote>
      <p class="text-lg font-medium">
        “Tailwind CSS is the only framework that I've seen scale
        on large teams. It’s easy to customize, adapts to any design,
        and the build size is tiny.”
      </p>
    </blockquote>
    <figcaption class="font-medium">
      <div class="text-sky-500 dark:text-sky-400">
        Sarah Dayan
      </div>
      <div class="text-slate-700 dark:text-slate-500">
        Staff Engineer, Algolia
      </div>
    </figcaption>
  </div>
</figure>

 

  • ๋Ÿฌ๋‹ ์ปค๋ธŒ : ์ด๋ฏธ ํด๋ž˜์Šค๋ช…์ด ์ •ํ•ด์ ธ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ฒ˜์Œ ์จ๋ณด๋Š” ์‚ฌ๋žŒ๋“ค์€ ํด๋ž˜์Šค๋ช…์— ์ต์ˆ™ํ•ด์ง€๋Š”๋ฐ ์‹œ๊ฐ„์ด ํ•„์š”ํ•˜๋‹ค. ํ•˜์ง€๋งŒ `Tailwind CSS IntelliSense`์™€ ๊ฐ™์€ VS code ์ต์Šคํ…์…˜์„ ์„ค์น˜ํ•˜๋ฉด ์ž๋™์™„์„ฑ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋А์ •๋„ ์ปค๋ฒ„๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. (๊ฐœ์ธ์ ์œผ๋กœ ์ด ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ ์—†์œผ๋ฉด tailwind css ๋ชป์“ธ๋“ฏ)

 

 


2. ์„ค์น˜ ๋ฐ ์‚ฌ์šฉ

1) ์„ค์น˜

npm install tailwindcss
npm tailwindcss init // ์„ค์ •ํŒŒ์ผ tailwind.config.ts ์ƒ์„ฑ

 

๋˜๋Š” Next.js ํ”„๋กœ์ ํŠธ ์„ธํŒ… ์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ„ฐ๋ฏธ๋„์—์„œ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ธฐ๋„ ํ•˜๋‹ค.

 

2) tailwind.config.ts

tailwind CSS Configuration

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./src/**/*.{html,js}'],
  theme: {
    colors: {
      'blue': '#1fb6ff',
      'purple': '#7e5bef',
      'pink': '#ff49db',
      'orange': '#ff7849',
      'green': '#13ce66',
      'yellow': '#ffc82c',
      'gray-dark': '#273444',
      'gray': '#8492a6',
      'gray-light': '#d3dce6',
    },
    fontFamily: {
      sans: ['Graphik', 'sans-serif'],
      serif: ['Merriweather', 'serif'],
    },
    extend: {
      spacing: {
        '8xl': '96rem',
        '9xl': '128rem',
      },
      borderRadius: {
        '4xl': '2rem',
      }
    }
  },
}

 

3) ์‚ฌ์šฉ ์˜ˆ์‹œ

์—ฐ์Šต ๊ฒธ ์˜ˆ์‹œ๋กœ ๋งŒ๋“ค์–ด ๋ณธ ๊ฒฐ๊ณผ๋ฌผ๊ณผ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

import profilePic from "@/assets/pic.jpg"
import chairPic from "@/assets/chair.webp"
import Image from "next/image";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faArrowLeft, faStar, faHeart } from "@fortawesome/free-solid-svg-icons";

const Home = () => {
  return (
    <div className="w-11/12 max-w-xl mx-auto py-20 px-10 flex flex-col space-y-10">
      <div className="bg-white p-8 rounded-3xl shadow-xl">
        <span className="font-semibold text-3xl">Select Item</span>
        <div className="flex justify-between my-2">
          <span className="text-gray-500">Grey Chair</span>
          <span className="font-semibold">$19</span>
        </div>
        <div className="flex justify-between">
          <span className="text-gray-500">Tooly Table</span>
          <span className="font-semibold">$80</span>
        </div>
        <div className="flex justify-between mt-2 pt-2 border-t-2 border-dashed">
          <span className="font-semibold">Total</span>
          <span className="font-semibold">$99</span>
        </div>
        <div className="flex justify-center">
          <button className="w-40 py-3 mt-5 bg-red-400 text-white p-2 rounded-lg shadow-lg hover:bg-red-300 hover:-translate-y-1 transition ease-in-out duration-200 active:bg-red-400">
            Checkout
          </button>
        </div>
      </div>
      <div className="bg-white overflow-hidden rounded-2xl shadow-xl">
        <div className="bg-blue-500 p-6 pb-14">
          <span className="text-white text-2xl font-medium">Profile</span>
        </div>
        <div className="rounded-3xl p-6 bg-white relative -top-5">
          <div className="flex relative -top-16 items-end justify-between">
            <div className="flex flex-col items-center">
              <span className="text-sm text-gray-400 font-medium">Orders</span>
              <span className="font-semibold">340</span>
            </div>
            <Image src={profilePic} alt="profile" className="w-24 h-24 rounded-full" />
            <div className="flex flex-col items-center">
              <span className="text-sm text-gray-400 font-medium">Spent</span>
              <span className="font-semibold">$2,310</span>
            </div>
          </div>
          <div className="relative flex flex-col items-center -mt-10">
            <span className="text-lg font-semibold">
              Jane Doe
            </span>
            <span className="text-sm text-gray-500">
              New York, USA
            </span>
          </div>
        </div>
      </div>
      <div className="bg-white p-10 rounded-2xl shadow-xl">
        <div className="flex mb-5 justify-between">
          <button><FontAwesomeIcon icon={faArrowLeft} /></button>
          <div className="space-x-3 flex items-center">
            <span className="font-semibold">
              <FontAwesomeIcon icon={faStar} className="text-yellow-400 mr-1" />
              4.9
            </span>
            <button>
              <FontAwesomeIcon icon={faHeart} className="text-red-500 shadow-xl p-2 rounded-md" />
            </button>
          </div>
        </div>
        <Image src={chairPic} alt="chair" />
        <div className="flex flex-col">
          <span className="font-semibold text-xl tracking-tight">Swoon Lounge</span>
          <span className="text-sm text-gray-500">Chair</span>
          <div className="mt-1 mb-5 flex justify-between items-center">
            <div className="space-x-2 flex">
              <div className="w-6 h-6 rounded-full bg-stone-300" />
              <div className="w-6 h-6 rounded-full bg-orange-300" />
              <div className="w-6 h-6 rounded-full bg-indigo-400" />
            </div>
            <div className="flex items-center space-x-5">
              <button className="rounded-lg bg-stone-300 active:bg-stone-400 flex justify-center items-center aspect-square w-10 text-xl text-gray-500">
                -
              </button>
              <span className="text-xl">1</span>
              <button className="rounded-lg bg-stone-300 active:bg-stone-400 flex justify-center items-center aspect-square w-10 text-xl text-gray-500">
                +
              </button>
            </div>
          </div>
          <div className="mt-5 flex justify-between items-center">
            <span className="text-2xl font-semibold">$450</span>
            <button className="w-40 bg-stone-500 text-white p-3 rounded-lg shadow-xl hover:bg-stone-600 hover:-translate-y-1 transition ease-in-out duration-200">Add to Cart</button>
          </div>
        </div>
      </div>
      {/* <div className="bg-white p-10 rounded-2xl shadow-xl"></div> */}
    </div>
  );
};

export default Home;

3. ๊ฐœ์ธ์ ์ธ ์†Œ๊ฐ

styled-components๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š”, ์ด๊ฒŒ h1์ธ์ง€ div์ธ์ง€ form์ธ์ง€ HTML ํƒœ๊ทธ๋“ค์„ sematicํ•˜๊ฒŒ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ต๋‹ค๊ณ  ๋А๊ผˆ์—ˆ๋Š”๋ฐ, tailwind css๋Š” HTML ํƒœ๊ทธ๋“ค์„ ํ•œ๋ˆˆ์— ๋ณผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹๋‹ค๊ณ  ๋А๊ปด์กŒ๋‹ค.

 

ํ•˜์ง€๋งŒ tailwind๋ฅผ ์ฒ˜์Œ ์ ‘ํ–ˆ์„ ๋•Œ ๋ฌดํ•œ์ฆ์‹ ํ•˜๋Š” ๋“ฏํ•œ className๋“ค์„ ๋ณด๊ณ  '์•„๋‹ˆ ์ด๊ฒŒ ๋ญ์•ผ...?' ์‹ถ๊ธด ํ–ˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์˜ˆ์ œ ์ •๋„๋งŒ ๋งŒ๋“ค์–ด๋ณด๋‹ˆ ์ต์ˆ™ํ•ด์ง€๋ฉด ๊ธˆ๋ฐฉ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ฌด์—‡๋ณด๋‹ค ์ฝ”๋“œ ์ž‘์„ฑํ•  ๋•Œ ์‹œ๊ฐ„์ด ๋‹จ์ถ•๋œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐ”๋กœ ์ฒด๊ฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๋ฌด์—‡๋ณด๋‹ค๋„ ๊ณต์‹ ๋ฌธ์„œ๊ฐ€ ์ž˜ ๋˜์–ด์žˆ์–ด ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด์„œ ๋” ์ต์ˆ™ํ•ด์ง€๋ฉด ์ž˜ ์“ธ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

 


์ฐธ๊ณ  ์‚ฌ์ดํŠธ

๋…ธ๋งˆ๋“œ ์ฝ”๋” - [ํ’€์Šคํƒ] ์บ๋Ÿฟ๋งˆ์ผ“ ํด๋ก ์ฝ”๋”ฉ

Hello Tailwind CSS! | ์žฅ์ , ๋‹จ์ , ์‚ฌ์šฉ๋ฒ•

tailwind css์˜ ์žฅ๋‹จ์ ๊ณผ ์‚ฌ์šฉ๋ฒ•