Vertical align with tailwindcss across full screen div

ajthinking picture ajthinking · Mar 8, 2019 · Viewed 45.2k times · Source

How can I vertically align a div with tailwind? What I want:

-----------------------------------
|                                |
|                                |
|                                |
|             item1              |
|             item2              |
|                                |
|                                |
|                                |
-----------------------------------

What I currently have:

-----------------------------------
|             item1              |
|             item2              |
|                                |
|                                |
|                                |
|                                |
|                                |
|                                |
-----------------------------------

HTML

<div class="flex flex-col h-screen my-auto items-center bgimg bg-cover">
  <h3 class="text-white">heading</h3>
  <button class="mt-2 bg-white text-black font-bold py-1 px-8 rounded m-2">
    call to action
  </button>
</div>

CSS

.bgimg {
    background-image: url('https://d1ia71hq4oe7pn.cloudfront.net/photo/60021841-480px.jpg');
}

I have successfully centered on the secondary axis (left-right) with class items-center. Reading the documentation I tried align-middle but it does not work. I have confirmed the divs have full height and my-auto.

Im using this version of tailwind: https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css

Here is a JSFIDDLE: https://jsfiddle.net/7xnghf1m/2/

Answer

Nartub picture Nartub · Mar 15, 2019

You can also do

<div class="flex h-screen">
  <div class="m-auto">
    <h3>title</h3>
    <button>button</button>
  </div>
</div>