简体   繁体   English

Flexbox 等高问题

[英]Flexbox equal height issue

I'm trying to create a list with items each containing text and an image.我正在尝试创建一个列表,其中每个项目都包含文本和图像。 I'm using flexbox to align the text and the image.我正在使用 flexbox 来对齐文本和图像。

However, I would like the height of the text element ( .list-content ) to define the height of the image element ( .list-img ), but I can't make it work.但是,我希望文本元素 ( .list-content ) 的高度来定义图像元素 ( .list-img ) 的高度,但我无法使其工作。

 .advanced-list { list-style: none; width: 85%; } .advanced-list li { display: flex; min-height: 80px; background-color: #eee; } .advanced-list li .list-img { display: block; overflow-x: hidden; flex: 1 0 33%; } .advanced-list li .list-img img { display: block; max-width: 100%; height: auto; } .advanced-list li .list-content { padding: 10px; } .advanced-list li .list-content .list-header { margin-top: 0; } .advanced-list li .advanced-link { display: flex; color: #252525; text-decoration: none; } .advanced-list li .advanced-link:hover { background-color: #e7e6e6; } .advanced-list li + li { margin-top: 5px; }
 <ul class="advanced-list"> <li> <div class="list-content"> <h4 class="list-header">List item without link</h4> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus placerat augue diam, id ornare neque mollis eu. Vivamus semper diam urna, in maximus felis sollicitudin eget. Vivamus sed mi at nunc condimentum placerat. </div> <div class="list-img"> <img src="https://placehold.it/250x125?text=250x125" alt="Img title"> </div> </li> <li> <a href="#" class="advanced-link"> <div class="list-content"> <h4 class="list-header">List item with link</h4> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus placerat augue diam, id ornare neque mollis eu. Vivamus semper diam urna, in maximus felis sollicitudin eget. Vivamus sed mi at nunc condimentum placerat. Aenean eget gravida sem. Vestibulum mollis eros non fermentum rhoncus. </div> <div class="list-img"> <img src="https://placehold.it/250x125?text=250x125" alt="Img title"> </div> </a> </li> </ul>

Note that sometimes there can be an <a> element wrapping the content.请注意,有时可以有一个<a>元素包裹内容。 Also, the image should fill up 33% of the width of the list item.此外,图像应填满列表项宽度的 33%。

See fiddle here .请参阅此处的小提琴

Give height: 100% to the img tag.img标签指定height: 100% Hope this will help you.希望这会帮助你。

See this: http://jsfiddle.net/y5pxkbn1/4看到这个: http : //jsfiddle.net/y5pxkbn1/4

Since your image should be based on the height of the text and be 33% wide, combine background-image with background-size: cover .由于您的图像应该基于文本的高度并且宽度为 33%,因此将background-imagebackground-size: cover

Here I simply removed the img and added the image source to the list-img element in the markup using a inline style, though you can of course also put the image path in the external CSS.在这里,我只是删除了img并使用内联样式将图像源添加到标记中的list-img元素,当然您也可以将图像路径放在外部 CSS 中。

Another option would be to use object-fit on the img , thought it has less browser support than background-image .另一种选择是在img上使用object-fit ,认为它比background-image具有更少的浏览器支持。

Updated fiddle更新的小提琴

Stack snippet堆栈片段

 .advanced-list { list-style: none; width: 85%; } .advanced-list li { display: flex; min-height: 80px; background-color: #eee; } .advanced-list li .list-img { flex: 1 0 33%; background-position: center; background-size: cover; } .advanced-list li .list-content { padding: 10px; } .advanced-list li .list-content .list-header { margin-top: 0; } .advanced-list li .advanced-link { display: flex; color: #252525; text-decoration: none; } .advanced-list li .advanced-link:hover { background-color: #e7e6e6; } .advanced-list li + li { margin-top: 5px; } body { padding: 10px; }
 <ul class="advanced-list"> <li> <div class="list-content"> <h4 class="list-header">List item without link</h4> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus placerat augue diam, id ornare neque mollis eu. </div> <div class="list-img" style="background-image: url(https://placehold.it/250x125?text=250x125)"> </div> </li> <li> <a href="#" class="advanced-link"> <div class="list-content"> <h4 class="list-header">List item with link</h4> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus placerat augue diam, id ornare neque mollis eu. Vivamus semper diam urna, in maximus felis sollicitudin eget. Vivamus sed mi at nunc condimentum placerat. Aenean eget gravida sem. Vestibulum mollis eros non fermentum rhoncus. </div> <div class="list-img" style="background-image: url(https://placehold.it/250x125?text=250x125)"> </div> </a> </li> </ul>

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

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