Tailwind CSS Size Control
Tailwind CSS provides a set of concise utility classes to help developers quickly control element sizes in HTML.
Here are some common size control utility classes and their usage.
Width - w-*
w-* is used to set the width of an element.
Tailwind provides some predefined widths, supporting pixels, percentages, viewport units, etc.
| Class Names | Meaning | Example |
|---|---|---|
w-1/2 |
Set the width to half of the parent element | |
w-1/4 |
Set the width to a quarter of the parent element | |
w-full |
Set the width to 100% of the parent element | |
w-screen |
Set the width to the viewport width | |
w-64 |
Set the width to 16rem (256px) | |
w-auto |
Width adapts to content | |
Use utility classes such as w-px, w-1, w-64 to set fixed widths for elements.
Example
w-96
w-80
w-64
w-48
w-40
w-32
w-24
Try it »
Use utility classes such as w-full, w-1/2, w-2/5 to set percentage-based element widths.
Example
w-1/2
w-1/2
w-2/5
w-3/5
w-1/3
w-2/3
w-1/4
w-3/4
w-1/5
w-4/5
w-1/6
w-5/6
w-full
Try it »
Use w-screen to make an element span the entire viewport width.
If you need to remove an element's specified width under certain conditions (for example, at a specific breakpoint), the w-auto utility can be useful:
Height - h-*
Height classes are used to set the height of an element, also supporting pixels, percentages, viewport units, etc. Common height utility classes include:
| Class Names | Meaning | Example |
|---|---|---|
h-16 |
Set the height to 4rem (64px) | |
h-1/2 |
Set the height to half of the parent element | |
h-screen |
Set the height to the viewport height | |
h-auto |
Height adapts to content | |
Use utility classes such as h-px, h-1, h-64 to set fixed heights for elements.
Example
h-96
h-80
h-64
h-48
h-40
h-32
h-24
Try it »
Use h-full to set an element's height to 100% of its parent, provided the parent has a defined height.
Use h-screen to make an element span the entire viewport height.
Use h-dvh to make an element span the entire viewport height, which changes as the browser UI expands or shrinks.
Scroll up and down in the viewport to hide/show the browser UI:
Example
Try it »
Use h-lvh to set an element's height to the largest possible viewport height. This behaves the same as 100vh.
Scroll up and down in the viewport to hide/show the browser UI:
Example
Try it »
Use h-svh to set an element's height to the smallest possible viewport height.
Size Utility Classes
In Tailwind CSS,size Utility classes let you set both the width (width) and height (height) of an element. These utility classes are typically used in scenarios requiring a fixed width-to-height ratio, such as creating squares, circles, or containers with fixed proportions.
size-* class
Tailwind provides some size utility classes that can set an element's width and height to the same value.
Common utility classes include:
| Class Names | Meaning | Example |
|---|---|---|
size-0 |
Set the width and height to 0 |
|
size-1 |
Set the width and height to 0.25rem |
|
size-2 |
Set the width and height to 0.5rem |
|
size-4 |
Set the width and height to 1rem |
|
size-8 |
Set the width and height to 2rem |
|
size-16 |
Set the width and height to 4rem |
|
size-32 |
Set the width and height to 8rem |
|
size-64 |
Set the width and height to 16rem |
|
size-auto |
Set the width and height to adapt automatically | |
These size utility classes set the same value for both the width and height of an element, typically used in scenarios requiring equal width and height, such as:
- Circular images
- Card components
- Buttons and other fixed-size elements
Use utility classes such as size-px, size-1, size-64 to set both fixed width and height for an element.
Example
size-16
size-20
size-24
size-32
size-40
Try it »
Use size-full to set an element's width and height to 100% of the parent container's width and height.
size-full
If you need to remove an element's specified width and height under certain conditions (for example, at a specific breakpoint), the size-auto utility can be useful:
The size-auto class is ideal for elements whose size is not fixed and adapts to content size.
Tailwind CSS also supports responsive design, allowing different size utility classes to be used at different screen sizes. You can set different widths and heights for elements at different screen widths.
This box's size will change based on the screen size.
In the code above, size-16 is the default size, changing to size-32 on medium screens (md:) and to size-64 on large screens (lg:).
Minimum Width - min-w-*
min-w-* Used to set the minimum width of an element, ensuring it is not smaller than the specified width when its content is insufficient.
| Class Names | Meaning | Example |
|---|---|---|
min-w-full |
Set the minimum width to 100% | |
min-w-0 |
Set the minimum width to 0 | |
min-w-max |
Set the minimum width to the max-content width | |
Minimum Height - min-h-*
min-h-*
Used to set the minimum height of an element, ensuring its height is not smaller than the specified value.
| Class Names | Meaning | Example |
|---|---|---|
min-h-0 |
Set the minimum height to 0 | |
min-h-full |
Set the minimum height to 100% of the parent element | |
min-h-screen |
Set the minimum height to the viewport height | |
Maximum Width (Max Width) - max-w-*
max-w-* Used to set the maximum width of an element; when the content width exceeds this value, the element width will not increase further.
| Class Names | Meaning | Example |
|---|---|---|
max-w-xs |
Set the maximum width to 20rem(320px) |
|
max-w-sm |
Set the maximum width to 24rem(384px) |
|
max-w-full |
Set the maximum width to 100% of the parent container | |
Maximum Height (Max Height) - max-h-*
max-h-* Used to set the maximum height of an element; when the content height exceeds this value, the element height will not increase further.
| Class Names | Meaning | Example |
|---|---|---|
max-h-32 |
Set the maximum height to 8rem(128px) |
|
max-h-screen |
Set the maximum height to the viewport height | |
Automatic Size (Auto) - auto
w-auto and h-auto classes are used to let an element's width or height adapt to its content.
| Class Names | Meaning | Example |
|---|---|---|
w-auto |
Width adjusts automatically | |
h-auto |
Height adjusts automatically | |