Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tailwind CSS - how to make a grid with two columns where the 1st column has 20% of the width and 2nd one 80% width?

From the official documentation, I am only able to come up with something like this:

<div class="grid grid-cols-2 gap-3">
  <div>1st col</div>
  <div>2nd col</div>
</div>

But this gives me 2 columns with an equal width - how do I specify that the first column would be like 20% of the total width (I only need to place there a simple icon) and the rest of the width would be the second column (here would be a text)?

Thank you in advance.

like image 444
user984621 Avatar asked Apr 24 '21 11:04

user984621


People also ask

How do you split columns in Tailwind?

Make your div into grid and use grid-col-2 to divide it into two parts. That's about it.

How do you change the grid width of a column in CSS?

Set the following on the grid container: grid-template-columns: auto 1fr; This sets the width of the first column to equal the width of the widest item in that column, and the width of the second column to get the remaining width of the grid.

How do you use percentage in Tailwind?

You can use them by appending square brackets containing a CSS value (e.g. [80%] ) to a prefix such as p- for padding or mr- for margin-right. Show activity on this post. theme: { extend: { spacing: { '80%': '80%', // p-80% - should work } }, }, The class Tailwind will create is .


Video Answer


3 Answers

Set grid-cols-5 to the wrapper and col-span-4 to second column. It will cover 4/5 (80%)

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.1.2/tailwind.min.css" />

<div class="grid grid-cols-5 gap-3">
  <div class="bg-blue-100">1st col</div>
  <div class="bg-red-100 col-span-4">2nd col</div>
</div>

Another way with grid-flow-col

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.1.2/tailwind.min.css" />

<div class="grid grid-flow-col gap-3">
  <div class="bg-blue-100 col-span-1">1st col</div>
  <div class="bg-red-100 col-span-4">2nd col</div>
</div>
like image 57
doğukan Avatar answered Oct 23 '22 12:10

doğukan


You can define additional utilities by extending the theme in tailwind.config.js:

module.exports = {
  theme: {
    extend: {
      gridTemplateColumns:
      {
        '20/80': '20% 80%',
        'fixed': '40px 260px',
      }
    }
  }
}
<div class="grid grid-cols-20/80 w-full h-64">
  <div class="bg-blue-500"></div>
  <div class="bg-red-500"></div>
</div>

<div class="grid grid-cols-fixed h-64">
  <div class="bg-blue-500"></div>
  <div class="bg-red-500"></div>
</div>

Updated for Tailwind CSS 3:

With the introduction of the JIT compiler and the ability to use arbitrary/dynamic values in some utilities, you can now do this without the config:

<div class="grid grid-cols-[20%_80%] w-full h-64">
<div class="grid grid-cols-[40px_260px] w-full h-64">
like image 45
HorusKol Avatar answered Oct 23 '22 13:10

HorusKol


You can use col-span like below

    <div class="grid grid-cols-5 gap-3"> // This will create 5 grids so 20% each
      <div class="some-class"></div>
      <div class="col-span-4"></div> // This will take 80% of space
    </div>

Reference: https://tailwindcss.com/docs/grid-column#class-reference

like image 36
Zuber Avatar answered Oct 23 '22 14:10

Zuber