Tailwind CSS Basic Usages
July 8, 2024[Tailwind CSS Quick Guide]
In the last post Get Started with Tailwind CSS, we have set up our Tailwind CSS development environment, and we also create a Hero Image page. Now, let me explain the details how it works. You can also refer to the offical documentations: https://tailwindcss.com/docs/.
Here’s the codes for Hero Image page:
|
|
Background Color
The class bg-gray-50
(line 8) sets the background color of the body to gray rgb(250 250 249)
. Other available values: bg-red-50, bg-red-100, bg-red-200,…, bg-red-900, bg-red-950 etc.
Centered Section
The class container
(line 9) sets the max-width
of an element to match the min-width
of the current breakpoint.
E.g., the width of the screen is 1000px, the pre-defined breakpoints of md
and lg
screens are 768px and 1024px, the max-width
of the section
would be 768px, instead of the full width of 1000px.
The class mx-auto
(line 9) sets horizontal margins to auto
. The effect of the combined classes container
and mx-auo
is to have a centered section window.
Margins
There are other classes used to set margins:
mx-4
: set horizontal margin to 16px or 1remmy-6
: set vertical margin to 24px or 1.5remml-5
: set left marginmr-5
: set right marginmt-3
: set top marginmb-2
: set bottom margin
Paddings
Similar with the margin settings, we also have kinds of paddings like: px-3
, py-2
, pl-1
, pr-3
, pt-5
and pb-2
.
Set Width / Height
The class w-full
sets the width of an element to full width of the container.
The similar usages:
w-12
: set width to 48px or 3remh-5
: set height to 20px or 1.24remw-[100px]
: set width to custom value 100pxmax-w-96
: setmax-width
to 384px or 24remmin-w-48
: setmin-width
to 192px or 12rem
Flex Items
Use flex flex-col items-center
classes (line 10) to position the flex items vertically and centered horizontally.
To create a horizontal navigation bar, we can use flex
classes as well:
<nav class="flex items-center bg-teal-500">
<a href="#" class="flex-none inline-block px-3 py-1 ml-1 text-white">Home</a>
<a href="#" class="flex-none inline-block px-3 py-1 ml-1 text-white">About</a>
<a href="#" class="grow inline-block px-3 py-1 ml-1 text-white">Projects</a>
<a href="#" class="flex-none inline-block px-3 py-1 mr-3 text-white font-semibold">Download</a>
</nav>
By default, the flex items are positioned horizontally. It is different from the above example, items-center
here centers the items vertically.
flex-none
set the items no grow nor sink, while grow
lets the item grow to occupy the remain place so that Download
link is pushed to the right end.
Preview:
Text Classes
text-center
(line 12): setstext-align
tocenter
.
Other available values:text-left
,text-right
, andtext-justify
etc.text-3xl
(line 13): setsfont-size
to 30px and setsline-height
to 36pxfont-bold
(line 13): setsfont-weight
to 700
Other available values:font-thin
,font-light
,font-normal
, andfont-semibold
etc.text-gray-900
(line 14): setscolor
to gray
Border
border
setsborder-width
to 1pxborder-0
: remove the borderborder-x-2
: setsborder-left-width
andborder-right-width
to 2pxborder-y
: setsborder-top-width
andborder-bottom-width
to 1pxborder-t
: sets `border-top-width to 1pxborder-b
: sets `border-bottom-width to 1pxrounded-md
(line 15): setsborder-radius
to 6pxborder-gray-900
(line 15): setsborder-color