The Wayback Machine - https://web.archive.org/web/20241106081433/https://www.geeksforgeeks.org/how-to-horizontally-and-vertically-center-an-element-in-tailwind-css/
Open In App

How to Horizontally and Vertically Center an Element in Tailwind CSS ?

Last Updated : 25 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

To horizontally and vertically center an element in Tailwind CSS, you can use the combination of flexbox and alignment utility classes. Apply flex and alignment classes to the parent container, and use mx-auto and my-auto for horizontal and vertical centering respectively.

Syntax

<div class="flex items-center justify-center h-screen">
<div class="bg-gray-300 p-4">
<!-- Your content here -->
</div>
</div>

Classes used to Center an Element

ClassDescription
flexEstablishes a flex container, enabling a flexible box layout for its child elements.
items-centerAligns items along the horizontal axis at the centre, facilitating horizontal alignment of child elements.
justify-centerCentres items along the vertical axis, ensuring vertical alignment of child elements within the flex container.
h-screenSets the height of the container to the full-screen height, making it occupy the entire vertical space of the viewport.
mx-autoSets the margin on the horizontal axis to auto, achieving horizontal centring for the container or element.

Example 1: Centering element with Flexbox

HTML
<!DOCTYPE html>
<html>

<head>
    <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
          rel="stylesheet">
    <title>
        Horizontally and Vertically Center
        an Element in Tailwind CSS
    </title>
</head>

<body class="px-10 py-10">
    <div class="flex justify-center items-center 
        bg-gray-100 h-screen">
        <div class="bg-green-500 text-white p-6">
            Horizontally and Vertically Centered Element
        </div>
    </div>
</body>

</html>

Output

Center-element-horizontal-and-vertical-flex

Example 2: Centering element with Grid

HTML
<!DOCTYPE html>
<html>

<head>
    <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
          rel="stylesheet">
    <title>
        Horizontally and Vertically Center 
        an Element in Tailwind CSS
    </title>
</head>

<body class="px-10 py-10">
    <div class="grid place-items-center bg-gray-100 h-screen">
        <div class="bg-green-500 text-white p-5">
            Horizontally and Vertically 
            Centered Element with Grid
        </div>
    </div>
</body>

</html>

Output

Center-element-horizontal-and-vertical

Key Features

  • Flexbox Layout: Utilizes flexbox to create a flexible container for easy horizontal and vertical centering.
  • Responsive Design: The approach is responsive, adapting to different screen sizes using Tailwind's responsive variants.
  • No Custom CSS: Achieve centering without the need for custom CSS rules, following Tailwind's utility-first approach.

Similar Reads

