Answer the question
In order to leave comments, you need to log in
How to add a class to every second line of a div with a picture?
Good day!
The page contains a list of news.
In the list of news, each news has an image and a description.
<div class="row align-items-center">
<div class="col-md-6">Изображение</div>
<div class="col-md-6">Текст</div>
</div>
<div class="row align-items-center">
<div class="col-md-6">Изображение</div>
<div class="col-md-6">Текст</div>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
try one class and pseudo-classes :nth-child(even) / :nth-child(odd)
const selector = '.row.align-items-center:nth-child(2n) .col-md-6:nth-child(2n)';
const divList = document.querySelectorAll(selector);
divList.forEach(div => div.classList.add('order-md-2'));
Thank you very much for the answers.
Managed to do this:
<div class="row align-items-center img__location">
<div class="col-md-6">Изображение</div>
<div class="col-md-6">Текст</div>
</div>
<div class="row align-items-center img__location">
<div class="col-md-6">Изображение</div>
<div class="col-md-6">Текст</div>
</div>
</div>
.img__location:nth-child(2n)>.col-md-6:first-child {
order: 2;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question