Tailwind center div vertically and horizontally - Full screen

Updated by shortbuzz

Using tailwind css align item in the centre of the screen text and tailwind logo

Tailwind CSS is a utility-first CSS framework that offers a library of pre-defined CSS classes for quickly applying common styling patterns to elements.

Utilizing TailwindCSS, you can center a div both vertically and horizontally by combining the items-center and justify-center classes with the flex utility classes.

HTML:

<div class="flex items-center justify-center h-screen w-screen">
  <div>
    Your content goes here
  </div>
</div>

Output:

Check Live Demo

Explanation:

  • flex: This class transforms an element into a flex container, allowing its child elements to be positioned either horizontally or vertically. The CSS property used is display: flex
  • justify-center: This class centres the flex container's child elements horizontally within the parent container. The CSS property used is justify-content: center
  • items-center: This class centres the flex container's child elements vertically within the parent container. The CSS property used is align-items: center
  • h-screen: This class makes an element's height to be the exact height of the screen. The CSS property used is height: 100vh
  • w-screen: This class makes an element's width to be the entire width of the screen. The CSS property used is width: 100vw

As a result, the code will generate a div element that is centred both horizontally and vertically on the screen and takes up the entire screen space. The child div within it will also be centred within the parent div, as will the content within it.

Here's an example of how you can achieve the same layout as the Tailwind classes using only CSS:

CSS:

.flex-container {
  display: flex; /* sets the element to be a flex container */
  justify-content: center; /* aligns child elements horizontally */
  align-items: center; /* aligns child elements vertically */
  height: 100vh; /* sets the element's height to be the full height of the screen */
  width: 100vw; /* sets the element's width to be the full width of the screen */
}

HTML:

<div class="flex-container">
  <div>Your content goes here</div>
</div>

To centre align the div vertically and horizontally, you can also use the absolute inset-0 m-auto h-max w-max

HTML:

<div class="absolute inset-0 m-auto h-max w-max">
  <div>
    Content goes here
  </div>
</div>

Explanation:

  • absolute: This class removes the element from the typical document flow while positioning it in relation to its closest positioned ancestor. It means that while the element will be positioned in a specific location on the page, the placement of the components around it won't be affected.
  • inset-0: This class adds zero margin and padding to the div element's four sides. This removes the element's default padding and margin from the top, bottom, left, and right, bringing it flush with the edge of its parent container. The inset CSS property is a shortcut for the properties top, right, bottom, left.
  • m-auto: This class centres the element horizontally by setting the element's left and right margins to auto. This will centre the div element horizontally within its parent container. When the left and right margins are set to auto, the browser will calculate the remaining space and distribute it evenly between the two margins. The element will be effectively centred within its parent container as a result.
  • h-max: This class makes the element's height equal to 100% of the height of its parent container.
  • w-max: This class makes the element's width equal to 100% of the width of its parent container.

As a result, the code will generate a div element that is centred horizontally and vertically and fills the entire space of its parent container.

Here's an example of how you could use pure CSS to get the same styling as the preceding code snippet:

HTML:

<div class="container">
    Content goes here
</div>

CSS:

.container {
  position: absolute;
  inset: 0;
  margin: auto;
  height: 100%;
  width: 100%;
}

Using Tailwind CSS's grid system, centre a div both vertically and horizontally.

HTML:

<div class="grid place-content-center h-screen">
  Your content goes here1
</div>

Explanation:

  • grid: This class makes the element a grid container and enables grid layout. As a result, it is simple to align and position the child items inside the div.
  • place-content-center: This class centres the content within the parent container both vertically and horizontally. It combines the align-content, align-items, and justify-content features into a single shorthand utility class.
  • h-screen: This class makes the element's height equal to the height of the viewport/screen.

Thus, the code will generate a div element that is both horizontally and vertically centred on the screen, and the content contained within the div will also be centred. In terms of horizontal space, the div will occupy the entire screen.

This method, which is simple and easy to understand, uses Tailwind CSS's grid alignment utilities to centre the element within its parent container. It's worth noting that in this example, the parent container must have a defined height in order for the inner div to be correctly positioned.

Here's an example of using vanilla CSS to achieve the same styling as the previous code snippet:

CSS:

.center-container {
    display: grid;
    place-content: center;
    height: 100vh;
}

HTML:

<div class="center-container">
  Your content goes here1
</div>

Conclusion

In conclusion, Tailwind CSS is a strong tool that can be used to rapidly and simply design complicated and responsive layouts. It offers a selection of pre-defined CSS classes that can be used to apply typical stylistic conventions to elements, such as the ability to center div on the middle of screen. It can be simple to create responsive designs using its utility classes and to maintain uniformity across the entire website or application.

It is a fantastic option for developers that want to concentrate on creating the project's functionality rather than spending a lot of time on style.

More articles from shortbuzz

ecommece template tailwindcss text and businessman
Technology

Get Free TailwindCSS eCommerce Template: Build Online Store

TailwindCSS eCommerce template free: Take advantage of our template & boost your online store's sales with a visually stunning and user-friendly website design continue reading

OpenAI logo chatgpt
Technology

How to block ChatGPT from access the content of your website

By adding code to robots.txt, .htaccess or Nginx server, you can stop ChatGPT from access or crawl your website content that is being used for AI training data continue reading

Google sign-in button and Google logo
Technology

How to migrate from Google Sign-In API to Identity Services

Use Google Identity Services API to improve the user experience & security of your website. Replace old Google Sign-In API with new method: Full migration guide continue reading

flower with cards
Technology

TailwindCSS: Show More/Less Truncate Text using JS & Reactjs

Use TailwindCSS, JavaScript or ReactJS to create a read more & show less function with a smooth transition using tailwind multi-line-clamp truncation continue reading

Convert Number to Words
Technology

Convert Number to Words using Javascript, Jquery, ReactJS

Code and output for Convert number to words using JQuery, ReactJS, JavaScript, and TypeScript. Example: convert 1 to one continue reading

cloud server with laptop
Technology

Best web hosting service provider in Saudi Arabia (KSA)

Find the best web hosting service providers for your company in Saudi Arabia. Our detailed list includes the most cost-effective, reliable, and fast options. continue reading

Web hosting server and cloud hosting server
Technology

What is Web Hosting and Types: A Complete Beginners Guide

For creation of websites requires web hosting. Learn about many types and their advantages and disadvantages to select the best option for your website. continue reading

Xampp error windows
Technology

How to fix Error: MySQL shutdown unexpectedly in XAMPP

The cause of "Error: MySQL Shutdown Unexpectedly" in XAMPP, shutting down MySQL without running XAMPP as an administrator frequently leads to corruption. continue reading

Bootstrap icon and Tailwind css icon and grid layout design
Technology

How to make Tailwind CSS grid system like Bootstrap grids

Tailwind offers grid-template-column utility for creating basic grids, as well as the grid-cols-{size} & col-span-{size} utilities to create bootstrap-like grid continue reading

Python logo with folder icon and python code on computer screen in the background
Technology

Python Check If File Exists - Python Check If Directory Exists?

path.exists(path) method in python is used to check if a given file or directory exists or not; if it exists, it returns true or else return False continue reading