.Hi Folks Im trying to set a Javascript function to align (stretch) the last child in a FlexBox. To show a maximum of three columns per row, I have. item-card-container is installed flex: 1 1 calc(100 * (1/3));
.
To stretch the last element, I need to set it flex: 1;
<div id="content-container">
<article class="article-card-container"></article>
<article class="article-card-container"></article>
<article class="article-card-container"></article>
<article class="article-card-container"></article>
</div>
Here is my solution that works, BUT ...
window.onload = function alignLeftovers() {
var container = document.getElementById("content-container");
var childernCount = container.children.length;
var leftover = container.children.length % 3;
if (childernCount > 3 && leftover === 1 ) {
for (var i = 0; i < container.children.length; i++) {
container.lastElementChild.classList.remove('article-card-container');
}
container.lastElementChild.classList.add('article-card-container-100');
}
};
Im using leftover === 1
for the test because there are two remaining elements and leftover > 0
cause problems.
The problem with this function is that I had to write a new class to be assigned (including all subclasses for internal elements = a lot of code).
The first condition in the expression is if
used because it modulus
returned 1, there were even only two elements.
(Im new JS) JavaScript, .article-card-container
"flex" ( ) ?
,