8

I am using flexbox to align divs, and I want to make both postavatar and userinfo in left side and posttime to be in the right.

.postheader {
  display: flex;
  justify-content: space-between;
}

.postheader .postavatar img {
  width: 90px;
}

.postheader .userinfo {
  margin-top: 10px;
  margin-left: 20px;
}

.postheader .userinfo .postusername {
  color: #b3b3b3;
}

.postheader .posttime {
  color: #b3b3b3;
}
<div class="postheader">
  <div class="postavatar"><img src="images/avatar01.png" alt="User Image"></div>
  <div class="userinfo">
    <div class="postfullname">Fahad Aldaferi</div>
    <div class="postusername">@Q8Xbox</div>
  </div>
  <div class="posttime">24 m</div>
</div>
Stickers
  • 75,527
  • 23
  • 147
  • 186

5 Answers5

9

You can simply remove justify-content: space-between; from the flex container, and add margin-left: auto; on last flex item.

.postheader {
  display: flex;
  /*justify-content: space-between;*/
}

.postheader .postavatar img {
  width: 90px;
}

.postheader .userinfo {
  margin-top: 10px;
  margin-left: 20px;
}

.postheader .userinfo .postusername {
  color: #b3b3b3;
}

.postheader .posttime {
  color: #b3b3b3;
  margin-left: auto; /*new*/
}
<div class="postheader">
  <div class="postavatar"><img src="images/avatar01.png" alt="User Image"></div>
  <div class="userinfo">
    <div class="postfullname">Fahad Aldaferi</div>
    <div class="postusername">@Q8Xbox</div>
  </div>
  <div class="posttime">24 m</div>
</div>
Stickers
  • 75,527
  • 23
  • 147
  • 186
1

You can simply add margin-left: auto; on your .posttime, which will move the elements where you want them.

chriskirknielsen
  • 2,839
  • 2
  • 14
  • 24
0

One option: instead of justify-content:space-between;, use justify-content:flex-start;; Then, on the time, use flex-grow:1; and text-align:right;

.postheader {
    display: flex;
    justify-content: flex-start;
    width:100%;
}

.postheader .postavatar img{
    width: 90px;
}

.postheader .userinfo {
    margin-top: 10px;
    margin-left:20px;
}

.postheader .userinfo .postusername {
    color: #b3b3b3;
}

.postheader .posttime {
    color: #b3b3b3;
    flex-grow:1;
    text-align:right;
}
<div class="postheader">
        <div class="postavatar"><img src="images/avatar01.png" alt="User Image"></div>
        <div class="userinfo">
          <div class="postfullname">Fahad Aldaferi</div>
          <div class="postusername">@Q8Xbox</div>
        </div>
        <div class="posttime">24 m</div>
      </div>
Matthew Johnson
  • 4,875
  • 2
  • 38
  • 51
0

Put

<div class="postavatar"><img src="images/avatar01.png" alt="User Image"></div>

inside

<div class="userinfo">
Tobias
  • 229
  • 1
  • 2
  • 13
-1

No need to overcomplicate, just have everything you want on the left in its own div. Therefore, the flexbox will space that div (.header-left) and post-time apart from each other. You can of course use your own flexbox or other styles within .header-left to align those elements.

.postheader {
    display: flex;
    justify-content: space-between;
}

.postheader .postavatar img{
    width: 90px;
}

.postheader .userinfo {
    margin-top: 10px;
    margin-left:20px;
}

.postheader .userinfo .postusername {
    color: #b3b3b3;
}

.postheader .posttime {
    color: #b3b3b3;
}
<div class="postheader">
  <div class="header-left">
    <div class="postavatar"><img src="images/avatar01.png" alt="User Image"></div>
    <div class="userinfo">
      <div class="postfullname">Fahad Aldaferi</div>
      <div class="postusername">@Q8Xbox</div>
    </div>
  </div>
  <div class="posttime">24 m</div>
</div>
Ben Kolya Mansley
  • 1,768
  • 16
  • 24