TailwindCSS Practice Project
建議不要使用CDN, 原因:
https://tailwindcss.com/docs/installation
Tailwind CSS IntelliSense
/* 組合utility 可簡化class裡的utility ,也可變成一個可重複利用的class*/
.btn {
@apply font-bold py-2 rounded-lg mx-auto flex bg-white;
}
<div class="font-bold py-2 rounded-lg mx-auto flex bg-white"></div>
<!-- 就變成 -->
<div class="btn"></div>
https://tailwindcss.com/docs/responsive-design
https://tailwindcss.com/docs/adding-custom-styles
module.exports = {
theme: {
screens: {
'tablet': '640px',
// => @media (min-width: 640px) { ... }
'laptop': '1024px',
// => @media (min-width: 1024px) { ... }
'desktop': '1280px',
// => @media (min-width: 1280px) { ... }
},
}
}
@tailwind base;
@tailwind components;
@tailwind utilities;
/* 覆蓋 base 裡面的某個樣式;*/
@layer base {
h1 {
@apply text-2xl;
}
h2 {
@apply text-xl;
}
/* ... */
}
module.exports = {
// ...
plugins: [
plugin(function ({ addBase, addComponents, addUtilities, theme }) {
addBase({
'h1': {
fontSize: theme('fontSize.2xl'),
},
'h2': {
fontSize: theme('fontSize.xl'),
},
})
addComponents({
'.card': {
backgroundColor: theme('colors.white'),
borderRadius: theme('borderRadius.lg'),
padding: theme('spacing.6'),
boxShadow: theme('boxShadow.xl'),
}
})
addUtilities({
'.content-auto': {
contentVisibility: 'auto',
}
})
})
]
}
https://tailwindcss.com/docs/hover-focus-and-other-states
<button class="bg-sky-600 hover:bg-sky-700 ...">
Save changes
</button>
/* rwd + 偽元素*/
<button class="dark:md:hover:bg-fuchsia-600 ...">
Save changes
</button>
<!-- 外層放一個 group -->
<!-- 內層可以使用group-hover 做整體效果-->
<a href="#" class="group block max-w-xs mx-auto rounded-lg p-6 bg-white ring-1 ring-slate-900/5 shadow-lg space-y-3 hover:bg-sky-500 hover:ring-sky-500">
<div class="flex items-center space-x-3">
<svg class="h-6 w-6 stroke-sky-500 group-hover:stroke-white" fill="none" viewBox="0 0 24 24"><!-- ... --></svg>
<h3 class="text-slate-900 group-hover:text-white text-sm font-semibold">New project</h3>
</div>
<p class="text-slate-500 group-hover:text-white text-sm">Create a new project from a variety of starting templates.</p>
</a>
https://tailwindcss.com/docs/configuration
npx tailwindcss init --full
/* 先寫基本樣式結構 再加上多種class樣式 */
@layer components {
.btn-blue {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}
}
@layer utilities {
.filter-none {
filter: none;
}
.filter-grayscale {
filter: grayscale(100%);
}
}
https://github.com/aniftyco/awesome-tailwindcss
https://tailwindcss.com/docs/functions-and-directives
因為都把樣式寫到class 所以class會變非常多, 雖然沒辦法減少class 但是可以增加可讀性:
https://tailwindcss.com/docs/guides/create-react-app