1

For a layout, I need to have three flex items (rows) in a flex container, and I want them justified as space-between... The first row will contain some cloud tags, the second a price, and the third a Read more link.

But there will be cases where for specific items, only the last row (the Read more) will need to exist.

So, in those cases, for uniformity, I want the Read more link to be placed at the bottom of the container; but space-between doesn't help much towards that approach...

What can I do to have a fallback justify-content property set to end for when there is only one child item?

.container {
  background-color: #aaa;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-end;
  height: 200px;
  margin-bottom: 20px;
}

.tags {
  display: flex;
}

.tags span {
  background-color: #f0f;
  padding: 10px;
  margin: 0 0 0 10px;
}

.price {
  display: flex;
  background-color: #ff0;
  padding: 10px;
  font-size: 150%
}

.read-more {
  display: flex;
  background-color: #0ff;
  padding: 10px;
}
<div class="container">
  <div class="tags">
    <span>tag 1</span><span>tag2</span><span>tag 3</span>
  </div>
  <div class="price">
    $100
  </div>
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>

<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>
Faye D.
  • 833
  • 1
  • 3
  • 16
  • Instead of `justify-content` on the container, use `auto` margins on the items. https://jsfiddle.net/73hkugaq/ (The explanation is in the duplicate.) – Michael Benjamin Nov 04 '22 at 01:54

2 Answers2

1

If you are able/willing to change the order of the flex-items in your HTML code, you can reverse them there and use flex-direction: column-reverse; on the container. That way the "read more" element is the first flex-item and due to the reversed direction at the bottom of the container:

.container {
  background-color: #aaa;
  display: flex;
  flex-direction: column-reverse;
  justify-content: space-between;
  align-items: flex-end;
  height: 200px;
  margin-bottom: 20px;
}

.tags {
  display: flex;
}

.tags span {
  background-color: #f0f;
  padding: 10px;
  margin: 0 0 0 10px;
}

.price {
  display: flex;
  background-color: #ff0;
  padding: 10px;
  font-size: 150%
}

.read-more {
  display: flex;
  background-color: #0ff;
  padding: 10px;
}
<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
  <div class="price">
    $100
  </div>
  <div class="tags">
    <span>tag 1</span><span>tag2</span><span>tag 3</span>
  </div>
</div>

<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>
Johannes
  • 64,305
  • 18
  • 73
  • 130
-1

You can give the container a position: relative; tag, the .read-more class, and the :only-child pseudo class. Then when it is the only child in the container, it will add the attributes position:absolute; bottom:0; right:0; to it.

This moves it to the bottom right of the container. The justify-content: end !important; doesn't move the container to where you want it to be.

Example:

.container {
  background-color: #aaa;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-end;
  height: 200px;
  margin-bottom: 20px;
  position:relative;
}

.tags {
  display: flex;
}

.tags span {
  background-color: #f0f;
  padding: 10px;
  margin: 0 0 0 10px;
}

.price {
  display: flex;
  background-color: #ff0;
  padding: 10px;
  font-size: 150%
}

.read-more {
  display: flex;
  background-color: #0ff;
  padding: 10px;
}

.read-more:only-child{
    position:absolute;
    bottom:0;
    right:0;
}
<div class="container">
  <div class="tags">
    <span>tag 1</span><span>tag2</span><span>tag 3</span>
  </div>
  <div class="price">
    $100
  </div>
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>

<div class="container">
  <div class="read-more">
    <a href="#">Read more >></a>
  </div>
</div>
James Risner
  • 5,451
  • 11
  • 25
  • 47
Z2r
  • 82
  • 1
  • 8