How to use calc() in TailwindCSS
/* Set top margin depending on screen height */
<div class="mt-[calc(100vh_/_5)]">
calc()
is a commonly used CSS function. It's useful if you want to dynamically change the position of individual components.
For TailwindCSS it is very important that space characters are not allowed in the expression. So, unlike normal CSS, you either have to type everything without space or use underscore characters instead of space characters.
So calc(100vh / 5)
becomes calc(100vh/5)
or calc(100vh_/_5)
For example, if you want a modal dialogue window to be displayed closer to the top of the screen than in the middle. Since we don't know the client screen resolution in advance, the value of the top margin has to be calculated dynamically.
<div class="fixed bg-slate-400 bg-opacity-70 top-0 bottom-0 left-0 right-0 flex justify-center">
<div class="p-5 bg-white rounded w-96 h-24 shadow mt-[calc(100vh_/_5)]">Dialog</div>
</div>
The result is:
- Create blinking background with TailwindCSS
- Create a neon effect with TailwindCSS
- Rotate element with TailwindCSS
- Change SVG icon color with TailwindCSS
- Using :not in TailwindCSS
- Specify exact width value in TailwindCSS
- Set element width over 100% using Tailwind CSS
- Stretch children to fill horizontally
- Create a fixed navbar with TailwindCSS
- Use font from local files in TailwindCSS
- Make an element invisible in mobile size in Tailwind CSS
- Create a 20/80 grid with Tailwind CSS
- Select the nth child in TailwindCSS
- Add class to child when hovering on parent in TailwindCSS
- Fill the full height of the screen in TailwindCSS
- Create transition with TailwindCSS
- Create horizontal rule with text using TailwindCSS
- Set background image with TailwindCSS
- How to use CSS variables in TailwindCSS
- Center an absolute element in TailwindCSS
- Remove arrows from number input in TailwindCSS
- How to use !important in TailwindCSS
- Access child elements in TailwindCSS
- Select first child in TailwindCSS
- Create semi transparent background in TailwindCSS
- How to use calc() in TailwindCSS
- Add text shadow with Tailwind CSS