Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Tailwindcssを使ってCSSを恐れず書こう
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
rhistoba
November 30, 2019
570
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Tailwindcssを使ってCSSを恐れず書こう
rhistoba
November 30, 2019
More Decks by rhistoba
See All by rhistoba
GUIでやる機械学習 Azure Machine Learning Studioの紹介
rhistoba
0
37
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
698
190k
Navigating Team Friction
lara
192
16k
What's in a price? How to price your products and services
michaelherold
247
13k
Deep Space Network (abreviated)
tonyrice
0
290
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
290
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
AI: The stuff that nobody shows you
jnunemaker
PRO
9
970
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Code Reviewing Like a Champion
maltzj
528
40k
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Transcript
Tailwindcssを使って CSSを恐れず書こう @rhistoba
About 私 鳥羽 隼司(@rhistoba) belongs 株式会社リゾーム 普段使う技術: Ruby on Rails
好きな技術: Vue.js Tailwindcss ゲーム好き、最近やりたいゲームが多すぎる 2
CSSの困難 Web開発では欠かすことのできないCSSだが、困難が付き物 • 名前衝突しないようにするのが辛い • 値指定の難しさ • CSSフレームワーク難しい 3
名前衝突しないようするのが辛い <button class=”btn”> ここは変えたくない </button> <article> <button class=”btn”> ここを変えたい </button>
</article> .btn-custom { ... } /* もしくは */ article .btn { ... } 既存のクラスと名前衝突しないように、 気をつけて名前空間を作るのが辛い 4
値指定の難しさ A A ちょうどいいグレーとはどれか? 文字の大きさは何が適切? 文字サイズに規則性はあるか? .title-sub { font-size: ?;
/* 12ptの見出しと24ptの見出しがあるなら? */ color: ?; /* ちょうどいいグレーにしたかったら? */ } 5
CSSフレームワーク難しい .btn { display: inline-block; padding: 6px 12px; margin-bottom: 0;
font-size: 14px; font-weight: 400; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; -ms-touch-action: manipulation; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-image: none; border: 1px solid transparent; border-radius: 4px; } Bootstrapの.btn 6
CSSを恐れずに書くには Tailwindcssを使おう - https://tailwindcss.com • Utility FirstなローレベルのCSSフレームワーク • .btnや.cardのようなコンポーネントクラスは存在しない •
あるのは.font-boldや.float-rightといった役割のはっきりしたユーティリ ティークラスのみ 7
Tailwindcssのクラスの例 8 .bg-gray-500 { background-color: #a0aec0; } .text-base { font-size:
1rem; } .text-right { text-align: right; } .border { border-width: 1px; } .rounded { border-radius: .25rem; }
Tailwindcssで書くとこうなる 9 <button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded select-none">
Button </button> <button class="btn btn-primary"> Button </button> Bootstrap Tailwindcss
カスタムしてみる 10 <button class="btn btn-primary btn-custom"> Button </button> .btn-custom {
font-size: 1.5rem; font-weight: 100; border-radius: 0; } <button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 select-none font-hairline text-2xl rounded-none">Button </button> Bootstrap Tailwindcss
スタイルの使い回し方 • コンポーネントクラスを定義する ◦ TailwindcssのクラスがCSS定義に使える • React、Vue.jsなどはコンポーネントとして分離すればよい 11 .btn-custom {
@apply text-2xl font-hairline rounded-none; } <template> <button class="bg-blue-500 ...">{{text}}</button> </template>
Tailwindcssの特徴 Positive • HTMLを編集するだけでスタイルが変更できるようになった → CSSを書かなくていいので名前衝突・意図しない箇所の変更がない • 色、文字や余白の大きさなどを用意されたクラスから決められる → 自分で具体的な値を決めなくてよい
• HTMLのクラス群だけで具体的な見た目が把握できる → HTMLを見るだけでスタイルが明確的 12
Tailwindcssの特徴 Negative • 基本的にユーティリティークラスを並べて見た目を構成する → コンポーネントクラスが無いので自力で見た目を整える必要がある → 素のCSSの知識が必要 → 他のCSSフレームワークに比べて学習コストが大きい
• Bootstrapとか使う方が楽じゃね? 13
Tailwindcssと他のCSSフレームワークを使う Tailwindcssにコンポーネントクラスが無いなら、 他のCSSフレームワークのを使えばよい 14 module.exports = { important: true, //
important! を追加 prefix: 'tw-', // クラスのプレフィクス tw-を追加 } <button class="btn btn-primary tw-text-2xl tw-font-hairline tw-rounded-none">Button</button> 名前衝突の回避と優先順位の設定を tailwind.config.jsに追加 他CSSフレームワークのクラスを使いながら Tailwindcssのクラスが使える
まとめ 以上、CSSの救世主となるTailwindcssの紹介でした。 • CSSの管理がぐっと楽になる • HTMLを書くだけで見た目が変えられる • 他のCSSフレームワークと一緒に使える 15
16 <div class="font-serif bg-teal-200 h-screen flex justify-center items-center"> <span class="text-6xl
text-teal-800 font-thin tracking-widest"> Tailwindcssはいいぞ </span> </div>