html - 如何在不破坏比例的情况下使所有图像高度相同
问题描述
我有一个文章列表,并希望它们的所有图像都具有相同的高度,以便文章标题垂直排列。
我尝试为图像设置一个固定的高度,例如 200 像素,但这会导致它们看起来被压扁。
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.article {
margin-bottom: 24px;
width: 100%;
}
.image {
width: 100%;
display: block;
height: auto;
}
.article__category {
margin: 12px 0;
padding: 2px 4px;
display: inline-block;
}
<article class="article">
<div class="article__image">
<a href="#">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/ceremony.jpg" class="radius-small image">
</a>
</div>
<div class="article__category radius-small">
<a href="#">
<span>New Track</span>
</a>
</div>
<h1 class="article__title">
<a href="#">Ceremony: <em>“Turn Away The Bad Thing” Video</em>
</a>
</h1>
</article>
<article class="article">
<div class="article__image">
<a href="#">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/joyero_copy.jpg" class="radius-small image">
</a>
</div>
<div class="article__category radius-small">
<a href="#">
<span>Music Video</span>
</a>
</div>
<h1 class="article__title">
<a href="#">Joyero: <em>“Dogs” Video</em>
</a>
</h1>
</article>
我希望图像都具有相同的高度,而不会破坏它们的比例。
解决方案
嗯,你可以试试这个...
非常适合我
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.article {
margin-bottom: 24px;
width: 100%;
}
.image {
width: 100%;
display: block;
height: auto;
}
.article__category {
margin: 12px 0;
padding: 2px 4px;
display: inline-block;
}
/*
classes of images had spaces Probably that's why it's not working.
I have added - in images' classes
*/
.radius-small-image {
height: 200px;
}
<!Doctype HTML>
<html>
<head></head>
<body>
<article class="article">
<div class="article__image">
<a href="#">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/ceremony.jpg" class="radius-small-image">
</a>
</div>
<div class="article__category radius-small">
<a href="#">
<span>New Track</span>
</a>
</div>
<h1 class="article__title">
<a href="#">Ceremony: <em>“Turn Away The Bad Thing” Video</em>
</a>
</h1>
</article>
<article class="article">
<div class="article__image">
<a href="#">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1774776/joyero_copy.jpg" class="radius-small-image">
</a>
</div>
<div class="article__category radius-small">
<a href="#">
<span>Music Video</span>
</a>
</div>
<h1 class="article__title">
<a href="#">Joyero: <em>“Dogs” Video</em>
</a>
</h1>
</article>
</body>
</html>
推荐阅读
- entity-framework-6 - 字段 Pdf 必须是字符串或数组类型,最大长度为 '4000'
- jquery - 如何获取数据表“标题名称”
- elasticsearch - 如何查询包含具有至少一个多边形匹配地理形状过滤器的多边形数组的文档?
- css - 具有固定背景图像 css 的圆形动画
- libgdx - box2d 中的软约束是什么?
- apache-spark - 有没有办法根据 pyspark 中的索引对数据帧进行切片?
- c# - 如何在不单击按钮的情况下直接使用 RadioButtons 和 CheckBoxes 输出到 TexBox
- android - 为什么 IncomingNumber 总是空的?
- javascript - Angular 2 表单验证触碰和脏返回错误,找不到原因
- c# - System.ObservableExtensions.Subscribe:不明确的引用