Content width + some amount of px with calc()
Asked Answered
G

3

18

I know i can do calc(100% + 15px), but is there way to do calc("content width" + 15px)?

fit-content and auto didn't work, which kinda makes sense.

I have unknown number of items that i want just to take width they need (inline-block) + some amount of px for justify-content: space-between;

If this can't be done with calc, is there an alternative?

(I'm using scss if that changes anything)

Gore answered 15/1, 2019 at 14:0 Comment(5)
If they are inline block can't you just add padding?Weekend
Can you share your use case, I am pretty sure there is an easy way to what you wantFerretti
Yeah, how about using a CS table and border-spacing. jsfiddle.net/MrLister/s91254cg/3Debouchment
Yeah, i totally overlooked :not(:last-child) selector and padding-right and narrowed it to usage of calc() and flexbox. Your comment shifted me to the right direction. But i still wonder if it is possible to do such thing with calc(), without js and accessing the dom.Gore
Good question. Any solution for this?Dday
L
5

Use padding. The content+padding => determines size without using % percentage for width.

In my scenario, a button added a '>' using pseudo ::after, and was positioned 18px from the right edige.

Legion answered 23/4, 2020 at 19:5 Comment(1)
Any code sample ?Limitative
A
0

I also found myself in this situation, unfortunately not having found a solution I used a small JS function that assigns the value

width: calc ($(element).width() + 10px);

Unfortunately, rather than calculating the measure via javascript when loading the page I can not help you.

Andrey answered 15/1, 2019 at 14:5 Comment(0)
L
0

Have you try the following ?

width: max-content;

It works on my case. I have a <div> whose children somehow wrapped. Somehow fit-content nor auto did not work. But width: max-content; works.

Limitative answered 21/9, 2023 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.