【发布时间】:2017-08-15 21:40:20
【问题描述】:
我正在尝试创建一个布局,其中当元素(.figure)比其容器(.timeline-content)短(高度)时,元素(.figure)居中对齐。但我希望元素在比容器长时与容器具有相同的高度。
容器本身的高度取决于其父级。
This image should help clarify the desired behavior. 这就是我现在所拥有的,我不太明白为什么 img 会超出其父级,即使它的 max-height: 100%
https://jsfiddle.net/kgdkyte4/3/
html{
position: relative;
}
.timeline-item{
width: 100%;
overflow:hidden
}
.timeline-content{
width: 50%;
float: left;
text-align: right;
}
.timeline-image{
display:flex;
align-items: center;
position:absolute;
right: 0;
width: 50%;
height:100%;
}
.figure{
width:100%;
max-height: 100%;
margin: 0;
position:relative;
}
img{
max-height:100%;
max-width:100%;
float:left;
}
<div class="timeline-item">
<div class="timeline-content">
<h3 class="timeline-title">Blah blah blah
</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum a ornare sem. In sodales ac nisl facilisis pharetra. Nam non pellentesque mauris. Proin scelerisque, sapien non scelerisque auctor, nunc erat condimentum est, viverra dapibus dui odio a neque. Mauris est dui, posuere at urna in, gravida tincidunt odio. Integer quis egestas est. Praesent tincidunt justo nec nibh malesuada ullamcorper. Nulla convallis et quam vitae posuere.
</p>
</div>
<div class="timeline-image">
<figure class="figure">
<img src="http://via.placeholder.com/550x900">
<figcaption class="figure-caption">blah
</figcaption>
</figure>
</div>
</div>
【问题讨论】:
-
设置高度:100%;和 max-height 到 img 的 px 值
-
@buxbeatz 在哪个元素上? .figure?
-
没有实际的 via.placeholder.com/550x900">
-
将高度设置为 100% 将拉伸图像。你可以在我分享的jsfiddle里试试。
-
检查这个jsfiddle.net/woptima/kgdkyte4/4你需要设置一个最大高度,单位px,高度:100%;和宽度自动到img
标签: css flexbox vertical-alignment