$(document).ready(() => { let squarePerRow = Math.floor($(".square-icons-container")[0].offsetWidth/250); $squares = $(".square-icon"); let numSquares = $squares.length; let leftOver = numSquares % squarePerRow; if (leftOver != 0) { let i = 0; let index = numSquares - leftOver; console.log($squares[index]); for (i = 0; i < leftOver; i++) { console.log($squares[index+i].parentElement); $(".square-icons-container")[0].removeChild($squares[index+i].parentElement); } } });