Maintain consistent row heights in the Tailwind grid to avoid any row from stretching to match the height of the tallest row

EDIT 2: Here's an example with filler text added: https://play.tailwindcss.com/KPrBMJr6vI

At a viewport width of "2XL," you'll notice that the heights of Blocks 3 and 4 are exceptionally tall. Block 5, on the other hand, behaves as desired. My current challenge is to ensure that only Block 3 and 4 adjust their heights based on their respective content sets.

EDIT 1: Removing all 'grid-rows-X' definitions from the outer div seems to make everything behave correctly, except at the 2xl media size. Below that, everything works perfectly. I'm uncertain about what definitions should be added to set Block 3, 4, and 5 to auto-height according to content while ensuring that 5 stretches to "fill."

Here is the layout/code:

<div class="grid-rows-7 mt-2 grid grid-cols-1 gap-2 text-white lg:mt-4 lg:grid-cols-5 lg:grid-rows-6 lg:gap-4 2xl:grid-rows-5">
  <div class="col-span-1 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-5">
    <div class="relative h-auto overflow-hidden rounded-lg"></div>
  </div>

  <div class="col-span-1 col-start-1 row-span-1 row-start-2 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-2 lg:row-span-4 2xl:col-span-2 2xl:row-span-3">Block 2: Left Column</div>

  <div class="col-span-1 col-start-1 row-start-3 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-2 2xl:col-span-2 2xl:col-start-3 2xl:row-start-2">
    <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-white">Block 3</h5>
    <div class="mb-4 items-center justify-between pl-3 pr-3 sm:flex sm:space-x-2 sm:space-y-0">
      <div class="relative inline-flex items-center justify-start"></div>
    </div>

    <div class="mb-4 content-center pl-3 pr-3">
      <div class="flex w-full items-center justify-start pb-2">
        <span class="flex text-sm font-medium text-gray-500 dark:text-gray-400">
          <div role="status" class="max-w-sm animate-pulse">
            <div class="h-1 w-24 rounded-full bg-gray-200 dark:bg-gray-700"></div>
          </div>
        </span>
      </div>
    </div>
  </div>

  <div class="col-span-1 col-start-1 row-start-4 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-3 2xl:col-span-2 2xl:col-start-3 2xl:row-start-3">
    <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-white">Block 4</h5>
    <div class="mb-4 items-center justify-between pl-3 pr-3 sm:flex sm:space-x-2 sm:space-y-0">
      <span class="ml-2 text-sm text-gray-500 dark:text-gray-400">blah</span>
    </div>
  </div>

  <div class="col-span-1 col-start-1 row-start-5 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-4 2xl:col-span-2 2xl:col-start-3 2xl:row-start-4">
    <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-white">Block 5</h5>
  </div>

  <div class="col-span-1 col-start-1 row-span-1 row-start-6 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-5 2xl:col-span-1 2xl:col-start-5 2xl:row-span-3 2xl:row-start-2">
    <div class="mb-8 block w-full">
      <h5 class="text-xl font-medium text-gray-500 dark:text-white">Block 6: Right Column</h5>
    </div>
    <div class="block w-full pt-8"></div>
  </div>

  <div class="col-span-1 row-start-7 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-5 lg:row-start-6 2xl:row-start-5">Block 7</div>
</div>

This produces the following layout:

https://i.sstatic.net/tX3PK.png

I am facing difficulty in setting each row to have a height based on its own content. In cases where Block 2 or Block 6 has greater height than Blocks 3, 4, and 5 combined, I want Block 5 to automatically adjust its height to match either Block 2 or 6. Currently, Block 6 has the longest content, causing all Blocks to have the same height. Using 'h-fit' on Block 1 makes its height shrink to fit the content, but the second row's distance remains long as if it still matches Block 6's height.

How can I ensure that each Block adjusts its height according to its content, while allowing Blocks 2, 5, and 6 to adjust their height as needed based on whichever column has the longest content?

Answer №1

Consider using auto instead of 1fr for most grid row tracks, except for the second-to-last row which should expand to fill the remaining space available:

