M
M
malayamarisha2020-05-13 12:25:36
css
malayamarisha, 2020-05-13 12:25:36

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>

Tell me, please, how to add the .order-md-2 class to every second line of a div with a picture.
It is necessary for the images to be displayed as if in a checkerboard pattern.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
shsv382, 2020-05-13
@malayamarisha

try one class and pseudo-classes :nth-child(even) / :nth-child(odd)

A
Andrey Pastukhov, 2020-05-13
@tyllo

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'));

M
malayamarisha, 2020-05-13
@malayamarisha

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 question

Ask a Question

731 491 924 answers to any question