.Hi Folks Im trying to set Javascript function to align (stretch) last child in FlexBox.
To show max three columns in row i have on . article-card-container is set f
This can be solved with CSS only, and will be much more efficient than using script.
It is done by combining the nth-child
/nth-last-child
and last-child
selector, and will work on any number of items, with start from 1 item.
It work like that, when both the nth-*
and last-child
selector will match an element at the same time, the rule kicks in.
E.g., the :nth-child(3n+1):last-child
rule will target every 3rd element, starting from the 1st, then 4th, 7th and so on, if it also is the last, and with this, select any single element on the last row.
The :nth-child(3n+1):nth-last-child(2)
will do the same, though select it if it is the 2nd element, meaning there is 2 on the last row.
The final sibling selector +
will target the immediate sibling if there were 2, which always also will be the last.
Stack snippet
.content-container {
display: flex;
flex-wrap: wrap;
}
.article-card-container {
width: 33.3333%;
padding: 9px;
background: red;
border: 2px solid #fff;
box-sizing: border-box;
}
/* if every 3rd, start from 1st, also being 2nd last + last */
.article-card-container:nth-child(3n+1):nth-last-child(2),
.article-card-container:nth-child(3n+1):nth-last-child(2) + article {
width: 50%;
}
/* if every 3rd, start from 1st, also being last */
.article-card-container:nth-child(3n+1):last-child {
width: 100%;
}
/* for demo styling */
.content-container + .content-container {
margin-top: 20px;
}
<div class="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>
<div class="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>
<article class="article-card-container"></article>
</div>
<div class="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>
<article class="article-card-container"></article>
<article class="article-card-container"></article>
</div>
You have to set the flex
of lastElementChild
and lastElementChild.previousElementSibling
to 1
if leftover===2
window.onload = function alignLeftovers() {
var container = document.getElementById("content-container");
var childernCount = container.children.length;
console.log(childernCount);
var leftover = container.children.length % 3;
if (childernCount > 3 && leftover === 1) {
container.lastElementChild.style.flex = "1"
} else if (childernCount > 3 && leftover === 2) {
container.lastElementChild.style.flex = "1"
container.lastElementChild.previousElementSibling.style.flex = "1"
}
};
#content-container {
display: flex;
flex-wrap: wrap;
}
.article-card-container {
width: 33.3333%;
padding: 9px;
background: red;
border: 2px solid #fff;
box-sizing: border-box;
}
<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>
<article class="article-card-container"></article>
</div>