Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use calc() in tailwind CSS?

I have this html:

  <div class="container h-screen w-screen">
    <div class="navBar h-7"></div>
    <div class="content-container"></div>
  </div>

I have set the .navBar's height to h-7. Now I want to set .content-container's height to 100vh-(h-7).

How can i use calc() to set it?

like image 721
kob003 Avatar asked Jan 31 '21 06:01

kob003


People also ask

What does CALC () do in CSS?

calc() The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length> , <frequency> , <angle> , <time> , <percentage> , <number> , or <integer> is allowed.

Can I use Calc in padding?

A solution for you would be to use CSS calc, it has good browser support and fixes your issue in quite a simple manner. The only downside here is that it doesn't calculate the padding-top in % but you simply cannot calculate padding-top in % from the height of the element unless you use javascript.

Can I use Calc for margin CSS?

You can use calc() anywhere where you would use numeric values (e.g.: width, max-height, margin-left, …) Can I Use calc? Data on support for the calc feature across the major browsers from caniuse.com.

How to use CSS calc() statements in tailwind CSS?

Now with Tailwind CSS v3.0 and above, you can use arbitrary CSS statements in your classes, and they’ll get generated using the JIT (Just-In-Time) engine. That means you can use a CSS calc () statement within a class, just by wrapping it inside square brackets. The only caveat is that CSS classes can’t contain spaces.

How do I customize the divide width values in tailwind?

To customize only the divide width values, use the theme.divideWidth section of your tailwind.config.js file. Learn more about customizing the default theme in the theme customization documentation.

What is @tailwind and how does it work?

Tailwind will automatically move any CSS within a @layer directive to the same place as the corresponding @tailwind rule, so you don’t have to worry about authoring your CSS in a specific order to avoid specificity issues.

How do I use Tailwind in my markup?

Use Tailwind’s utilities directly in your markup the way they are intended to be used, and don’t abuse the @apply feature to do things like this and you will have a much better experience. Tailwind adds a few custom functions you can use in your CSS to access Tailwind-specific values.


2 Answers

If you are using v2.x, add mode: 'jit' to your tailwind.config.js (no mode: 'jit' needed in tailwind v3)

module.exports = {
  mode: 'jit',
  ....
};

And use like this: (Without space!)

class="w-[calc(100%+2rem)]"

It will produce:

.w-\[calc\(100\%\+2rem\)\] {
  width: calc(100% + 2rem);
}

We can use the theme variables as well:

h-[calc(100%-theme(space.24))]

Tailwind v3 update

Now we can use an underscore _ instead of whitespaces:

Ref: Handling whitespace

<script src="https://cdn.tailwindcss.com"></script>
<div class="h-20 w-[calc(100%_-_10rem)] bg-yellow-200"></div>
like image 59
doğukan Avatar answered Sep 21 '22 21:09

doğukan


theme()

Use the theme() function to access your Tailwind config values using dot notation.

This can be a useful alternative to @apply when you want to reference a value from your theme configuration for only part of a declaration:

.content-container {
  height: calc(100vh - theme('spacing.7'));
}
like image 26
Digvijay Avatar answered Sep 22 '22 21:09

Digvijay