Box Sizing In Tailwind CSS

Let’s delve into the concept of Tailwind box sizing with examples through this article. Users can use this class to define whether padding and borders are included in the total width and height calculations or not.

The class accepts multiple values in tailwind CSS, covering all properties.

An alternative to CSS box-sizing.

Tailwind Box Sizing



Tailwind Box Sizing Classes

Box-border

When using tailwind box border class, the width and height properties encompass an element’s content, padding, and borders.

For instance, if we define an element’s width as 500 pixels, this width includes any added borders or padding.

Hence, the content box will reduce its size to adjust for the extra width.

This simplifies the process of sizing elements.

Syntax

<element class="box-border">....</element>

Box-content

When using box-content class, the width and height properties include only the content of an element.

For instance, if we define an element’s width as 500 pixels, the content would be 500 pixels wide without padding and border.

The padding and border will get added to this width on render.

This is the default value of the box-sizing class.

Syntax

<element class="box-content">....</element>

Below is an example revolves around the application of box-border and box-content classes in Tailwind:

Example: 

<!DOCTYPE html> <html><head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <center> <h1 class="text-gray-600 text-2xl font-semibold"> MrExamples </h1> <p class="font-semibold my-2">Box Sizing Class in Tailwind CSS</p> <div class="box-border h-28 w-48 p-4 font-semibold my-2 border-4 bg-gray-400 m4 flex justify-center items-center"> box-border </div> <div class="box-content h-28 w-48 p-4 font-semibold my-w border-4 bg-gray-400 m4 flex justify-center items-center"> box-content </div> </center> </body> </html>

Below is another example that helps in understanding of box-border and box-content classes:

Example: 

<!DOCTYPE html> <html><head> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="text-center"> <center> <h1 class="text-gray-600 text-2xl font-semibold"> MrExamples </h1> <p class="font-semibold my-2">Box Sizing Class in Tailwind CSS</p> <div class="box-border h-32 w-32 rounded-full p-4 font-semibold my-2 bg-yellow-500 m4 flex justify-center items-center"> box-border </div> <div class="box-content h-32 w-32 rounded-full p-4 font-semibold my-2 bg-yellow-500 m4 flex justify-center items-center"> box-content </div> </center> </body> </html>

Having learned what Tailwind Box Sizing is, you can now use it to create a stunning website design.

We value your feedback.
+1
0
+1
0
+1
1
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *