Is there a way to set padding-left and padding-right to 10% of element width in Flexbox.
I tried with padding: 0 10%;
, but its not 10% of element width.
.flex {
display: flex;
}
.item1 {
padding: 0 3px;
/*calculated depending on text width*/
}
.item2 {
padding: 0 16px;
/*calculated depending on text width*/
}
.item3 {
padding: 0 34px;
/*calculated depending on text width*/
}
/*can it be done with one rule for items*/
.item {
/*padding: 0 (width/10); something like this*/
}
<div class="flex">
<div class="item1">itm1</div>
<div class="item2">item2 with greater width</div>
<div class="item3">item3 with the greatest width ....................................</div>
</div>
EDIT: I solved it with javascript, but can be solved without using js? I just want to set padding to some percentage of text width.
(() => {
const items = document.getElementsByClassName('item');
for (let item of items) {
item.style.padding = `0 ${item.offsetWidth / 10}px`;
}
})();
.flex {
display: flex;
}
.item {
border: 2px solid red;
white-space: nowrap;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="flex">
<div class="item">itm1</div>
<div class="item">item2 with greater width</div>
<div class="item">item3 with the greatest width ....................................</div>
</div>
</body>
</html>