Since you can't use SASS to count elements, you can use CSS to target a parent's children based on their amount (and when/if we get a parent selector, we can target the parent too :)
Based on this solution Can CSS detect the number of children an element has? we can alter the nth-child
to target all if there is 5 or more.
In your specific case, where you want to change the justify-content
property, this won't help since that property is set on the parent, though there is other ways to create a space between flex items, i.e. auto margins, where this method can be used.
Stack snippet
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
background: blue;
height: 20px;
margin: 5px;
}
/* five or more items */
li:first-child:nth-last-child(n+5),
li:first-child:nth-last-child(n+5) ~ li {
background: red;
}
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
If that won't help, use a small script to add a class to a parent if its children is more than 4.
Stack snippet
$('ul').each(function(idx, el) {
if(el.children.length > 4) {
$(el).addClass('more_than_4');
}
});
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
background: blue;
height: 20px;
margin: 5px;
}
ul.more_than_4 {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>