简体   繁体   English

如何在弹性商品中获取商品以拉伸100%的父商品?

[英]How can I get an item inside a flex item to stretch 100% of the parent?

I have a group of cards that are all in a flex container so that they stretch out the same height. 我有一组卡都放在弹性容器中,以便它们伸展到相同的高度。 How can I make the card__content stretch the remaining height so that the date will be at the bottom of the card? 如何使卡片__内容拉伸剩余的高度,以便日期位于卡片的底部? I've tried adding height: 100%; 我尝试增加身高:100%; which isn't doing anything. 什么也没做。 Why's this? 怎么会这样 What am I not understanding? 我不明白什么?

Html and css below HTML和CSS下面

<div class="wrapper">
    <article class="card">
        <a class="card-link" href="www.test.com">
           <img class="card__image" src="card-image.jpg" />
          <div class="card__content">
            <h2 class="h5">This is a news</h2>
            <small class="card__date">30 May 2019</small> 
          </div>
        </a>
    </article>
    <article class="card">
        <a class="card-link" href="www.test.com">
           <img class="card__image" src="card-image.jpg" />
          <div class="card__content">
            <h2 class="h5">This is another news</h2>
            <small class="card__date">30 May 2019</small> 
          </div>
        </a>
    </article>
    <article class="card">
        <a class="card-link" href="www.test.com">
           <img class="card__image" src="card-image.jpg" />
          <div class="card__content">
            <h2 class="h5">This is another news with a very long title</h2>
            <small class="card__date">30 May 2019</small> 
          </div>
        </a>
    </article>
    <article class="card">
        <a class="card-link" href="www.test.com">
            <img class="card__image" src="card-image.jpg" />
            <div class="card__content">
                <h2 class="h5">This is a another news with a really long heading</h2>
                <small class="card__date">30 May 2019</small> 
            </div>
        </a>
    </article>
</div>

CSS CSS

.wrapper {
    display: flex;
    flex-wrap: wrap;
}

.card {
    box-shadow: 0 15px 35px 0 rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    overflow: hidden;
    min-height: 100%;
}

.card-link {
    display: inline-block;
    min-width: 100%;
    min-height: 100%;
    margin-right: 1rem;
}

.card__content {
    position: relative;
    padding: 3rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    background: white;
    height: 100%;
}

.card__image {
    background: purple;
    min-width: 100%;
    position: relative;
}


.card__date {
    margin-top: auto;
}

A link to a codepen here 指向Codepen的链接在这里

https://codepen.io/anon/pen/KjBGrd#anon-login https://codepen.io/anon/pen/KjBGrd#anon-login

Just keep using flexbox on the wrapping items and extending their children as required. 只需在包装物品上继续使用flexbox并根据需要扩展其子项即可。

 * { margin: 0; padding: 0; box-sizing: border-box; } ::before, ::after { box-sizing: inherit; } .wrapper { display: flex; } .card { width: 30%; border-radius: 4px; overflow: hidden; border: 1px solid red; display: flex; flex-direction: column; } .card-link { flex: 1; display: flex; flex-direction: column; } .card__content { padding: 2rem; display: flex; flex: 1; flex-direction: column; justify-content: space-between; align-items: center; } .card__image { display: block; background: purple; height: 100px; } .card__date { margin-top: auto; } 
 <div class="wrapper"> <article class="card"> <a class="card-link" href="www.test.com"> <img class="card__image" src="card-image.jpg" /> <div class="card__content"> <h2 class="h5">This is a news</h2> <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Magni at repellat fugit veniam! Optio maxime tempora quis inventore pariatur architecto provident, ex quam ullam repellendus.</p> <small class="card__date">30 May 2019</small> </div> </a> </article> <article class="card"> <a class="card-link" href="www.test.com"> <img class="card__image" src="card-image.jpg" /> <div class="card__content"> <h2 class="h5">This is another news</h2> <small class="card__date">30 May 2019</small> </div> </a> </article> <article class="card"> <a class="card-link" href="www.test.com"> <img class="card__image" src="card-image.jpg" /> <div class="card__content"> <h2 class="h5">This is a another news with a really long heading</h2> <small class="card__date">30 May 2019</small> </div> </a> </article> </div> 

I have change the date position to absolute to put it always at the bottom of the card no matter what is the text size. 无论文字大小如何,我都将日期位置更改为绝对位置,以使其始终位于卡片的底部。

 .wrapper { display: flex; } .card { width: 30%; box-shadow: 0 15px 35px 0 rgba(0, 0, 0, 0.08); border-radius: 4px; overflow: hidden; min-height: 100%; position: relative; } .card-link { display: inline-block; min-width: 100%; min-height: 100%; } .card__content { padding: 3rem; display: flex; flex-direction: column; justify-content: space-between; align-items: center; background: white; } .card__image { display: inline-block; background: purple; min-width: 100%; position: relative; height: 100px; } .card__date { margin-top: auto; position: absolute; bottom: 20px; } 
 <div class="wrapper"> <article class="card"> <a class="card-link" href="www.test.com"> <img class="card__image" src="card-image.jpg" /> <div class="card__content"> <h2 class="h5">This is a news</h2> <small class="card__date">30 May 2019</small> </div> </a> </article> <article class="card"> <a class="card-link" href="www.test.com"> <img class="card__image" src="card-image.jpg" /> <div class="card__content"> <h2 class="h5">This is another news</h2> <small class="card__date">30 May 2019</small> </div> </a> </article> <article class="card"> <a class="card-link" href="www.test.com"> <img class="card__image" src="card-image.jpg" /> <div class="card__content"> <h2 class="h5">This is a another news with a really long heading</h2> <small class="card__date">30 May 2019</small> </div> </a> </article> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM