How to bottom-align an element inside a flex item?

前端 未结 2 988
醉梦人生
醉梦人生 2020-12-21 09:36

I have a flex container with three flex items and inside each item a link that should be aligned on the bottom (all links bottom middle aligned).

Flex-items are str

相关标签:
2条回答
  • 2020-12-21 09:54

    Put position: absolute; on your a tag and position: relative; to your flex-item:

    .flex-container {
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      -webkit-flex-direction: row;
      -ms-flex-direction: row;
      flex-direction: row;
      -webkit-flex-wrap: nowrap;
      -ms-flex-wrap: nowrap;
      flex-wrap: nowrap;
      -webkit-justify-content: center;
      -ms-flex-pack: center;
      justify-content: center;
      -webkit-align-content: flex-start;
      -ms-flex-line-pack: start;
      align-content: flex-start;
      -webkit-align-items: flex-start;
      -ms-flex-align: start;
      align-items: flex-start;
    }
    
    .flex-item {
      -webkit-order: 0;
      -ms-flex-order: 0;
      order: 0;
      -webkit-flex: 1 0 0;
      -ms-flex: 1 0 0;
      flex: 1 0 0;
      -webkit-align-self: stretch;
      -ms-flex-item-align: stretch;
      align-self: stretch;
      padding: 15px;
      position: relative;
    }
    
    .flex-item a {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      text-align: center;
    }
    <section class="main-section">
            <div class="container">
                <div class="flex-container">
                    <div class="flex-item">
                        <p class="subTitle">Titel</p>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim ante turpis, sit amet suscipit velit consequat id. Donec a est tellus. Sed pellentesque non arcu et interdum. Aenean venenatis, ipsum a viverra interdum, neque nisl tincidunt arcu, non vulputate justo metus nec quam. Nullam blandit, purus id pretium congue, turpis diam semper sapien, vel suscipit est velit eget leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent consequat urna quis commodo varius. Praesent hendrerit at augue in fermentum. Ut sed dolor blandit, lacinia orci eget, ultrices elit. Nulla luctus, risus eu viverra eleifend, leo orci posuere nulla, ut vestibulum orci metus in nisi. Aenean et porttitor lacus.</p>
                        <a href="#">Link</a>
                    </div>
                    <div class="flex-item">
                        <p class="subTitle">Titel</p>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim ante turpis, sit amet suscipit velit consequat id. Donec a est tellus.</p>
                        <a href="#">Link</a>
                    </div>
                    <div class="flex-item">
                        <p class="subTitle">Titel</p>
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim ante turpis, sit amet suscipit velit consequat id. Donec a est tellus. Sed pellentesque non arcu et interdum. Aenean venenatis, ipsum a viverra interdum, neque nisl tincidunt arcu, non vulputate justo metus nec quam. Nullam blandit, purus id pretium congue, turpis diam semper sapien, vel suscipit est velit eget leo.</p>
                        <a href="#">Link</a>
                    </div>
                </div>
            </div>
        </section>

    0 讨论(0)
  • 2020-12-21 10:04

    First, remove align-items: flex-start from the container. That's overriding the stretch default, so the columns are not stretching the full height of the container. They are only as high as their content.

    Second, make your flex items into (nested) flex containers with flex-direction: column. You can then use an auto margin on the link to fix it to the bottom.

    .flex-container {
      display: flex;
      flex-direction: row;
      flex-wrap: nowrap;
      justify-content: center;
      align-content: flex-start;
      /* align-items: flex-start; <--------- REMOVE */
    }
    
    .flex-item {
      flex: 1 0 0;
      padding: 15px;
      
      display: flex;                /* NEW */
      flex-direction: column;       /* NEW */
    }
    
    .flex-item > a {
      margin-top: auto;             /* NEW */
      align-self: center;           /* NEW (optional; horizontal centering) */
    }
    <section class="main-section">
      <div class="container">
        <div class="flex-container">
          <div class="flex-item">
            <p class="subTitle">Titel</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim ante turpis, sit amet suscipit velit consequat id. Donec a est tellus. Sed pellentesque non arcu et interdum. Aenean venenatis, ipsum a viverra interdum, neque nisl tincidunt
              arcu, non vulputate justo metus nec quam. Nullam blandit, purus id pretium congue, turpis diam semper sapien, vel suscipit est velit eget leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent
              consequat urna quis commodo varius. Praesent hendrerit at augue in fermentum. Ut sed dolor blandit, lacinia orci eget, ultrices elit. Nulla luctus, risus eu viverra eleifend, leo orci posuere nulla, ut vestibulum orci metus in nisi. Aenean et
              porttitor lacus.</p>
            <a href="#">Link</a>
          </div>
          <div class="flex-item">
            <p class="subTitle">Titel</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim ante turpis, sit amet suscipit velit consequat id. Donec a est tellus.</p>
            <a href="#">Link</a>
          </div>
          <div class="flex-item">
            <p class="subTitle">Titel</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dignissim ante turpis, sit amet suscipit velit consequat id. Donec a est tellus. Sed pellentesque non arcu et interdum. Aenean venenatis, ipsum a viverra interdum, neque nisl tincidunt
              arcu, non vulputate justo metus nec quam. Nullam blandit, purus id pretium congue, turpis diam semper sapien, vel suscipit est velit eget leo.</p>
            <a href="#">Link</a>
          </div>
        </div>
      </div>
    </section>

    revised fiddle

    More info on flex auto margins:

    • In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
    0 讨论(0)
提交回复
热议问题