css - 如何使图像高度包含文本div的一定百分比?
问题描述
有一个 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/
解决方案
您需要将您img
的容器包装在具有特定高度的容器中。Pourcent 仅在它指的是特定高度时才有效。例如,如果您的身高可能是动态的,您可以使用一些 JS 来获取您的 bloc 文本的高度。
.container-img {
min-height: 340px;
float: left;
width: auto;
margin-right: 1em;
}
.Image {
max-height: 70%;
border: 0;
}
.DivBlock {
display: block;
width: 100%;
}
<h3>History:</h3>
<div class="DivBlock">
<div class="container-img">
<img class="Image" src="https://cdn.pixabay.com/photo/2015/10/09/00/55/lotus-978659__340.jpg" />
</div>
<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>
推荐阅读
- node.js - 如何在 Windows 中将 npm start 添加为服务
- json - 打字稿中的 JSON 转换
- php - 显示特定尺寸的图像
- c# - 在服务器上运行时获取 AD 组列表失败
- c# - 实例化一个对象但不存储对它的引用
- javascript - 开玩笑测试 onClick() 得到错误“预期的模拟函数已被调用,但没有被调用。”
- java - Swing 应用程序 Exported Jar 扩大维度
- java - 匹配单词的正则表达式以特殊字符的组合开头和结尾
- android - 无法解决:com.android.support:support-v4:28.0.0-rc01
- javascript - 节点从 DOM 生成图像