I have a tooltip implementation that uses some CSS variables to customize. I'm using CSS calc to calculate arrow position inside like below:
--size: 16px;
--border-size: 1px;
--arrow-position: calc((var(--size) / -2) - var(--border-size));
Result is be -8px
with the values above and it works well. But if I want to have a borderless tooltip and set --border-size: 0
(without unit) this doesn't work. I made a demonstration below.
There are some ways that I know to set a unit for a value in CSS like calc(var(--border-size) * 1px)
but this doesn't work as well once --border-size
is a value with unit, because one side of the multiplication should be always unitless.
I also considered using new @property
but couldn't manage with it as well.
I'm targeting to write a calculation that will cover border size both unitless and with a unit, without asking extra variable to set. Any idea how to do that?
div {
--size: 16px;
--border-size: 1px;
--arrow-position: calc((var(--size) / -2) - var(--border-size));
--bg-color: white;
border: var(--border-size) solid black;
padding: var(--size);
background-color: var(--bg-color);
position: absolute;
top: 120px;
left: 120px;
width: 200px;
}
div::before {
content: '';
box-sizing: border-box;
display: block;
position: absolute;
top: var(--arrow-position);
width: var(--size);
height: var(--size);
transform: rotate(45deg);
border: var(--border-size) solid black;
background-color: var(--bg-color);
border-bottom: none;
border-right: none;
}
div.no-border {
--border-size: 0;
--bg-color: gray;
top: 200px;
}
div.with-unit {
--border-size: 0px;
top: 280px;
}
<div>
Tooltip Content
</div>
<div class="no-border">
Tooltip Without Borders
</div>
<div class="no-border with-unit">
Tooltip Without Borders with Unit
</div>
EDIT: Code above is for demonstrating the issue in a simple way. The real use case is a web component. You can check it here: https://github.com/Trendyol/baklava/pull/483
EDIT: The difference in this question from Why doesn't css-calc() work when using 0 inside the equation? is I already know the reason why it doesn't work. Instead, I'm looking for workarounds for it.