<script src="https://cdn.tailwindcss.com/3.3.2"></script>

<div class="grid-rows-[repeat(7,auto)] mt-2 grid grid-cols-1 gap-2 text-white lg:mt-4 lg:grid-cols-5 lg:grid-rows-[repeat(4,auto)_1fr_auto] lg:gap-4 2xl:grid-rows-[repeat(3,auto)_1fr_auto]">
  <div class="col-span-1 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-5">
    <div class="relative h-auto overflow-hidden rounded-lg"></div>
  </div>

  <div class="col-span-1 col-start-1 row-span-1 row-start-2 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-2 lg:row-span-4 2xl:col-span-2 2xl:row-span-3">Block 2: Left Column</div>

  <div class="col-span-1 col-start-1 row-start-3 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-2 2xl:col-span-2 2xl:col-start-3 2xl:row-start-2">
    <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-white">Block 3</h5>
    <div class="mb-4 items-center justify-between pl-3 pr-3 sm:flex sm:space-x-2 sm:space-y-0">
      <div class="relative inline-flex items-center justify-start"></div>
    </div>

    <div class="mb-4 content-center pl-3 pr-3">
      <div class="flex w-full items-center justify-start pb-2">
        <span class="flex text-sm font-medium text-gray-500 dark:text-gray-400">
          <div role="status" class="max-w-sm animate-pulse">
            <div class="h-1 w-24 rounded-full bg-gray-200 dark:bg-gray-700"></div>
          </div>
        </span>
      </div>
    </div>
  </div>

  <div class="col-span-1 col-start-1 row-start-4 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-3 2xl:col-span-2 2xl:col-start-3 2xl:row-start-3">
    <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-white">Block 4</h5>
    <div class="mb-4 items-center justify-between pl-3 pr-3 sm:flex sm:space-x-2 sm:space-y-0">
      <span class="ml-2 text-sm text-gray-500 dark:text-gray-400">blah</span>
    </div>
  </div>

  <div class="col-span-1 col-start-1 row-start-5 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-4 2xl:col-span-2 2xl:col-start-3 2xl:row-start-4">
    <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-white">Block 5</h5>
  </div>

  <div class="col-span-1 col-start-1 row-span-1 row-start-6 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-3 lg:col-start-3 lg:row-start-5 2xl:col-span-1 2xl:col-start-5 2xl:row-span-3 2xl:row-start-2">
    <div class="mb-8 block w-full">
      <h5 class="text-xl font-medium text-gray-500 dark:text-white">Block 6: Right Column</h5>
    </div>
    <div class="block w-full pt-8"></div>
  </div>

  <div class="col-span-1 row-start-7 rounded-lg bg-white p-4 shadow dark:bg-zinc-950 lg:col-span-5 lg:row-start-6 2xl:row-start-5">Block 7</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmit information to an HTML website displayed within a QT GUI utilizing QWebview

Currently, I am developing a desktop application using QT (c++) that integrates a website created with a basic html file (including javascript) through Qwebview. The website consists of a plugin and html boxes where the data entered triggers changes in the ...

conceal the HTML code from the students

As an instructor of an HTML/CSS course, I often assign tasks that require students to replicate the design of a webpage. However, providing them with direct links to the page makes it easy for them to decipher my methods and simply copy the work. Furthermo ...

Leaflet map displaying accurate position only upon browser resize

Let me start by showing you a screenshot of the issue I am facing. Check it out here The screenshot clearly shows a gap between my expandable left navbar and the left border of the map image. However, when I resize the browser window by dragging and drop ...

arranging texts in a vertical stack with wrapping

Looking to change the orientation of my horizontally wrapped list items from vertical to horizontal. Take a look at my code snippet: HTML <ul> <li>Long text goes here.</li> <li>Another Longer Text Goes Here</li> < ...

What sets apart #navlist li #current from #navlist li .current?

My quest to grasp the ins and outs of CSS continues, but tutorials I've encountered so far are only skimming the surface. I'm particularly trying to wrap my head around the fundamental variances between using #navlist li #current and #navlist li ...

