There are 3 blocks with a width of 8 columns ( news class) and 3 blocks with a width of 4 columns ( posts class).

With a width of less than 992 pixels, each column of the posts class needs to be aligned AFTER each column of news

And here all three elements will be aligned in one place after the second column of the news block.

Why is this happening, I do not understand.

Help please solve this problem.

Codepen link

 document.addEventListener("DOMContentLoaded", () => { let w = window.innerWidth; // ΡˆΠΈΡ€ΠΈΠ½Π° ΠΎΠΊΠ½Π° let lastPosts = document.querySelectorAll(".last_posts"); //всС ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Π²Π½ΡƒΡ‚Ρ€ΠΈ Π±Π»ΠΎΠΊΠ° posts let newsInfo = document.querySelectorAll(".news_info"); // всС ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Π²Π½ΡƒΡ‚Ρ€ΠΈ Π±Π»ΠΎΠΊΠ° news let news = document.querySelector(".news"); //Π±Π»ΠΎΠΊ news if(w <= 992){ //Ссли ΡˆΠΈΡ€ΠΈΠ½Π° мСньшС 992 Ρ‚ΠΎ for(let i =0;i < lastPosts.length;i++){ for(let j = 0;j < newsInfo.length;j++){ news.insertBefore(lastPosts[i], newsInfo[j]); //вставляСм Π² Π±Π»ΠΎΠΊ news ΠΊΠΎΠ»ΠΎΠ½ΠΊΡƒ lastPosts с [индСксом ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ массив] Π΄ΠΎ элСмСнта newsInfo[Ρ‚Π°ΠΊΠΆΠ΅ индСкс ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΡƒΠΊΠ°Π·Ρ‹Π²Π°Π΅Ρ‚ массив] } } } }) //аналогичная Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ с resize ΠΌΠΎΠΆΠ΅Ρ‚Π΅ Π½Π΅ ΠΏΠΎΡΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ Π΄Π°ΠΆΠ΅ window.onresize = () => { let w = window.innerWidth; let lastPosts = document.querySelectorAll(".last_posts"); let newsInfo = document.querySelectorAll(".news_info"); let news = document.querySelector(".news"); let posts = document.querySelector(".posts"); if(w <= 992){ for(let i =0;i < lastPosts.length;i++){ for(let j = 0;j < newsInfo.length;j++){ news.insertBefore(lastPosts[i], newsInfo[j]); } } } else if (w > 992){ for(let i =0;i < lastPosts.length;i++){ posts.insertBefore(lastPosts[i], null); } } } 
 <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="container no-padding"> <div class="row"> <div class="news col-12 col-sm-12 col-md-12 col-lg-8 col-xl-8"> <h6 class="news_title">News</h6> <div class="news_info"> <div class="statue_liberty"> <img src=""> </div> <div class="post_title">Discover Milan's religious sites</div> <p class="post_briefly">Canadian-born and educated Jamie Fobert has practiced architecture in England for 30 years, first under David Chipperfield and then, since 1995, as head of his own firm. During that time, he has quietly built a…</p> <div class="button-container-4"> <span class="mas_">Read more &rarr;</span> <button type="button" name="Hover">Read more &rarr;</button> </div> </div> <div class="news_info"> <div class="statue_liberty"> <img src=""> </div> <div class="post_title">Discover Milan's religious sites</div> <p class="post_briefly">Canadian-born and educated Jamie Fobert has practiced architecture in England for 30 years, first under David Chipperfield and then, since 1995, as head of his own firm. During that time, he has quietly built a…</p> <div class="button-container-4"> <span class="mas_">Read more &rarr;</span> <button type="button" name="Hover">Read more &rarr;</button> </div> </div> <div class="news_info"> <div class="statue_liberty"> <img src=""> </div> <div class="post_title">Discover Milan's religious sites</div> <p class="post_briefly">Canadian-born and educated Jamie Fobert has practiced architecture in England for 30 years, first under David Chipperfield and then, since 1995, as head of his own firm. During that time, he has quietly built a…</p> <div class="button-container-4"> <span class="mas_">Read more &rarr;</span> <button type="button" name="Hover">Read more &rarr;</button> </div> </div> </div> <div class="posts col-12 col-sm-12 col-md-12 col-lg-4 col-xl-4"> <div class="last_posts"> <h3 class="last_post_title">Discover Milan's religious sites</h3> <p class="latest_post_briefly">Canadian-born and educated Jamie Fobert has practiced architecture in England for 30 years, first …</p> <a href="javascript:void(0)">Read</a> </div> <div class="last_posts"> <h3 class="last_post_title">Discover Milan's religious sites</h3> <p class="latest_post_briefly">Canadian-born and educated Jamie Fobert has practiced architecture in England for 30 years, first …</p> <a href="javascript:void(0)">Read</a> </div> <div class="last_posts"> <h3 class="last_post_title">Discover Milan's religious sites</h3> <p class="latest_post_briefly">Canadian-born and educated Jamie Fobert has practiced architecture in England for 30 years, first …</p> <a href="javascript:void(0)">Read</a> </div> </div> </div> </div> 

    1 answer 1

    Most likely you have an error in the logic of the cycle:

     for(let i =0;i < lastPosts.length;i++){ for(let j = 0;j < newsInfo.length;j++){ news.insertBefore(lastPosts[i], newsInfo[j]); } } 

    inside your nested loop, you insert the same element first before newsInfo[0] , then the value of i does not change and you insert the same lastPost[i] element (move) just before newsInfo[1] and so on .. that is, the same block is stupidly moved several times, and at the end of the nested loop it remains before the last newsInfo[]

    As far as I understand you, the number of lastPost is equal to the number of newsInfo, then you, if I understand your logic, you need to use one cycle, but this will be only if their number coincides, otherwise you must add more checks

     for(let i =0;i < lastPosts.length;i++){ news.insertBefore(lastPosts[i], newsInfo[i]); } 
    • Ummm, where is the answer? - Demon __ ANT
    • so I answered the question posed: "Why does the loop insertBefore add all three elements to one place?" Because you do not go out of the cycle where necessary - Sergei Petrashko
    • need to resolve this issue - Demon __ ANT
    • people .. programmers get s / n for what they do themselves. not everyone has time to solve the problem. but you can tell where the error is - Sergey Petrashko
    • thanks for the help - Demon __ ANT