66

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?

Michael
  • 4,282
  • 9
  • 55
  • 89
bencarter78
  • 3,555
  • 9
  • 35
  • 53

9 Answers9

88

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

Ced
  • 1,293
  • 5
  • 23
  • 34
Nitin Jadhav
  • 6,495
  • 1
  • 46
  • 48
  • 5
    You are wrong to obtain `height:100vh;`you must use `h-screen` instead of `h-full` – Ced Jan 08 '19 at 10:14
  • 1
    @ced correct, that was a mistake on my side! Thanks – Nitin Jadhav Jan 08 '19 at 13:53
  • 4
    I think it's valid to note that `.h-full` will only work if the parent's element height is defined. Like: `
    This will do the work
    ` By not defining the height, this will not work: `
    This will NOT do the work
    `
    – AKOP Jul 16 '19 at 16:23
  • @AleksandrKopelevich I think 100vh = 100 viewport height. It has nothing to do with parent height. – Nitin Jadhav Jul 17 '19 at 03:35
  • @NitinJadhav Sure. I was talking about the case using h-full, instead of h-screen. Was a bit off topic though, I must admit. – AKOP Jul 17 '19 at 09:28
  • @AleksandrKopelevich got it now, no problem – Nitin Jadhav Jul 17 '19 at 09:47
31

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

gosentetsu
  • 471
  • 5
  • 5
17

You can use .h-screen class of Tailwind CSS.

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
11

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>
Jonathan Morales Vélez
  • 3,030
  • 2
  • 30
  • 43
7

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.

Simone Celli
  • 147
  • 2
  • 5
4

You may use h-screen class in your main div. Check out more here

Foysal imran
  • 111
  • 1
  • 11
1

When the content of the body is too little to fill the whole screen, I would use flexbox to vertically stretch the smaller divs evenly.

Make sure body and the parent div have 100% height, and use flex box as columns and add flex-basis to spread vertically and evenly.

html, body {height: 100%;}

.flex {
  display: flex;
  height: 100%;
  flex-direction: column;
}

.flex-1 {
/* or flex-grow: 1; */
flex-basis: 33.33%;
}
yinkouya
  • 83
  • 1
  • 11
1

For mobile, anything related to "h-screen" is going to cause problems because it sets the style to

height: 100vh

On mobile, the entire screen is seen as 100vh, meaning that your content would overflow the screen. This is due to the dynamic toolbars which are unique to every device. I would recommend you manually setting the height to 100svh to ensure that your content is always rendered correctly regardless of whether you view it on mobile or desktop. To my knowledge, Tailwindcss does not have an option for dynamic view port (yet).

<div style="height: 100svh" 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>
user2210411
  • 1,497
  • 1
  • 9
  • 7
0

Since I figured I might not be the only person here trying to make a full-screen div with a translucent background color... here's an explicit way to do so:

<div className='bg-black bg-opacity-80 h-screen w-screen top-0 left-0 fixed'></div>

You can also optionally add z-10 if a z-index is relevant to your use case.

Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47