180

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?

2 Answers 2

391

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))]
3
  • 13
    It is also possible to omit the whitespaces like so w-[calc(100%-10rem)]
    – edta
    Apr 8, 2022 at 16:31
  • 14
    Remember guys, without space!
    – Ray Jasson
    May 6, 2022 at 20:06
  • 2
    If you want to add this to the tailwind config file, then you should use spaces instead of underscores when extending the theme. May 28, 2022 at 19:31
55

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'));
}
4
  • 1
    This is the exact use case I was looking for. Not sure what 'spacing.7' is exactly, but thanks! Dec 1, 2021 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, 2022 at 15:50
  • @JohnY Brother you should not put spaces in your class string. it is mentioned many times in this answer.
    – Shadow_m2
    Oct 18 at 8:57
  • A bit late to the party but "spacing" is the unit in tailwind used for all padding, margin, gap, height, width, etc. In case other people reading this are wondering. Nov 12 at 0:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.