-1

How to position the elements which are even to the right? I've tried nth-child(even); and float: right; but it's not working.

#container {
  grid-template-columns: repeat(1,minmax(0px,1fr));
  grid-column-gap: 60px;
  grid-row-gap: 20px;
  text-align: left;
  display: grid;
  width: 100vw;
}

.items {
  width: 80%;
  height: 90px;
  background-color: blue;
}

.items:nth-child(even) {
  float: right;
}
<div id="container">
  <div class="items"></div>
  <div class="items"></div>
  <div class="items"></div>
  <div class="items"></div>
</div>

3 Answers3

1

Just change float:right to justify-self: end;

#container {
  grid-template-columns: repeat(1,minmax(0px,1fr));
  grid-column-gap: 60px;
  grid-row-gap: 20px;
  text-align: left;
  display: grid;
  width: 100vw;
}

.items {
  width: 80%;
  height: 90px;
  background-color: blue;
}

.items:nth-child(even) {
  justify-self: end;
}
<div id="container">
  <div class="items"></div>
  <div class="items"></div>
  <div class="items"></div>
  <div class="items"></div>
</div>
Yadab
  • 1,767
  • 1
  • 10
  • 16
0
#container {
  grid-template-columns: repeat(1,minmax(0px,1fr));
  grid-column-gap: 60px;
  grid-row-gap: 20px;
  text-align: left;
  width: 100vw;
  overflow: hidden;
}

.items {
  margin-bottom: 10px;
  width: 80%;
  height: 90px;
  background-color: blue;
  overflow: hidden;
}

.items:nth-child(even) {
  float: right;
}
Vishal_VE
  • 1,852
  • 1
  • 6
  • 9
-1

use justify-self: end; on .items:nth-child(even).

#container {
  grid-template-columns: repeat(1,minmax(0px,1fr));
  grid-column-gap: 60px;
  grid-row-gap: 20px;
  text-align: left;
  display: grid;
  width: 100vw;
}

.items {
  width: 80%;
  height: 90px;
  background-color: blue;
}

.items:nth-child(even) {
  justify-self: end;
}
<div id="container">
  <div class="items"></div>
  <div class="items"></div>
  <div class="items"></div>
  <div class="items"></div>
</div>
TechySharnav
  • 4,869
  • 2
  • 11
  • 29