gpt4 book ai didi

html - 向左浮动图像,适合父级,保持纵横比

转载 作者:太空狗 更新时间:2023-10-29 16:45:02 24 4
gpt4 key购买 nike

我有以下内容:jsfiddle.net

我想要做的是让图像 float 在文本的左侧,以便它填充父级 (.box)。请注意,.box 的高度可能因文本行数而异。

最终结果应如下所示:i.imgur.com

这将如何完成?

.box {
position: relative;
display: block;
width: 600px;
padding: 24px;
margin-bottom: 24px;
border: 2px solid red;
}
.img {
float: left;
}
.text {
font-size: 14px;
}
<div class="box">
<div class="img" style="background-image: url('https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg');"></div>
<div class="text">This box is one line.</div>
</div>

<div class="box">
<div class="img" style="background-image: url('https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg');"></div>
<div class="text">This box has two lines. This box has two lines. This box has two lines. This box has two lines. This box has two lines. This box has two lines.</div>
</div>

最佳答案

您可以在父元素上使用display: table,在子元素上使用display: table-cell

PLUNKER

片段

<!DOCTYPE html>
<html>

<head>
<style>
html,
body {
height: 100%;
width: 100%;
}
figure {
display: table;
width: 600px;
height: auto;
margin-bottom: 24px;
border: 2px solid red;
}
img {
float: left;
display: table-cell;
min-height: 100%;
margin-right: 20px;
}
figcaption {
font-size: 14px;
height: 100%;
}
</style>
</head>

<body>
<figure>
<img src="http://i.imgur.com/MhHgEb1.png">
<figcaption>This box is one line.</figcaption>
</figure>

<figure>
<img src="http://i.imgur.com/MhHgEb1.png">
<figcaption>This box has two lines. This box has two lines. This box has two lines. This box has two lines. This box has two lines. This box has two lines.</figcaption>
</figure>
</body>

</html>

关于html - 向左浮动图像,适合父级,保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39477885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com