Vertically centering an element with Tailwind

Vertically Centering Element with Flexbox: By leveraging Tailwind’s flexbox capabilities, this method focuses on centering elements along the vertical axis, providing a streamlined solution for achieving balanced and aesthetically pleasing designs.

Syntax

<div class="h-screen flex items-center">
Vertically Centered Element
</div>

Approach

  • Use h-screen to set the height of the element to the screen size.
  • Apply flex to make the element a flex container.
  • Use items-center to vertically center the element.

Example: Implementation to center an element vertically.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Vertically Centered Element with Tailwind</title>
    <link href=
"https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" 
          rel="stylesheet">
</head>
  
<body>
    <div class="h-screen flex items-center">
        Vertically Centered Element
    </div>
</body>
  
</html>


Output:

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 guide you through the various techniques to center an element using Tailwind CSS.

Table of Content

  • Horizontally centering an element
  • Vertically centering an element
  • Horizontally and Vertically center an element

Similar Reads

Horizontally centering an element with Tailwind

Horizontally Centering Element with Flexbox: Tailwind’s flexbox utilities make it simple to center elements horizontally, ensuring that content remains neatly aligned at the center of the page, enhancing readability and visual appeal....

Vertically centering an element with Tailwind

...

Horizontally and Vertically center an element with Tailwind

Vertically Centering Element with Flexbox: By leveraging Tailwind’s flexbox capabilities, this method focuses on centering elements along the vertical axis, providing a streamlined solution for achieving balanced and aesthetically pleasing designs....