I'm just trying out Tailwind CSS and want to know how to fill the height of the viewport.
Taking this example HTML from the docs
<div class="flex items-stretch bg-grey-lighter">
<div class="flex-1 text-grey-darker text-center bg-grey-light px-4 py-2 m-2">1</div>
<div class="flex-1 text-grey-darker text-center bg-grey-light px-4 py-2 m-2">2</div>
<div class="flex-1 text-grey-darker text-center bg-grey-light px-4 py-2 m-2">3</div>
</div>
How do I make it stretch to the bottom of the screen?
Use h-full to set an element's height to 100% of its parent, as long as the parent has a defined height.
height:100vh When you set the height to 100vh, the box element will stretch its height to the full height of the viewport regardless of its parent height.
h-full: This class sets an element's height to 100% of its parent, as long as the parent has a defined height. h-screen: This class used to make an element span the entire height of the viewport.
There is a tailwind class named .h-screen
that translates to height:100vh;
css. This should work as well. For further details please refer to the Official Tailwind documentation
You can use .h-screen
class of Tailwind CSS.
You can add min-h-screen
to the parent <div>
, this would fill the height of the viewpoint.
And the difference with h-screen
is that this will stretch over the screen. It would be helpful when the screen is tiny and the content is overflowing with a scrollbar. In this situation the background will not fill the rest of the scrolled-up part.
<div class="flex items-stretch bg-grey-lighter min-h-screen">
<div class="flex-1 text-grey-darker text-center bg-grey-light px-4 py-2 m-2">1</div>
<div class="flex-1 text-grey-darker text-center bg-grey-light px-4 py-2 m-2">2</div>
<div class="flex-1 text-grey-darker text-center bg-grey-light px-4 py-2 m-2">3</div>
</div>
Link to some snippets at tailwind play (added a background color example to clarify it): https://play.tailwindcss.com/8Qd822yY4w
I know it's an old post, but I found it now, while I was trying to have the background color streched to the size of the screen when the content was too little to fill it and avoid to have the background color to stop suddenly when the content was more of the size of the screen (that happens with h-screen). I solved the problem using min-h-screen.
You may use h-screen class in your main div. Check out more here
I'm using NextJS + tailwindcss.
NextJS adds a <div id="__next">
, so this is what I have in my global.css
@tailwind base;
@tailwind components;
@tailwind utilities;
html {
@apply h-full;
}
body {
@apply h-full;
}
div#__next {
@apply h-full;
}
main {
@apply h-full;
}
And in my page I have
<main className="bg-gradient-to-br from-indigo-500 via-purple-500 to-pink-500">
...
</main>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With