DoeyFoey DoeyFoey - 2 months ago 8
CSS Question

Alternate box position - CSS/Bootstrap

I created a Bootstrap 4 script here:

http://jsfiddle.net/aftu6ehL/

How is it possible to alternate left/right alignment of the boxes. I.e. I want to switch each second box the left and right side..

Description - Image
Image - Description
Description - Image
Image - Description
Description - Image
Image - Description


nth-child(3n+1) somehow didn't seem to work.

Answer

You want to use 2n + 1 (every odd value)

.container {
  width: 200px;
}

.image, .desc{
  width: 50%;
  height: 50px;
  display: inline-block;
}

.image {
  background: red;
  float: left;
}

.desc {
  background: blue;
  float: right;
}

.container:nth-child(2n + 1) .image {
  float:right;
}
  <div class="container">
    <div class="image">
      Image
    </div>
    <div class="desc">
      Description
    </div>
  </div>
  <div class="container">
    <div class="image">
      Image
    </div>
    <div class="desc">
      Description
    </div>
  </div>
  <div class="container">
    <div class="image">
      Image
    </div>
    <div class="desc">
      Description
    </div>
  </div>