2 Ways to Center an Absolute Positioned Element in TailwindCSS

In TailwindCSS, centering an element may be a challenge for beginners if they don’t know the core concepts of CSS. You can center a div element easily using margin or flex-box in TailwindCSS but if you have an element that is positioned absolute on the page, this will not work. Here are 2 methods to center an absolute positioned element in TailwindCSS.

Method 1: Horizontally Centering an Absolute Positioned Element

You can make use of the margin and left , right positioning to center an absolute positioned element on the page.  In this method, you have to set the margin to auto and then left , right to 0. Here is the example below.

Here is the working code and you can check the output in the following screenshot.

center an absolute positioned element in tailwindcss

Method 2: Horizontally and Vertically Centering an Absolutely Positioned Element

The above method will only center the element horizontally but will not do for vertical position. So, if you want to center the absolute positioned element both vertically and horizontally, you need some additional classes. Here is an example that uses the translate properties to center an absolute element both vertically and horizontally.

You can check the working example on the Tailwind playground.


You can center an absolutely positioned element using TailwindCSS by using 2 methods. In the first method, you can use of margin , left and right classes to center the element horizontally. The second method is useful when you want to center the element in both the x and y directions. For the second method, you have to use the translate properties in TailwindCSS.


Latest articles

Related articles