How to Center an Image using Tailwind CSS ? Last Updated : 20 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Here are the methods to center an image using Tailwind CSS:Method 1. Using Flexbox ClassesThis method centers the image both horizontally and vertically using Tailwind's flex, justify-center, and items-center classes. HTML <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="h-screen flex items-center justify-center"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20220221132017/download.png" alt="Centered Image" class="w-32 h-32"> </body> </html> The h-screen class sets the height of the body to 100% of the viewport height.The flex class makes the body a flex container.The items-center class centers the image vertically within the flex container.The justify-center class centers the image horizontally within the flex container.The w-32 and h-32 classes set the width and height of the image, respectively.Method 2. Using Positioning ClassesThis method uses Tailwind's absolute and transform utilities to center the image. HTML <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="relative h-screen"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20220221132017/download.png" alt="Centered Image" class="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-32 h-32"> </body> </html> The relative class on the container establishes a positioning context.The absolute class positions the image relative to the container.The top-1/2 and left-1/2 classes position the image in the center.The transform with -translate-x-1/2 and -translate-y-1/2 ensures the image is perfectly centered by adjusting its position based on its dimensions.Method 3. Using Margin Auto for Horizontal CenteringTo center an image horizontally within a block-level parent container, you can use the mx-auto class. HTML <html> <head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="h-screen flex items-start justify-center"> <div class="w-1/2"> <img src="https://media.geeksforgeeks.org/wp-content/uploads/20220221132017/download.png" alt="Centered Image" class="mx-auto w-32 h-32"> </div> </body> </html> The mx-auto class applies automatic margins on the left and right, centering the image horizontally within its parent.Ensure the parent container has a defined width (w-1/2 in this case) for mx-auto to work effectively. Comment More infoAdvertise with us Next Article How to Center an Image using Tailwind CSS ? tarunsinghwap7 Follow Improve Article Tags : Web Technologies CSS Tailwind CSS Tailwind CSS-Questions Similar Reads How to Center an Image in CSS? To center an image in CSS, we will align the image to the container horizontally and vertically as the layout requires.Center Images Horizontally1. Using text-align PropertyThe simplest way to center an image horizontally within a block-level container is to use the text-align property. This method 3 min read How to centre an Element using Tailwind CSS ? Tailwind CSS follows a utility-first approach, which means that instead of writing custom CSS for each component, we can utilize pre-defined classes that apply specific styles directly to HTML elements. We can center an element by using the utility classes of "margin" and "flex". This article will g 3 min read How to create a Chevron using Tailwind CSS ? A Chevron is a basic geometric shape that is used in websites to indicate directional movements or navigation. By utilizing specific border properties and transformations, a chevron shape can be achieved without the need for custom CSS. Tailwind's utility-first approach allows for a straightforward 3 min read How to Change Image on Hover using Tailwind CSS? One common effect is changing an image when the user hovers over it. We use Tailwind CSS, a utility-first CSS framework, to accomplish this without any additional JavaScript logic for the hover effect. By utilizing Tailwind's built-in classes we can create smooth transitions between two images where 2 min read How to Create A Sticky NavBar Using Tailwind CSS? Sticky Navbar in Tailwind CSS is mostly used in applications where maintaining quick access to navigation links is essential as users scroll through content. It ensures that the navbar remains visible at the top of the screen, enhancing user experience by providing constant access to key sections of 4 min read How to Rotate Container Background Image using CSS ? Rotating a container's background image using CSS involves applying the transform: rotate() property to the container element. This rotates the entire element at a specified angle, including its background image, creating a dynamic, rotating visual effect on the web page's design.Approach: Using tra 2 min read How to use calc() in Tailwind CSS? The calc() function in CSS allows you to perform calculations for property values dynamically. While Tailwind CSS doesnât directly support calc(), you can use it inline or with custom utilities.1. Using Inline StylesYou can use calc() directly within the style attribute for dynamic property values.H 2 min read Create Image Resize App using Tailwind CSS Image Resizer is an application that is used to resize an uploaded image by providing new dimensions in the form of width and height. This application contains two numbered input fields to get the number input for width and height from the user. It also contains a file input field to upload an image 3 min read How to Create Animated Loading Button using Tailwind CSS? An Animated Loading Button is a button that displays a loading spinner or animation to indicate an ongoing process, such as form submission. Using Tailwind CSS, you can create this by combining utility classes for styling and animations to visually enhance the button during loading. Table of Content 2 min read How to Align Form Elements to Center using Tailwind CSS? Form elements are parts of a webpage that let users enter information. They include things like text boxes, checkboxes, radio buttons, dropdown menus, and buttons. To center form elements using Tailwind CSS, use classes like flex, items-center, and justify-center on a parent container.These are the 2 min read Like