wam
w__am 개발노트
wam
  • 분류 전체보기 (165)
    • CS 지식 (10)
      • 자료구조 (0)
      • 알고리즘 (0)
      • 컴퓨터 구조 (0)
      • 운영체제 (0)
      • 네트워크 (7)
      • 데이터베이스 (0)
      • 디자인 패턴 (3)
    • Frontend (131)
      • Three.js (64)
      • NPM (1)
      • Nest.js (19)
      • React (10)
      • Apollo (7)
      • TypeScript (2)
      • JavaScript (12)
      • HTML, CSS (1)
      • Jest (3)
      • E2E (5)
      • Cypress (7)
    • Database (12)
      • TypeORM (12)
    • IT 지식 (8)
      • 클라우드 서비스 (3)
      • 네트워크 (1)
      • 데이터 포맷 (2)
      • 기타 (2)
    • IT Book (2)
    • 유용한 사이트 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 🐱 Github

인기 글

태그

  • API
  • reactive variables
  • threejs 개발 할 때 도움을 줄 수 있는 유틸리티
  • 초기 환경설정
  • 함수 리터럴
  • 함수 선언문
  • joi 에러
  • 함수의 범위
  • gridhelper
  • 렌더링 성능 최적화
  • Decorators
  • e.preventdefault()
  • 원형적인 움직임
  • 오프-프레미스(off-premise) 방식
  • 데이터 포맷
  • getelapsedtime()
  • math.cos()
  • mapped types
  • 스코프
  • isabstract
  • 함수 표현식
  • Interface
  • math.sin()
  • 삼각함수
  • getdelta()
  • type-graphql
  • axeshelper
  • three.js 구성 요소
  • 디자인 패턴
  • react 성능 최적화

최근 글

관리자

글쓰기 / 스킨편집 / 관리자페이지
hELLO · Designed By 정상우.
wam

w__am 개발노트

TailwindCSS Setup
Frontend/HTML, CSS

TailwindCSS Setup

2024. 9. 4. 20:43

 

Tailwind CSS

  • Tailwind CSS는 utility-first framework이다.
  • 미리 정의된 CSS 클래스를 사용해 스타일을 적용할 수 있게 해 준다.
  • 클래스를 이용해 HTML에 조합해서 사용할 수 있다.

 

 

설치: Tailwind CSS

https://tailwindcss.com/docs/installation

npm install -D tailwindcss
  • node 버전 12 이상이어야 한다.

 

 

VSCode extensions 설치

Tailwind CSS intellSense : 클래스 이름 자동완성 기능

 

 

필요 없는 파일 제거

  • App.css
  • index.css
  • logo.svg

 

 

코드 초기화

// App.tsx

function App() {
  return <div>HELLO</div>;
}

export default App;

 

// index.tsx

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  
    
  
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: <https://bit.ly/CRA-vitals>
reportWebVitals();

 

 

postcss 설치 및 config 파일 생성

CSS를 post process 할 수 있게 해주는 라이브러리이다.

https://tailwindcss.com/docs/installation/using-postcss

 

Install Tailwind CSS using PostCSS - Tailwind CSS

Installing Tailwind CSS as a PostCSS plugin is the most seamless way to integrate it with build tools like webpack, Rollup, Vite, and Parcel.

tailwindcss.com

 

 

설치 : postcss

npm install -D tailwindcss postcss autoprefixer

autoprefixer는 postcss의 또 다른 플러그인이며 클래스 이름에 접두사 호환성을 추가해주는 것

예시로 border-radius, 파이어폭스에 호환되기 위해 사용하는 -moz-border-radius 같은 것이다.

 

 

postcss config 파일 생성

// postcss.config.js

module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {}
  },
  plugins: []
};

tailwind를 일반 CSS파일로 빌드하기 위해 postcss config 파일이 필요하다.

 

 

tailwind.config 파일 생성

// tailwind.config.js

module.exports = {
  content: ["./src/**/*.{js,jsx,ts,tsx,html}", "./public/index.html"],
  theme: {
    extend: {}
  },
  plugins: []
};

tailwind를 커스터마이즈 하기위해 tailwind config 파일이 필요하다.

클래스 이름을 자동완성 해주는 VSCode 확장인 Tailwind CSS intellSense는 tailwind.config.js 파일을 자동으로 찾는다.

 

 

CSS include 하기

// src/tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

파일 이름은 임의로 정해도 된다.

https://tailwindcss.com/docs/installation/using-postcss

  • post CSS가 이 파일을 보고 코드 세 줄을 Tailwind가 갖고 있는 모든 클래스 이름으로 바꾼다.
  • tailwind config 파일을 들여다 보고 새 클래스 이름이 있다면 그것도 추가해준다.

 

 

post CSS를 통해 tailwind.css 파일 변형

// src/styles/tailwind.css

@tailwind base;
@tailwind components;

.btn {
  @apply px-4 py-2 bg-blue-600 text-white rounded;
}

@tailwind utilities;

 

 

tailwind CSS 파일 빌드

// 예제
npx tailwindcss -i ./src/input.css -o ./src/output.css --watch

 

 

// src/styles/tailwind.css

"scripts": {
    "tailwind:build": "tailwind build ㅊ ./src/styles/tailwind.css -o ./src/styles/styles.css",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  • INPUT : -i ./src/tailwind.css
  • OUPUT : -o ./src/styles/styles.css

 

 

실행

npm run tailwind:build

 

 

빌드된 css를 import 하기

// index.tsx

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import "./styles/styles.css";

const root = ReactDOM.createRoot(
  document.getElementById("root") as HTMLElement
);
root.render(
  
    
  
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: <https://bit.ly/CRA-vitals>
reportWebVitals();

 

 

react application을 start 하는 방식 변경

// package.json

"scripts": {
  "tailwind:build": "tailwind build -i ./src/styles/tailwind.css -o ./src/styles/styles.css",
  "start": "npm run tailwind:build & react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
},

start 할 때 먼저 tailwind:build를 실행시키고 그다음 start를 실행하기

 

 

스티일 지우기

// src/app.tsx

function App() {
  return (
    <div className="bg-orange-100">
      <h1 className="btn">HELLO</h1>
    </div>
  );
}

export default App;

 

 

// src/tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

 

 

실행

npm run start

btn 스타일이 작동하지 않으면 잘된 것

 

 

저작자표시 변경금지 (새창열림)
    wam
    wam

    티스토리툴바