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?
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.
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.
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.
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.
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.
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.
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.
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))]
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>
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'));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With