154

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?

doğukan
  • 23,073
  • 13
  • 57
  • 69
kob003
  • 2,206
  • 2
  • 12
  • 19

2 Answers2

344

Don't put space in calc:

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

Output:

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

Or you can use underscores _ 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>

We can use the theme variables as well:

h-[calc(100%-theme(space.24))]
doğukan
  • 23,073
  • 13
  • 57
  • 69
53

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'));
}
Digvijay
  • 7,836
  • 3
  • 32
  • 53
  • 1
    This is the exact use case I was looking for. Not sure what 'spacing.7' is exactly, but thanks! – Zack Plauché Dec 01 '21 at 15:41
  • 1
    I think the `spacing.` is relevant to `h-` for me I use h-20 so `spacing.20` worked for me – MSH Apr 30 '22 at 15:50
  • When I try putting `class="min-h-[calc(100vh - (2 * theme('spacing.3')))]"` I would expect it to evaluate as `class="min-h[calc(100vh - (2 * 0.75rem))]"`, but it doesn't, and no min-height is set. Is there a way of using the theme() function in the class definitions, rather than having to put it in standard CSS? – John Y Aug 02 '22 at 14:00