Align Self In Tailwind CSS

In this article, we will explore what Tailwind align self is, how it works, and how to use it effectively in your web development projects.

The align-self class in Tailwind CSS provides a quick and easy way to set the align-self property.

Tailwind align self aligns an individual flex item along the cross-axis (vertical axis) of a flex container.

Tailwind align-self lets you specify the vertical alignment of a flex item in conjunction with a flex container.



Tailwind Align Self: how does it work?

The align-self property can be assigned to any flex item, such as a div or an image.

This property determines how a flex item is aligned vertically within a flex container.

When a container is set to display: flex and flex-direction: row, the align-self property can be used to align flex items vertically.

Tailwind CSS supports the following align-self values:

  1. auto – This value allows the flex container to decide how the flex item should be aligned.
  2. start – Positions the flex item at the start of the cross-axis.
  3. End – Positions the flex item at the cross-axis end.
  4. Center – Aligns the flex item at the cross-axis center.
  5. baseline – Sets the flex item’s baseline to the flex container’s baseline.
  6. Stretch – Fills the container to the full height of the flex item.
Note: By default, align-self is set to auto, meaning the container decides the vertical alignment of flex items based on align-items.

Tailwind Align Self-auto

Align-auto inherits the align-items property of its parent container.

It inherits stretched as a default value if it does not have a parent container.

Syntax:

<element class="self-auto">...</element>

You can assign the align-self class to any flex item within a flex container by combining the align-self keyword with the desired value:

Example: 

<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-700 text-2xl font-semibold"> MrExamples </h1> <b>Align Self Class of Tailwind CSS</b> <div id="main" class="p-2 justify-evenly ml-0 h-32 w-full flex items-stretch bg-gray-200 border-solid border-2 border-gray-900 text-white"> <div class="bg-gray-900 rounded-lg w-16">a</div> <div class="self-auto bg-gray-800 rounded-lg w-16">b</div> <div class="bg-gray-700 rounded-lg w-16">c</div> </div> </body> </html>

Tailwind Align Self-start

When used with a flex container, align-self-start aligns a flex item to the top of the cross-axis.

Text, images, and other elements can be aligned to the top of a container when the default alignment is not preferred.

Syntax:

<element class="self-start">...</element>

Flex items can be aligned to the top of a container by adding the class align-self-start:

Example: 

<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-700 text-2xl font-semibold"> MrExamples </h1> <b>Align Self Class of Tailwind CSS</b> <div id="main" class="p-2 justify-evenly ml-0 h-32 w-full flex items-stretch bg-gray-200 border-solid border-2 border-gray-900 text-white"> <div class="bg-gray-900 rounded-lg w-16">a</div> <div class="self-start bg-gray-800 rounded-lg w-16">b</div> <div class="bg-gray-700 rounded-lg w-16">c</div> </div> </body> </html>

Tailwind Align Self-end

An align-self-end property aligns a flex item to the end of the cross-axis of a flex container.

Syntax:

<element class="self-end">...</element>

Flex items will appear in flex-direction of column at the bottom of the container, or on flex-direction of row at the right of the container:

Example: 

<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-700 text-2xl font-semibold"> MrExamples </h1> <b>Align Self Class of Tailwind CSS</b> <div id="main" class="p-2 justify-evenly ml-0 h-32 w-full flex items-stretch bg-gray-200 border-solid border-2 border-gray-900 text-white"> <div class="bg-gray-900 rounded-lg w-16">a</div> <div class="self-end bg-gray-800 rounded-lg w-16">b</div> <div class="bg-gray-700 rounded-lg w-16">c</div> </div> </body> </html>

Tailwind Align Self-center

The align-self-center property aligns a flex item vertically in the center of the container.

Within a container, you can center text, images, and other elements by using this feature.

Syntax:

<element class="self-center">...</element>

You can align flex items to the center of a container by adding the class align-self-center:

Example: 

<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-700 text-2xl font-semibold"> MrExamples </h1> <b>Align Self Class of Tailwind CSS</b> <div id="main" class="p-2 justify-evenly ml-0 h-32 w-full flex items-stretch bg-gray-200 border-solid border-2 border-gray-900 text-white"> <div class="bg-gray-900 rounded-lg w-16">a</div> <div class="self-center bg-gray-800 rounded-lg w-16">b</div> <div class="bg-gray-700 rounded-lg w-16">c</div> </div> </body></html>

Tailwind Align Self-baseline

By setting the align-self property to baseline, the align-self property of an item aligns with the baseline of a flex container.

Syntax:

<element class="self-baseline">...</element>

You can use this if you want to align items with different font sizes and line heights along a common baseline.

Example: 

<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-700 text-2xl font-semibold"> MrExamples </h1> <b>Align Self Class of Tailwind CSS</b> <div id="main" class="p-2 justify-evenly ml-0 h-32 w-full flex items-stretch bg-gray-200 border-solid border-2 border-gray-900 text-white"> <div class="bg-gray-900 rounded-lg w-16">a</div> <div class="self-baseline bg-gray-800 rounded-lg w-16">b</div> <div class="bg-gray-700 rounded-lg w-16">c</div> </div> </body> </html>

Tailwind Align Self-stretch

An align-self-stretch property stretches flex items across the cross-axis to their full height.

It is especially useful when HTML elements, such as images and form inputs, need to fill the whole container height.

Syntax:

<element class="self-stretch">...</element>

To stretch a flex item to the full height of the container, add the align-self-stretch class:

Example: 

<!DOCTYPE html> <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <h1 class="text-gray-700 text-2xl font-semibold"> MrExamples </h1> <b>Align Self Class of Tailwind CSS</b> <div id="main" class="p-2 justify-evenly ml-0 h-32 w-full flex items-stretch bg-gray-200 border-solid border-2 border-gray-900 text-white"> <div class="bg-gray-900 rounded-lg w-16">a</div> <div class="self-stretch bg-gray-800 rounded-lg w-16">b</div> <div class="bg-gray-700 rounded-lg w-16">c</div> </div> </body> </html>
This class can be used along with other flexbox utility classes in Tailwind, such as flex, flex-col, items-center, and justify-center.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *