Css - Use Calc() To Keep Widths Of Elements The Same
Solution 1:
Use CSS variables:
<style>:root { --width: 100px; } /* Set the variable --width */tabletd{
width: var(--width); /* use it to set the width of TD elements */border: 1px solid;
}
</style><table><tr><tdclass="tab">elementOne</td><tdclass="tab">elementtwo</td></tr></table><!-- reuse the variable to set the width of the DIV element --><divstyle="width: var(--width); border: 1px solid;">Element three</div>
You can also use variables in the calc() function:
<divstyle="width: calc(var(--width) * 3); border: 1px solid;">Element four</div>
Solution 2:
No, it's not possible with CSS.
For that you will have to use JavaScript (document.getElementById(elementOne).offsetWidth
or similar, depending on exactly what width you are looking for). Calc is used to do math, not execute scripts. There is no way of putting JS in a CSS statement, like you are trying to do.
For more help on the JS bit, see How do I retrieve an HTML element's actual width and height?
Edit: For some background on why this would be a bad idea to implement in CSS, se Value calculation for CSS (TL;DR: It has been tried. Things exploded)
Solution 3:
Yes. There are 2 possible ways of doing this with CSS (although not with calc):
1) If you know how many columns you have, then just use % (i.e. 50% for 2 cols)
2) If you don't know how many columns you have or you maybe can't use % for whatever use-case you might have, then you can use flexbox. Depending on your browser compatibility target, you can combine the "old" and "new" syntax to get some awesome results.
elementOne | elementtwo | Element?
I have a table and I need the cells to have an element posi…
Here's a js fiddle of what I currently have, I am tryin…
So I have a few pictures that I want to randomly generate o…
This is my first question. I usually try to research these …
Right Floated Element Disappears When Using Columns In Firefox
I am using an ol element with column-count and column-gap p…
Setting Link Href From Child Image Src Using JQuery
I am using .wrap() to create a link around each image withi…
How To Limit The Html Table Records In Each Page
I am populating a table in my jsp which has more than 20 re…
I want to show countdown in each of my divs. Right now I ge…
Retain Dynamically Created DropDown List's Selected Value On Event Window.history.back()- JavaScript
Can use the below question as a reference to explain the co…
|
Post a Comment for "Css - Use Calc() To Keep Widths Of Elements The Same"