Creating a standalone Wordpress child theme with its own CSS

After creating a child theme based on the responsive i-transform theme from http://templatesnext.org/itrans/, I found myself continuously undoing the i-transform CSS, which is taking up a lot of my time. However, if I remove the entire parent stylesheet, t ...

Guide on adding user input values (type=text) into HTML using JavaScript function outcome

I'm looking for a way to use the output of a JavaScript function as the value for an input field in HTML. Currently, I have a JavaScript function that generates a random string: function generateRandomString(length) { let result = ''; ...

Can you explain the variance between e-resize and ew-resize cursor styles?

Can you explain the differences between these cursor types? I'm noticing that they all appear as the same cursor in Chrome on Windows. .e-resize {cursor: e-resize;} .ew-resize {cursor: ew-resize;} .w-resize {cursor: w-resize;} <p>Mouse over t ...

The image link in HTML and CSS is no longer functioning properly following the relocation of the image

I am having an issue with positioning my image under my name on my website. When I try to move the image down, the link no longer works properly. Interestingly, moving the image left or right does not cause any issues. It's only when trying to move it ...

Integrating dynamic PHP echo strings from a database into an established CSS layout

I am currently exploring PHP and MySQL databases, and I have managed to develop a basic search engine for my website. However, I am facing an issue that I am struggling to resolve: Is there a way to integrate the PHP echo output (search results) into the ...

the radio selection buttons are malfunctioning

In a section, there are checkboxes that, when clicked, display radio button options specific to that checkbox. Each checkbox is associated with an array item. <form id="lab_test_detail"> {item.subcategory.map((item, index) => <& ...

Customize your click event with conditional styling in React

When the onClick event is triggered, I am attempting to modify a class by calling my function. It appears that the function is successfully executed, however, the class does not update in the render output. Below is the code snippet: import React from &ap ...

ReactJS how to prevent accordion from collapsing automatically

My custom accordion layout for the features needed in the site I am building is not working well with Jquery. How can I modify it to collapse properly? Specifically, I want it so that when I open number 2, number 1 will automatically close. I've trie ...

Switch the position of the checkbox label to the left in a customized Bootstrap checkbox

I am struggling with adjusting the label position for my checkbox. Here is my code: <div class="custom-control custom-checkbox custom-control-inline"> <input type="checkbox" id="location" v-model="checkedLocations" value="location" name="c ...

Tips for changing a fullscreen HTML5 video appearance

I discovered a way to change the appearance of a regular video element using this CSS code: transform: rotate(9deg) !important; But, when I try to make the video fullscreen, the user-agent CSS rules seem to automatically take control: https://i.sstatic. ...

Discovering the method to display the non-selected option from the dropdown list in another dropdown list within the same row of a table using jQuery

I've got an HTML table with input text fields and dropdown lists as columns. Two of the dropdown list columns display the same list of options. I'm attempting to remove the selected option in one dropdown from appearing in the other dropdown in ...

Identifying input fields using XPath depending on their associated labels

I'm trying to figure out why an XPath query I found returns multiple results when selecting an input based on its label. The first XPath query I used, //input[@id=(//label[.='Max Driving Time_h']/@for)], retrieves 3 inputs even though only o ...

Tips for shrinking a circle size

Is there a way to decrease the size of both circles? I have tried adjusting the values in the source code, but haven't been successful. The circles are generated using canvas. Here is the HTML: <div style='position: absolute: top: 0px; left: ...

What is the process for clearing the input value of a View from another View?

My situation seems simple and straightforward, yet I am struggling to achieve the desired output. In this scenario, I have two HTML pages named Index.htm and Main.htm, as well as a script page named mscript.js. Index.htm contains a button (Clear), and Mai ...

Does the parent container require a defined width?

I'm working with a parent div that contains three inner child divs. Here's the structure: <div style="width:900px;"> <div style="width:300px;">somedata</div> <div style="width:300px;">somedata</div> <div ...