How to Center a Button Element Vertically and Horizontally with CSS?
Centering a button element both vertically and horizontally on a webpage is a common requirement in web design. It ensures that the button is always displayed at the centre of the webpage, regardless of the screen size. In this article, we will cover different approaches to centre a button element vertically and horizontally with CSS. Below are som
3 min read
How to Center an Element Horizontally and Vertically ?
We will learn how to center an element horizontally and vertically. Centering an element both horizontally and vertically is a common task in web development. It ensures that the element is always displayed at the center of the webpage, regardless of the screen size. Whether you’re aligning text, images, or other content, mastering these techniques
5 min read
How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS ?
We will know how to center-align the image in a div tag using CSS & will also understand its implementation through the example. Given an image, we need to set the image that align to the center (vertically and horizontally) inside a bigger div. But first let's create a basic structure, in which inside the body tag with id=outer there is an img
2 min read
How to vertically and horizontally align flexbox to center ?
As we already know, flexbox is a model and not just a CSS property.The below image shows a box with two axis, one is Main Axis (i.e. horizontal axis) and Cross Axis (i.e. vertical axis). And for aligning a flexbox in the centre, both horizontally and vertically, we are going to need to work on both of this axis. So, first of all, we need to remembe
2 min read
How to Center an Image Vertically and Horizontally ?
To center an image along X-axis i.e. Horizontally and along Y-axis i.e. vertically we can use different CSS properties. There are different ways to center an Image Vertically and Horizontally including CSS flexbox properties, absolute positioning, and CSS Grid System. Table of Content Using FlexboxUsing Absolute PositioningUsing GridApproach 1: Usi
3 min read
How to Center Text in a Div Vertically and Horizontally?
Centering text inside a <div> both vertically and horizontally is a common requirement. This can be done using various CSS techniques. Here, we’ll explore different approaches: using Flexbox, CSS Grid, Text Align, and the Transform property. Below are the approaches to center the text vertically or horizontally: Table of Content Using Flexbox
4 min read
How to repeat background image vertically and horizontally using CSS ?
In this article, we are going to discuss the background image repeat property of CSS. Also, we are going to discuss how to repeat the background image in horizontal and vertical directions. The background-repeat property in CSS is used to repeat the background image both horizontally and vertically. It also decides whether the background-image will
2 min read
How to align image vertically in a division that spans vertically on the whole webpage using CSS ?
In this article, we will discuss the vertical alignment of images in a single division that spans vertically on the whole webpage. Vertical alignment of images refers to placing the images in a column. We often need to align the image vertically in order to display it properly on the mobile version or sometimes, we don't want to insert a horizontal
3 min read
How to center an element horizontally using the CSS Box Model ?
Centering an element horizontally using the box model in CSS involves manipulating the margins or using positioning properties to achieve the desired centering effect. Different Ways to Center an Element Horizontally1. Using auto Margins:The auto margins will distribute the available space equally on both sides, horizontally centering the element.
2 min read
How to Center an Image Horizontally Within a Div Element using CSS?
Centering an image horizontally generally means occupying the center of its parent element along the x-axis. Below are the approaches to Center an Image Horizontally Within a Div Element: Table of Content Using Flexbox PropertyUsing margin auto PropertyUsing Position and Transform PropertiesUsing Flexbox PropertyExample: This example uses Flexbox t
2 min read
How to horizontally center a div using CSS ?
To make an HTML div center horizontally using CSS is quite easy. We can horizontally center any element using the below-mentioned approaches. Before centering a div horizontally we need to create a fixed-size div. We have given a border to the div so the centering of that div can be visible. Set the main container width to 100% so the div can be mo
2 min read
How to Vertically Center Text with CSS ?
Vertically centering text with CSS means aligning text to appear in the middle of its containing element from top to bottom. This is a common design requirement in web development to ensure content is visually balanced and properly positioned within its parent container. Below are the approaches to Vertically Center Text with CSS: Table of Content
3 min read
How to Change Tabs Horizontally on Hover with Tailwind CSS and JavaScript ?
Changing tabs on hover in Tailwind CSS enhances the user experience by offering interactive hover navigation. Users can quickly preview different sections without clicking which reduces the time it takes to explore different sections and increase engagement. We can also add CSS transitions for visual effects. Preview: Approach:The body element cont
3 min read
How to get a react bootstrap card to center vertically?
In this article, we will see how to center a React Bootstrap card vertically, you can use various approaches, including CSS flexbox, CSS grid, and CSS positioning. Table of Content Using FlexboxUsing CSS GridUsing Absolute PositioningSteps to create React App And Install Required Module:Step 1: Create a React application using the following command
4 min read
How to Drag the Textarea Vertically in Tailwind CSS?
In web development, textarea is used to collect multi-line text inputs from users. However, by default, a textarea is resizable in both directions—horizontally and vertically. There are different situations where we want to limit this resizing to vertical only, to maintain a consistent layout or design. We can add a vertical drag feature in textare
2 min read
Differences between HTML <center> Tag and CSS "text-align: center;" Property
If you are designing a simple web page, then not much difference is noticeable, but it is very necessary to understand the basic differences between these two. Since we are only concerned with the text, these elements do not have separate meanings.HTML <center> tag: The <center> tag in HTML is used to set to align of text into the cente
3 min read
How to Fixed an Element to Center in Tailwind CSS ?
Tailwind CSS is a utility-first CSS library that provides developers to build responsive and customizable user interfaces/Front-end. Tailwind CSS is the best way to write your code in an inline style. You don't need to write a single line of your CSS code you need to write only tailwind utility classes. In this article, we will see how to center th
4 min read
How to Center an Image using Tailwind CSS ?
Centering an image is a common requirement, and Tailwind CSS makes it simple to achieve with its utility-first approach. Tailwind CSS provides a variety of methods to horizontally and vertically center an image using utilities like flex, grid, and mx-auto. In this article, we will see how to center an Image using Tailwind CSS. Approach 1: Using Fle
3 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 following approaches to align form elements to th
2 min read
How to add an element horizontally in Html page using JavaScript?
There can be some cases where we have the requirement to add elements in a horizontal manner. Like, if your elements are linked list nodes and you want to add them horizontally. Now the question arises how we can do this in better way.One approach can be to use “display” property with the value “inline grid” as shown in the following program:When t
3 min read
How to Align a Div to Middle (horizontally/width) of Page using CSS ?
We are going to see how we can align a div horizontally using CSS. Centering content both vertically and horizontally is one of the most important functions that needs to be done. Here horizontally align refers to the same amount of space to the left and right of the div inside the parent div. Three approaches can be used to achieve this layout and
5 min read
Make a Div Horizontally Scrollable using CSS
We will design a horizontally scrollable div using CSS, & will see its implementation through the example. We can make a div horizontally scrollable by using the CSS overflow property. Below are the approaches to make a div horizontally scrollable using CSS: Table of Content By setting the overflow-y: hidden and overflow-x: autoBy using overflo
4 min read
How to split text horizontally on mouse move over using CSS ?
The SPLIT or BREAK effect is a captivating technique where text splits horizontally on hover, enhancing user interaction. Using CSS transitions, pseudo-elements, and positioning, this effect creates dynamic, layered text with smooth transitions, resulting in an engaging and visually striking animation. ApproachHTML Structure: Use an <h1> elem
2 min read
How to align objects vertically when working with grids in CSS ?
CSS grid layout is one of the strongest layout of CSS. The grid layout is 2-D which means it can handle both rows and columns, unlike flexbox which is 1-D. To align objects apply CSS to the parent element which becomes the grid container and the element's child which becomes the items in the grid. Approach: Use the align-content property of CSS gri
2 min read
How to make a div vertically scrollable without content using CSS ?
In this article, we will see how we can create a vertical scrollable section using CSS. HTML code is used to create the basic structure of the sections and CSS code is used to set the style, HTML Code: Create an HTML div element with the class container.Created three more nested div with class boxWrite some content inside each div with the class bo
2 min read
How to Vertically Align Text Next to an Image using CSS ?
Adding images into our websites is a common practice, and there are situations where text must be vertically aligned alongside an image. For example, a user’s name should appear immediately next to their profile picture, vertically aligned for optimal readability . In this article, we will see how to align text next to an image using various method
2 min read
How to Vertically Align Text Within a Div in CSS ?
Aligning text vertically within a div can enhance the visual appeal of your web design. There are various approaches to achieve vertical alignment using CSS. In this article, we'll explore different methods to vertically center text inside a div, ensuring a polished and professional look for your web content. Syntax: To vertically align text within
3 min read
How to vertically align text inside a flexbox using CSS?
Vertically aligning text inside a flexbox involves positioning the text centrally within the vertical space of a flex container. This can be achieved using CSS properties such as align-items and justify-content to control the vertical alignment and centering of the text. Here we have some common approaches: Table of Content Using align-items on the
2 min read
How to Align Social Media Icons Vertically on Left Side using HTML CSS & JavaScript?
In this article, we will learn how to create vertically aligned social media icons on the left side of a webpage using HTML, CSS, and JavaScript. We will use HTML to create the basic structure, CSS for styling, and JavaScript for added functionality. PrerequisitesHTMLCSSJavaScriptApproachWe are using HTML to give the basic structure to this project
4 min read
Making a div vertically scrollable using CSS
To create a vertical scrollbar for a <div> element, you need to use CSS properties that control overflow behavior. The overflow property is crucial for this, allowing you to specify how content that overflows the boundaries of its container should be handled. This guide demonstrates two common methods to make a <div> vertically scrollab
3 min read
three90RightbarBannerImg