I need to obtain max-width
in px
to use it in a Javascript code but that may be defined in other units in CSS.
Do you have any ideas on how to obtain that using JS?
const ele = document.querySelector("div")
const eleMaxWidth = window.getComputedStyle(ele).maxWidth
console.log(eleMaxWidth)
div {max-width: 50%}
<div>Lorem, ipsum.</div>
2
Answers
You can use
offsetWidth
to get the total width of the parent div in pixels, and then derive the max-width of the needed div by multiplying it by the percentage you got earlier:Just multiply the percentage value by the width of its parent element.