How to use Tailwind Width
By Emmanuel Chinonso
Web Developer/Writer
Tailwind CSS Width
Tailwind CSS stands out as a highly customizable framework that allows developers to quickly style elements without battling against predefined styles. One of the most versatile features of Tailwind is its width utility, which simplifies the process of setting element widths. This guide will delve into how to use the width, max-width, and min-width utilities in Tailwind CSS to create responsive and adaptable designs.
Table of content
- Tailwind Width
- Tailwind Width Classes
- How to apply Tailwind CSS width
- How to Set Full Width in Tailwind
- Tailwind Fixed width
- Setting Width as a Percentages
- Responsive Width Adjustments
- Customizing Widths with Tailwind
- Tailwind Max width
- Tailwind Max width classes
- How to Use Tailwind Max Width
- Tailwind Min-width
- Tailwind Min-width Classes
- How to Use Tailwind Max Width
- Customizing Tailwind Max Width, and Min Width
- Conclusion
Tailwind Width
Tailwind width is a utility that allows you to set the width of an element using pre-defined classes. These classes range from w-0
(0px) to w-full
for 100% width, overing various sizes in between. This means you can quickly and easily set the width of an element without writing custom CSS.
<div class="w-32"> This div is 128 pixels wide.</div>
Tailwind Width Classes
Below is a table of Tailwind CSS width classes and properties
Class | Properties |
---|---|
w-0 | width: 0px; |
w-px | width: 1px; |
w-0.5 | width: 0.125rem; / 2px / |
w-1 | width: 0.25rem; / 4px / |
w-1.5 | width: 0.375rem; / 6px / |
w-2 | width: 0.5rem; / 8px / |
w-2.5 | width: 0.625rem;/10px / |
w-3 | width: 0.75rem; / 12px / |
w-3.5 | width: 0.875rem; / 14px / |
w-4 | width: 1rem; / 16px / |
w-5 | width: 1.25rem; / 20px / |
w-6 | width: 1.5rem; / 24px / |
w-7 | width: 1.75rem; / 28px / |
w-8 | width: 2rem; / 32px / |
w-9 | width: 2.25rem; / 36px / |
w-10 | width: 2.5rem; / 40px / |
w-11 | width: 2.75rem; / 44px / |
w-12 | width: 3rem; / 48px / |
w-14 | width: 3.5rem; / 56px / |
w-16 | width: 4rem; / 64px / |
w-20 | width: 5rem; / 80px / |
w-24 | width: 6rem; / 96px / |
w-28 | width: 7rem; / 112px / |
w-32 | width: 8rem; / 128px / |
w-36 | width: 9rem; / 144px / |
w-40 | width: 10rem; / 160px / |
w-44 | width: 11rem; / 176px / |
w-48 | width: 12rem; / 192px / |
w-52 | width: 13rem; / 208px / |
w-56 | width: 14rem; / 224px / |
w-60 | width: 15rem; / 240px / |
w-64 | width: 16rem; / 256px / |
w-72 | width: 18rem; / 288px / |
w-80 | width: 20rem; / 320px / |
w-96 | width: 24rem; / 384px / |
w-auto | width: auto; |
w-1/2 | width: 50%; |
w-1/3 | width: 33.333333%; |
w-2/3 | width: 66.666667%; |
w-1/4 | width: 25%; |
w-2/4 | width: 50%; |
w-3/4 | width: 75%; |
w-1/5 | width: 20%; |
w-2/5 | width: 40%; |
w-3/5 | width: 60%; |
w-4/5 | width: 80%; |
w-1/6 | width: 16.666667%; |
w-2/6 | width: 33.333333%; |
w-3/6 | width: 50%; |
w-4/6 | width: 66.666667%; |
w-5/6 | width: 83.333333%; |
w-1/12 | width: 8.333333%; |
w-2/12 | width: 16.666667%; |
w-3/12 | width: 25%; |
w-4/12 | width: 33.333333%; |
w-5/12 | width: 41.666667%; |
w-6/12 | width: 50%; |
w-7/12 | width: 58.333333%; |
w-8/12 | width: 66.666667%; |
w-9/12 | width: 75%; |
w-10/12 | width: 83.333333%; |
w-11/12 | width: 91.666667%; |
w-full | width: 100%; |
w-screen | width: 100vw; |
w-min | width: min-content; |
w-max | width: max-content; |
w-fit | width: fit-content; |
How to apply Tailwind CSS Width
Using Tailwind width is straightforward. Simply add the desired width class to your HTML element, and Tailwind will take care of the rest.
<div class="w-1/2"> This element has a width of half the parent container.</div>
How to Set Full Width in Tailwind
To make an element take up the full width of its container, use the w-full
class. This is particularly useful when you want an element to stretch across the entire width of the screen or a specific container.
<div class="w-full"> This div will stretch to the full width of its container.</div>
Preview
Tailwind Fixed width
To set a fixed width, you simply use the w-{size}
syntax where {size} is the size you want. For example, w-24
sets the element width to 6rem
(which is 96px if the default font size is 16px).
<div class="flex justify-center items-center"><div class="w-20">width of 80px</div><div class="w-32">width of 80px</div><div class="w-40">width of 80px</div></div>
Preview
Setting Width as a Percentages
Tailwind also allows you to set the width as a percentage of its parent container using fractional classes like w-1/4
, w-1/2
, or w-3/4
.
<div class="flex flex-col items-center justify-center"><div class="w-2/6">width of 33.33%</div><div class="w-1/2">width of 50%</div><div class="w-3/4">width of 75%</div></div>
Preview
Responsive Width Adjustments
Tailwind's responsive utilities enable you to adjust widths based on the viewport size. For instance, md:w-1/2
applies a width of 50% starting from the medium breakpoint.
<div class="w-full md:w-1/2 lg:w-1/4"> This element has a full width on small screens, half width on medium screens, and one-fourth width on large screens.</div>
Preview
Customizing Widths with Tailwind
Tailwind is highly customizable, allowing you to define your own width scales in the tailwind.config.js file. This is useful for adding widths that are specific to your project's design requirements.
module.exports = { theme: { extend: { width: { '96': '24rem', // Adds a new width utility for 24rem } } }}
<div class="text-lg w-96"> This div has with of 24rem</div>
Tailwind Max Width
Tailwind max width is a utility that allows you to set the maximum width of an element. This is useful when you want to prevent an element from becoming too wide and disrupting your layout. Max width classes range from max-w-xs
of max width of 20rem to max-w-xl
of max width of 80rem.
Tailwind Max Width Classes
Below are some classes used in Tailwind CSS for max width.
Class | Properties |
---|---|
max-w-0 | max-width: 0rem; |
max-w-none | max-width: none; |
max-w-xs | max-width: 20rem; |
max-w-sm | max-width: 24rem; |
max-w-md | max-width: 28rem; |
max-w-lg | max-width: 32rem; |
max-w-xl | max-width: 36rem; |
max-w-2xl | max-width: 42rem; |
max-w-3xl | max-width: 48rem; |
max-w-4xl | max-width: 56rem; |
max-w-5xl | max-width: 64rem; |
max-w-6xl | max-width: 72rem; |
max-w-7xl | max-width: 80rem; |
max-w-full | max-width: 100%; |
max-w-min | max-width: min-content; |
max-w-max | max-width: max-content; |
max-w-prose | max-width: 65ch; |
max-w-screen-sm | max-width: 640px; |
max-w-screen-md | max-width: 768px; |
max-w-screen-lg | max-width: 1024px; |
max-w-screen-xl | max-width: 1280px; |
max-w-screen-2xl | max-width: 1536px; |
Tailwind CSS Max Width has a number of advantages over conventional CSS frameworks. For starters, it eliminates the need for complex media queries, allowing developers to quickly and easily create responsive designs. It also makes it easier to create designs that are optimized for different devices and screen sizes, as developers can easily set different maximum widths for different devices. You can also check out Tailwind max-width page to learn more.
How to Use Tailwind Max Width
Using Tailwind max width is similar to using width. Simply add the desired max width class to your HTML element, and Tailwind will take care of the rest. Here are some examples:
max-w-md
sets the max width to 40rem (medium-sized screens)max-w-lg
sets the max width to 60rem (large screens)
<div class="flex"> <div class="w-1/2 bg-indigo-600 h-12 rounded-l-lg"> w-1/2 </div> <div class="w-1/2 bg-indigo-500 h-12 rounded-r-lg"> w-1/2 </div></div><div class="flex ..."> <div class="w-2/5 bg-indigo-600 h-12 rounded-l-lg"> w-2/5 </div> <div class="w-3/5 bg-indigo-500 h-12 rounded-r-lg"> w-3/5 </div></div><div class="flex ..."> <div class="w-1/3 bg-indigo-600 h-12 rounded-l-lg"> w-1/3 </div> <div class="w-2/3 bg-indigo-500 h-12 rounded-r-lg"> w-2/3 </div></div><div class="flex ..."> <div class="w-1/4 bg-indigo-600 h-12 rounded-l-lg"> w-1/4 </div> <div class="w-3/4 bg-indigo-500 h-12 rounded-r-lg"> w-3/4 </div></div><div class="flex ..."> <div class="w-1/5 bg-indigo-600 h-12 rounded-l-lg"> w-1/5 </div> <div class="w-4/5 bg-indigo-500 h-12 rounded-r-lg"> w-4/5 </div></div><div class="flex ..."> <div class="w-1/6 bg-indigo-600 h-12 rounded-l-lg"> w-1/6 </div> <div class="w-5/6 bg-indigo-500 h-12 rounded-r-lg"> w-5/6 </div></div><div class="w-full bg-indigo-500 h-12 rounded-lg"> w-full</div>
Preview
Tailwind Min Width
Tailwind min width is a utility that allows you to set the minimum width of an element. This is useful when you want to ensure that an element has a certain minimum width, even if its content is smaller. Min width classes range from min-w-xs
of min width of 20rem to min-w-xl
of min width of 80rem.
Tailwind Min-Width Classes
Class | Properties |
---|---|
min-w-0 | min-width: 0px; |
min-w-full | min-width: 100%; |
min-w-min | min-width: min-content; |
min-w-max | min-width: max-content; |
min-w-fit | min-width: fit-content; |
If the content is bigger than the minimum width, the min-width property has no impact. This prevents the width property's value from being less than the min-width. You can also check out Tailwind min-width page to learn more.
How to Use Tailwind Min Width
Using Tailwind min width is similar to using width. Simply add the desired min width class to your HTML element, and Tailwind will take care of the rest. Here are some examples:
min-w-md
sets the min width to 40rem (medium-sized screens)min-w-lg
sets the min width to 60rem (large screens)
<div className="w-24 min-w-full bg-indigo-600 h-12 rounded">min-w-full</div><div className="w-12 min-w-0 bg-indigo-300 h-12 rounded"> min-w-0</div><div className="w-12 min-w-max bg-indigo-300 h-12 rounded"> min-w-max</div><div className="flex ..."><div className="w-24 min-w-fit bg-indigo-600 h-12 rounded"> min-w-fit</div><div className=" w-24 min-w-mini bg-indigo-300 h-12"> min-w-mini</div></div>
Preview
min-w-full
min-w-0
min-w-max
min-w-fit
min-w-mini
Customizing Tailwind Max Width, and Min Width
While Tailwind provides a range of pre-defined max width and min width classes, you may need to create custom values for your project. Luckily, Tailwind makes it easy to do so. Simply add custom values to your tailwind.config.js
file, like so:
module.exports = { maxWidth: { 100: '800px' }, minWidth: { 100: '200px' } }
This will add new width, max width, and min width classes max-w-100
, and min-w-100
, which set the max width, and min width to 800px, and 200px respectively.
Conclusion
Tailwind width, max width, and min width are powerful utilities that make it easy to control the width of elements in your design. With its pre-defined classes and responsive design capabilities, you can create flexible and adaptable layouts with ease. By customizing Tailwind width, max width, and min width to fit your project's needs, you can take your web development to the next level.
Build modern projects using Bootstrap 5 and Contrast
Trying to create components and pages for a web app or website from
scratch while maintaining a modern User interface can be very tedious.
This is why we created Contrast, to help drastically reduce the amount of time we spend doing that.
so we can focus on building some other aspects of the project.
Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants.
Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for
building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.
Related Posts