2

Following is my code in which I am trying to align the last div (class="four") to the right and I am using align-self: flex-end; but still its not going to the right. Let me know what I am doing wrong here.

.container {
  display: flex;
  flex-direction: row;
  border: 2px solid blue;
}

.one {
  background: red;
}

.two {
  background: yellow;
}

.three {
  background: pink;
}

.four {
  background: teal;
  display: inline-block;
  align-self: flex-end;
}
<div class="container">
  <div class="one">One</div>
  <div class="two">Two</div>
  <div class="three">Three</div>
  <div class="four">Four</div>
</div>
Nesh
  • 2,164
  • 6
  • 26
  • 47

4 Answers4

5

margin-left:auto; will do the job.

One use of auto margins in the main axis is to separate flex items into distinct "groups"...

.container {
  display: flex;
  flex-direction: row;
  border: 2px solid blue;
}

.one {
  background: red;
}

.two {
  background: yellow;
}

.three {
  background: pink;
}

.four {
  background: teal;
  display: inline-block;
  margin-left:auto;
}
<div class="container">
  <div class="one">One</div>
  <div class="two">Two</div>
  <div class="three">Three</div>
  <div class="four">Four</div>
</div>
Abhishek Pandey
  • 12,759
  • 8
  • 35
  • 63
  • Thanks for the help, but any specific reason why we need to add `margin-left:auto;` in this case ? Let me know if I am missing something here conceptually. – Nesh Nov 12 '19 at 05:37
  • I have mentioned the link, you can read there more about it. – Abhishek Pandey Nov 12 '19 at 05:38
3

use margin-left: auto

.container {
  display: flex;
  flex-direction: row;
  border: 2px solid blue;
}

.one {
  background: red;
}

.two {
  background: yellow;
}

.three {
  background: pink;
}

.four {
  background: teal;
  display: inline-block;
margin-left: auto;
}
<div class="container">
  <div class="one">One</div>
  <div class="two">Two</div>
  <div class="three">Three</div>
  <div class="four">Four</div>
</div>
Samuel Goldenbaum
  • 17,413
  • 15
  • 57
  • 99
  • Thanks for the help, but any specific reason why we need to add margin-left:auto; in this case ? Let me know if I am missing something here conceptually. – Nesh Nov 12 '19 at 05:37
1

Align self property is used to adjust the flex items on the cross axis.

Please try this code.

.container {
  display: flex;
  flex-direction: row;
  border: 2px solid blue;
}

.one {
  background: red;
}

.two {
  background: yellow;
}

.three {
  background: pink;
}

.four {
  background: teal;
  margin-left: auto;
}
<div class="container">
  <div class="one">One</div>
  <div class="two">Two</div>
  <div class="three">Three</div>
  <div class="four">Four</div>
</div>
Yudiz Solutions
  • 3,473
  • 1
  • 5
  • 20
-1

Another way to do.

.container {
  display: flex;
  flex-direction: row;
  border: 2px solid blue;
  position: relative;
}

.one {
  background: red;
}

.two {
  background: yellow;
}

.three {
  background: pink;
}

.four {
  background: teal; 
  right: 0;
  position: absolute;
}
<div class="container">
  <div class="one">One</div>
  <div class="two">Two</div>
  <div class="three">Three</div>
  <div class="four">Four</div>
</div>
akhtarvahid
  • 8,226
  • 2
  • 18
  • 27