css - 如何使图像高度包含文本div的一定百分比?

标签 css image responsive

有一个 div 有一个图像和一些文本。包含的 div 没有特定的高度,取决于 div 中的文本。

我为图像使用以下 css 并包含 div:

.Image{
    float: left;
    width: auto;
    max-height: 70%;
    margin-right: 1em;
    border: 0;
}
.DivBlock{
    display: block;
    width: 100%;
}

但是,由于 div 没有指定高度,因此图像高度不会根据外部 div 进行调整。我该如何调整它以使其仅占包含的 div 高度的 70%?

在下面找到完整的代码:

.Image{
    float: left;
    width: auto;
    max-height: 70%;
    margin-right: 1em;
    border: 0;
}
.DivBlock{
    display: block;
    width: 100%;
}
<h3>History:</h3>
<div class="DivBlock">
    <img class="Image" src="https://cdn.pixabay.com/photo/2015/10/09/00/55/lotus-978659__340.jpg" />
    <p>Matheran was identified by Hugh Poyntz Malet, the then district collector of Thane district in May 1850. Lord Elphinstone, the then Governor of Bombay laid the foundations of the development as a future hill station. The British developed Matheran as a resort to beat the summer heat in the region.</p>
    <p>Matheran is the birthplace of freedom fighter Veer Bhai Kotwal. He was born on 1 December 1912 in a Barber family. The state government has built a monument in his memory. The Matheran Hill Railway was built in 1907 by Sir Adamjee Peerbhoy and covers a distance of 20 km (12 mi), over large swathes of forest territory.</p>
    <p>The Matheran hill railway, also known as Matheran Light Railway (MLR), was inspected by UNESCO world heritage site officials but failed to make it to the list as a World Heritage Site. India's other Hill Railways like the Darjeeling Railway, the Kangra Valley Railway, Nilgiri Mountain Railway are already on the list.</p>
</div>

同样可以找到 jsfiddle: https://jsfiddle.net/mithunu/tu25y6da/

最佳答案

将图像包裹在“div”标签内并为其添加最小高度。

<div class="img-wrapper">
    <img class="Image" src="https://cdn.pixabay.com/photo/2015/10/09/00/55/lotus-978659__340.jpg" />
</div>
<style>
    .img-wrapper{
        display:inline-block;
        min-height:70%;
    }
</style>

关于css - 如何使图像高度包含文本div的一定百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58117333/

相关文章:

html - 如果内联 block 没有内容,为什么它会与顶部对齐?

css - 从元素中获取 css 选择表达式

android - MediaCodec - 将图像编码为 mp4 视频

android - ORMLite 在数据库中存储 PNG 文件

可变高度的 CSS float Div

jQuery 在绑定(bind) .load() 事件之前检查图像是否已加载

css - 带有 flex-box 的响应式 CSS 让我抓狂

html - 使响应式菜单与上面的图片/标题宽度相同

video - 如何制作YouTube iFrame视频自适应

php - 这是什么类(class)资料?