tailwind.css
tailwind.config.js 파일에 옵션들을 작성해두면 다른 컴포넌트에서도 해당 변수를 사용해서 적용시킬 수 있다. theme
에 추가하면 된다.
👩🏻💻 예시 코드
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./index.html", "./src/**/*.{js,jsx,ts,tsx}'],
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',
}
}
},
}
🔍 theme 내용추가!
fontFamily: {
sans: ['Graphik', 'sans-serif'],
serif: ['Merriweather', 'serif'],
somyung: ['Somyung', 'sans-serif'], // 소명 폰트를 저장
},
예를 들어 ‘소명’ 폰트를 다운 받아서 적용하고 싶다면 이렇게 fontFamily
에 저장해두고
<div>
<h1 className="font-somyung">Tailwind CSS</h1>
</div>;
적용하고 싶은 부분에 font-
이후에 적용시킬 폰트를 적어주면 된다!
config.js
에서 적은 ‘somyung’
이 변수처럼 사용된다고 이해하면 쉽다.
colors: {
primary: "#EE8A2C", // 메인색상
secondary: { //서브 색상
100: "#FFDAB6"
}
},
//컴포넌트 코드에 적용
{*메인색상 적용*}
<div className="bg-primary flex">
<form>
<input/>
{*서브색상 적용*}
<button className="bg-secondary-100">검색</button>
</form>
</div>
tailwind는 지원하는 고정된 px 값이나 색상이 있는데 고정된 값 말고 원하는 단위로 적용시키고 싶다면 아래 코드처럼 [] 대괄호
로 작성하면 된다.
<div className="w-[100px]">
<h1>Tailwind CSS</h1>
</div>;
여러 컴포넌트에서 같은 스타일이 사용되는 경우가 높은 button
, input
같은 건 중복을 줄이고 효율을 높이기 위해 사용자 정의 css를 추가하여 재사용하면 좋다.
👩🏻💻 설정방법
/* main.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
.btn {
background-color: red;
color: #fff;
padding: 10px;
}
👩🏻💻 적용코드
<button className="btn" type="button">button</button>
@layer
문을 사용해서 base, components, utilities 레이어에 스타일 추가도 가능하다.
👩🏻💻 설정방법
/* main.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.btn {
background-color: red;
color: #fff;
padding: 10px;
}
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
h1 {
@apply text-red-950;
}
h2 {
color: red;
}
}
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.select2-dropdown {
@apply rounded-b-lg shadow-md;
}
.select2-search {
@apply rounded border border-gray-300;
}
.select2-results__group {
@apply text-lg font-bold text-gray-900;
}
}
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer utilities {
.content-auto {
content-visibility: auto;
}
}
👩🏻💻 설정방법
/* main.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components{
.btn {
/* ⭐️ 이미 작성해둔 긴! 속성이 다른데서도 쓰이게 된다면? 혹은 깔끔하게 하고 싶으면?! 이렇게 적고! */
@apply px-6 py-3 bg-blue-500 text-white rounded-md
}
.practice {
width: 300px;
height: 100px;
border: ~~~
}
}
👩🏻💻 적용코드
<button className="btn" type="button">button</button>
⭐️ 아까 만든 클래스 명을 적어주면 그대로 적용!
출처