简体   繁体   English

让图像占据改变大小的 div 的全部高度?

[英]Make image take up the full height of a div that changes size?

I will try to make this sound as easy as possible.我会尽量让这听起来更简单。

I am trying to place 2 div containers, side by side, and have them be the same height at all times.我正在尝试并排放置 2 个 div 容器,并让它们始终保持相同的高度。

The right div will be regular text.正确的 div 将是常规文本。 The amount of text in here will vary since I plan on using this for different pages.由于我计划将其用于不同的页面,因此此处的文本数量会有所不同。

The left div will be composed of 2 smaller containers - a title block, and an image block beneath it.左边的 div 将由 2 个较小的容器组成 - 一个标题块,以及它下面的一个图像块。

Here is a visual example of what I'm trying to achieve.这是我要实现的目标的视觉示例。 The green box is supposed to be the full photo绿色框应该是完整的照片

Example Photo示例照片


I would like the photo in the image block of the left side to take up the full height/width of the box - (similar to background-position: cover that is used in CSS).我希望左侧图像块中的照片占据盒子的整个高度/宽度 - (类似于 CSS 中使用的背景位置:封面)。 I'd prefer to use a regular img tag instead of setting it as a div background.我更喜欢使用常规的 img 标签,而不是将其设置为 div 背景。


The issue that I am having is that the image height on the left takes priority over the text box on the right hand side, and causes both containers to appear much longer than I want.我遇到的问题是左侧的图像高度优先于右侧的文本框,并导致两个容器看起来比我想要的长得多。 I want the text block on the right to be prioritized, and the image block changes height based on that.我希望右边的文本块被优先考虑,图像块根据它改变高度。

I've tried using object-fit: contain, but it isn't working, unfortunately.我尝试过使用 object-fit: contains,但不幸的是,它不起作用。 The closest I've gotten is to use width: 100%, but then it makes the height way too big.我得到的最接近的是使用宽度:100%,但它会使高度太大。

Here's what I have so far:这是我到目前为止所拥有的:

 .main { display: flex; }.main.left { width: 40%; float: left; }.main.left.title { background-color: black; text-align: center; display: block; height: 90px; padding: 50px; color: white; font-size: 40px; }.photo { height: auto; width: 100%; }.photo img { width: 100%; }.main.right { width: 60%; float: right; }
 <div class="main"> <div class="left"> <div class="title">This is my Title</div> <div class="photo"><img src="https://image.shutterstock.com/z/stock-photo-pastoral-green-field-with-long-shadows-in-tuscany-italy-275372477.jpg"></div> </div> <div class="right"> <p>text goes here lalalalalala</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> </div>

<style> 
.main {
    display: flex;
    }

.main .left {
    width: 40%;
    display: flex;
    flex-direction: column;
    }

.main .left .title {
    background-color: black;
    text-align: center;
    display: block; 
    height: 90px;
    padding: 50px;
    color: white;
    font-size: 40px;
}
.photo {
    width: 100%;
    overflow: hidden;
    position: relative;
    flex-grow: 1;
    }

.photo img {
    width: 100%;
    position: absolute;
    }

.main .right {
    width: 60%;
    }
</style>

Notes:笔记:

  • I made the image absolutely positioned so its own height won't stretch our flex row.我使图像绝对定位,因此它自己的高度不会拉伸我们的 flex 行。
  • The image is being cropped by height.图像正在按高度裁剪。 If the title is taller than the text (or the same height), you won't see the image at all.如果标题高于文本(或相同高度),您将根本看不到图像。
  • I made the left column also display flex and the photo box flex grow so that the title can stay the same height and the photo box will stretch the rest of the way to match the right column.我使左列也显示 flex 并且照片框 flex 增长,以便标题可以保持相同的高度,并且照片框将拉伸 rest 的方式以匹配右列。
  • We don't need float left/right for flex items.对于弹性项目,我们不需要向左/向右浮动。

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

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