2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

tailwindのflexの使い方について

Last updated at Posted at 2023-07-14

はじめに

tailwindのflexの使い方についてまとめました。

flexを指定しない場合

div要素が縦積みされます。

index.html
<div class="">
  <div class="bg-cyan-300">Item1</div>
  <div class="bg-gray-500">Item2</div>
  <div class="bg-lime-400">Item3</div>
  <div class="bg-purple-300">Item4</div>
</div>

image.png

flexを指定した場合

div要素が横並びになりました。

index.html
<div class="flex">
  <div class="bg-cyan-300">Item1</div>
  <div class="bg-gray-500">Item2</div>
  <div class="bg-lime-400">Item3</div>
  <div class="bg-purple-300">Item4</div>
</div>

image.png

子要素にflex-autoを指定した場合

Item1のみ、親要素の幅いっぱいに広がります。

index.html
<div class="flex">
  <div class="bg-cyan-300 flex-auto">Item1</div>
  <div class="bg-gray-500">Item2</div>
  <div class="bg-lime-400">Item3</div>
  <div class="bg-purple-300">Item4</div>
</div>

image.png

子要素にw-fullを指定した場合

Item3が、親要素の幅いっぱいに広がります。

index.html
<div class="flex">
  <div class="bg-cyan-300 flex-auto">Item1</div>
  <div class="bg-gray-500">Item2</div>
  <div class="bg-lime-400 w-full">Item3</div>
  <div class="bg-purple-300">Item4</div>
</div>

image.png

親要素にflex-wrapを指定した場合

Item3は幅いっぱいなので2行目にズレます。Item1は親要素いっぱいまで広がります。

index.html
<div class="flex flex-wrap">
  <div class="bg-cyan-300 flex-auto">Item1</div>
  <div class="bg-gray-500">Item2</div>
  <div class="bg-lime-400 w-full">Item3</div>
  <div class="bg-purple-300">Item4</div>
</div>

image.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?