From my interpretation of the question, it seems you want to know why the elements one and two do not occupy an equal amount of space as the element with the class .damn
.
If I am correct in understanding that question, let's consider the following demonstration:
// Here we define a named function using Arrow syntax, which
// accepts a single argument:
const analyze = (element) => {
// Inside this function, we declare some variables using object destructuring
let {
width,
height,
flexGrow,
flexShrink,
flexBasis
} = window.getComputedStyle(element, null),
tempElement = document.createElement('div');
tempElement.innerHTML = generateTemplate({
width,
height,
flexGrow,
flexShrink,
flexBasis
});
element.prepend(tempElement.querySelector('ol'));
},
// Another named function utilizing Arrow syntax:
generateTemplate = (data = {}) => {
return `
<ol class="analysis-report" style="--verticalAdjustment: ${data.height}">
<li class="height">height: <var>${data.height}</var></li>
<li class="width">width: <var>${data.width}</var></li>
<li class="flexGrow">flex-grow: <var>${data.flexGrow}</var></li>
<li class="flexShrink">flex-shrink: <var>${data.flexShrink}</var></li>
<li class="flexBasis">flex-basis: <var>${data.flexBasis}</var></li>
</ol>`
}
// Selecting all div elements inside the .wrap container,
// Iterating over them using NodeList.prototype.forEach()
// and calling the analyze() function for each element:
document.querySelectorAll('.wrap > div').forEach(analyze);
To see the demonstration in action, you can check out the JS Fiddle demo.
The discrepancy in the sizing might be due to the content layout calculations performed by the browser. In cases where there is messy HTML structure within the element with the class .damn, the ability of the browser to break lines for proper formatting is limited, resulting in longer unbreakable lines dictating the overall width of the element.
However, organizing the HTML content within the .damn element for better readability and allowing new-line breaks improves the display outcome significantly. You can achieve a more balanced sizing by adjusting the flex properties accordingly. For example, setting lower values for flex on the .damn element compared to the others:
const analyze = (element) => {
let {
width,
height,
flexGrow,
flexShrink,
flexBasis
} = window.getComputedStyle(element, null),
tempElement = document.createElement('div');
tempElement.innerHTML = generateTemplate({
width,
height,
flexGrow,
flexShrink,
flexBasis
});
element.prepend(tempElement.querySelector('ol'));
},
generateTemplate = (data = {}) => {
return `
<ol class="analysis-report" style="--verticalAdjustment: ${data.height}">
<li class="height">height: <var>${data.height}</var></li>
<li class="width">width: <var>${data.width}</var></li>
<li class="flexGrow">flex-grow: <var>${data.flexGrow}</var></li>
<li class="flexShrink">flex-shrink: <var>${data.flexShrink}</var></li>
<li class="flexBasis">flex-basis: <var>${data.flexBasis}</var></li>
</ol>`
}
// Adjusting the flex properties to control the sizes of the elements
document.querySelectorAll('.wrap > div').forEach(analyze);
A practice worth considering is maintaining readable code for better troubleshooting and clarity. While in some instances, minification may be necessary, ensuring your code remains legible is essential for effective development.
For further information, refer to these resources:
References: