3

I have a design where I have 3 items. 2 items should be placed vertically and 1 item has to be in it's own cell. So, 2 items should be placed in 1 cell vertically and 1 item takes it's own whole cell. To demonstrate, below is the image

enter image description here

How can I achieve this design using tailwind?

2 Answers 2

3

You can make such layout using grid - separate your layout on three columns (sidebar + main content spans on two columns) and 2 rows like @ChenBr did

<div class="border-2 grid grid-cols-3 grid-rows-2">
  <div class="border-2 col-span-1">1</div>
  <div class="border-2 col-span-2 row-span-2">2</div>
  <div class="border-2 col-span-1">3</div>
<div>

However grid-rows-2 will be compiled as

.grid-rows-2 {
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

1fr for both rows means 2 rows will be sized equally. So I would recommend to change this class into grid-rows-[min-content_1fr] - that mean first row will take place base on its minimum content

min-content - is a keyword representing the largest minimal content contribution of the grid items occupying the grid track.

and the second one will take the rest - but it's up to your application

Basic example

<div class="grid grid-cols-3 grid-rows-[min-content_1fr]">
  <div>1</div>
  <div class="col-span-2 row-span-2">2</div>
  <div>3</div>
<div>

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

what tailwind version is it? When I try to apply in my project, the browser tells me that the grid-rows-[min-content_1fr] is an invalid property. But, on tailwind playground, it works fine.
It should be supported since version 2.2+ with mode: 'jit' in config or 3+ by default - it is called arbitrary values
Hmmm, I am using tailwind 3.4. Weird that it does not work as a tailwind class but as a vanilla css class, it works.
@LosMos Tailwind doesn't have release 3.4 yet - max 3.2.4
I mean 3.2.4. That was a typo there.
1

You can achieve this setup using grid/flex. Using one or the other depends on your content.

Grid:

Create a three-by-two grid using grid-cols-3 and grid-rows-2 on the grid's container. Then, set each container's span to fit your structure (using col-span-n row-span-n).

Read about grid-cols here and about grid-row here.

<div class="border-2 grid grid-cols-3 grid-rows-2">
  <div class="border-2 col-span-1">1</div>
  <div class="border-2 col-span-2 row-span-2">2</div>
  <div class="border-2 col-span-1">3</div>
<div>

grid

Tailwind-play


Flex:

We are going to have two containers. The main container will wrap all the elements (including the second container), and the inner container will wrap your first two elements. Each of those containers will have a flex utility applied to it.

Then, we will apply flex-col on the second container. This way, the container will place its children on top of each other, just like the first column of your image.

The first container's default flex-direction is flex-row which is why the inner container and the third element will be positioned next to each other, just like a row.

To give the structure a proportion similar to your image, we can set the inner container's width to 30% (w-[30%]), and the third element to 70% (w-[70%]).

Read about flex-direction here.

<div class="flex border-2">
  <div class="flex w-[30%] flex-col">
    <div class="border-2">1</div>
    <div class="border-2">2</div>
  </div>
  <div class="w-[70%] border-2">3</div>
  <div></div>
</div>

flex

Tailwind-play

4 Comments

I have tried what u said. The problem is, The 3rd element should be placed in first row and since there is dynamic data in each element, mainly in 2nd element, it ends up making the 2nd element large which moves the 3rd element in first row way too down. I attached an SS here
@LosMos, I see what you mean. It looks like grid won't be working well with your structure unless you use Ihar Aliakseyenka addition. Doesn't the flex method work in your case? It should be a perfect scenario for that. Can you please provide the code from your SS? Also, the number I provided for each element is arbitrary. You can swap them however fits your structure.
Actually, in order to use flex, I will have to change all of the internal structure of the underlying components. Those components get html dynamically from layout files. I am working on a magento 2 project and there are different layout files for all underlying components. I can change the underlying html structure but, It is going to take a of a time. Also, I already have designed for mobile and it works fine for mobile. Only desktop version did not work which now works. Ihar Aliakseyenka's answer helped.
Glad it helped :) I wasn't thinking about his approach, happy it works with your structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.