The Wayback Machine - https://web.archive.org/web/20240702062532/https://www.geeksforgeeks.org/tailwind-css-container/
Open In App

Tailwind CSS Container

Last Updated : 07 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Tailwind CSS provides a container class to constrain the width of content. It centers the content horizontally and sets maximum widths at different breakpoints, ensuring responsive design. By default, it limits content width to prevent it from stretching too wide on larger screens.

Breakpoints in Tailwind CSS

Breakpointmin-width
sm640px
md768px
lg1024px
xl1280px
2xl1536px

Tailwind CSS does not center itself automatically and also does not contain any pre-defined padding. The following are some utility classes that make the container class stand out.

mx-auto Class

To center the container, we use mx-auto utility class. It adjust the margin of the container automatically so that the container appears to be in center. 

Syntax:

<element class="mx-auto">...</element>

Example: In tis example we ses Tailwind’s container class to create a fixed-width centered container. mx-auto class centers the container horizontally within the viewport.

HTML
<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
          rel="stylesheet">
</head>

<body>
    <h2 class="text-center text-green-500">
        GeeksforGeeks
    </h2><br />

    <div class="bg-green-500 text-white mx-auto p-4">
        This is mx-auto class
    </div>
</body>

</html>

Output:

mx-auto class

px-{size} Class

To add padding the container, we use px-{size} utility class. It adds horizontal padding to the container which is equal to the size mentioned.

Syntax:

<element class="px-20">...</element>

Example: In this example we uses Tailwind’s container class to create a fixed-width centered container. Additionally, the px-20 utility class adds horizontal padding of size 20 to the <div>, enhancing its layout and readability.

HTML
<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
          rel="stylesheet">
</head>

<body>
    <h2 class="text-center text-green-500">
        GeeksforGeeks
    </h2>
    <br />

    <div class="bg-green-500 text-white mx-auto px-20">
        This is px-20 class
    </div>
</body>

</html>

Output:

px-size class


Previous Article
Next Article

Similar Reads

How to move a container inside of another container in Bootstrap 5 ?
The container is used to set the content’s margin. It contains row elements and the row elements are containers of columns. This is known as the grid system. In this article, we will learn about How to move a container inside of another container in Bootstrap 5. We will move a container to the left in another container. Syntaxms-0 me-0 Table of Con
2 min read
What is the Difference Between container and container-fluid in Bootstrap 5 ?
In Bootstrap 5, the "container" class creates a responsive container with a fixed width, adjusting to different screen sizes while maintaining margins. On the other hand, the "container-fluid" class generates a full-width container that spans the entire viewport without any margins, providing a seamless layout across various devices. "Container" is
3 min read
How to create a full-width Container in Tailwind CSS?
Creating a full-width Container in Tailwind CSS involves using the container class along with additional utility classes like mx-auto to centre the content. The container class ensures that the content remains within a fixed width, and mx-auto centres it horizontally. Syntax:&lt;div class="container mx-auto"&gt; &lt;!-- Content goes here --&gt;&lt;
1 min read
What is the use of the tailwind.config.js file in Tailwind CSS ?
The tailwind.config.js file in Tailwind CSS serves as a central configuration hub for customizing the framework. It allows you to define colors, fonts, breakpoints, and more, tailoring Tailwind to fit your project's specific design and functionality requirements. Syntax:/** @type {import('tailwindcss').Config} */module.exports = { content: [], them
1 min read
Storybook-tailwind. How should I add tailwind to storybook
In this article, we will guide you through the process of adding Tailwind CSS to Storybook to enhance your UI development. Storybook is an open-source UI that helps developers create reusable, organized UI components with proper documentation independently. Tailwind CSS is basically a utility-first CSS framework for building rapid custom UI. Prereq
2 min read
Primer CSS Flexbox Flex Container
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Primer CSS Flexbox Flex Container is used to make an element layout it's content using
2 min read
How to Rotate Container Background Image using CSS ?
The CSS background-image property is used to add an image to the background of an element. In this article, the task is to rotate the image which is used in the background. Approach: The CSS transform property is used to apply two-dimensional or three-dimensional transformation to a element. This property can be used to rotate, scale, move or even
1 min read
How to fit text container width dynamically according to screen size using CSS ?
In this article, we have given a text paragraph and the task is to fit the text width dynamically according to screen size using HTML and CSS. Some of the important properties used in the code are as follows- display-grid: It helps us to display the content on the page in the grid structure.grid-gap: This property sets the minimum amount of gap bet
7 min read
How to flex-start items at the beginning of the container using CSS ?
The flex-start value of the justify-content property allows you to place the items at the start of the container. The flex-start indicates the flex-direction while the start indicates the writing-mode direction. The children of the parent container are allowed to align at the start of the parent container or div. Follow the given steps: Create an H
1 min read
How to wrap an element with more content in flexbox container to have the same width as other elements in CSS ?
In a flexbox container, we might have a scenario where you want to wrap an element with more content, such as text or an image, to have the same width as other elements. In this article, we will see how to wrap an element with more content in a flexbox container to have the same width as other elements using CSS. The flexbox or flexible box model i
3 min read
three90RightbarBannerImg