Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make CSS grid items have auto height using tailwind?

Let's suppose I have this 2 by 2 grid layout made with the help of Tailwindcss:

<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.4.6/tailwind.min.css" rel="stylesheet"/>
<div class="inline-grid grid-cols-2 grid-rows-2">
  <div class="px-1">Full name:</div>
  <div class="px-1">Favoutite fruits:</div>
  <div class="px-1">John Doe</div>
  <div class="px-1">
    <ul>
      <li>Apples</li>
      <li>Oranges</li>
      <li>Bananas</li>
    </ul>
  </div>
</div>

The problem with the above layout is that the rows are equal in height or, in other words, all the grid items are forced to have the height of the tallest of them.

The items on the first row must have the height required by only a single row of text.

How do I achieve that?

like image 339
Razvan Zamfir Avatar asked Jun 12 '20 14:06

Razvan Zamfir


People also ask

How do you customize your height in tailwind?

You can customize your spacing scale by editing theme.spacing or theme.extend.spacing in your tailwind.config.js file. To customize height separately, use the theme.height section of your tailwind.config.js file. Learn more about customizing the default theme in the theme customization documentation.

How does auto placement in CSS grid work?

Auto-placement by column In this case grid will add items in rows that you have defined using grid-template-rows . When it fills up a column it will move onto the next explicit column, or create a new column track in the implicit grid. As with implicit row tracks, these column tracks will be auto sized.

How does grid auto flow work?

The default behavior of Grid Auto Flow is to layout the elements by row, working along the row until there are no more slots then moving on to the next row. If a row is not declared then an implicit grid track will be created to hold the items.

How to use grid-Col and grid-row in tailwind CSS?

Tailwind uses grid-col and grid-row property which is an alternative to the grid-template-columns property in CSS. The grid-template-columns property in CSS is used to set the number of columns and size of the columns of the grid. This class accepts more than one value in tailwind CSS all the properties are covered as in class form.

How to set grid's height to fit content?

So you have this options here if you want grid's height to fit content: Set alignment for all items using align-items for grid container (default value is align-items: stretch ). So just set align-items: start for grid-2. Demo: Set alignment for grid items individually using align-self (default value is align-self: stretch ).

How do I customize the default grid-Auto-rows in tailwind?

By default, Tailwind includes four general purpose grid-auto-rows utilities. You can customize these values by editing theme.gridAutoRows or theme.extend.gridAutoRows in your tailwind.config.js file.

How do I add grid-cols with auto-fill and auto-fit in tailwind?

grid-cols- [repeat (auto-fill,minmax (100px… 1. Extend the gridTemplateColumns object in your Tailwind config This will create a grid-cols-auto-fill-100 and grid-cols-auto-fit-100 (but feel free to name them differently in the config!)


Video Answer


1 Answers

Simply remove grid-rows-2. No need to define the rows, defining the columns is enough

<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.4.6/tailwind.min.css" rel="stylesheet"/>
<div class="inline-grid grid-cols-2">
  <div class="px-1">Full name:</div>
  <div class="px-1">Favoutite fruits:</div>
  <div class="px-1">John Doe</div>
  <div class="px-1">
    <ul>
      <li>Apples</li>
      <li>Oranges</li>
      <li>Bananas</li>
    </ul>
  </div>
</div>
like image 125
Temani Afif Avatar answered Nov 08 '22 08:11

Temani Afif