html - html css img 分辨率操作
问题描述
我刚开始研究 html 和 css,需要了解如何解决以下问题:
我的 html 正文中有以下部分
.section-meals {
padding: 0;
background-color: #f4f4f4;
}
.meals-showcase {
list-style: none;
width: 100%;
}
.meals-showcase li {
display: block;
float: left;
width: 25%;
}
.meal-photo {
width: 100%;
margin: 0;
overflow: hidden;
background-color: #000;
}
.meal-photo img {
/*
Width 100% + height auto set to show up the entire image in a bounded area.
This is similar to wrap content height and width in android and iOS.
*/
width: 100%;
height: 100%;
/*
Made everything bigger, but now the images are bigger than the container themselves.
So we have to hide the overflow in the container with the property hidden.
*/
transform: scale(1.30);
transition: transform 0.5s, opacity 0.5s;
opacity: 0.6;
}
.meal-photo img:hover {
opacity: 1;
transform: scale(1.03);
}
<section class="section-meals">
<ul class="meals-showcase clearfix">
<li>
<figure class='meal-photo'>
<img src="resources/img/1.jpg" alt='passport photo'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="resources/img/2.jpg" alt='world map'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="resources/img/3.jpg" alt='globe in hand'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="resources/img/4.jpg" alt='taj mahal'>
</figure>
</li>
</ul>
<ul class="meals-showcase clearfix">
<li>
<figure class='meal-photo'>
<img src="resources/img/5.jpg" alt='cliff with beautiful houses'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="resources/img/6.jpg" alt='eiffel tower'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="resources/img/7.jpg" alt='Maya bay'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="resources/img/8.jpg" alt='beach'>
</figure>
</li>
</ul>
</section>
现在,当它在网页上呈现时。我看到了空白。因为我的图像都是不同的分辨率。如何确保 li 中的所有元素相互粘连,图像填充框以使其看起来正常。如果所有图像都具有相同的分辨率,则上述工作正常。
代码参考来自我学习的一门 udemy 课程。但我正在尝试自己的场景。
解决方案
您可以使用padding top 技巧给您的 li 一个宽高比,然后使用对象拟合使您的图像完美拟合(您还需要一个 ie polyfill 来进行对象拟合):
body {
margin:0;
}
.meals-showcase {
/* reset list styles and use flex to line up in a row */
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
}
.meals-showcase>li {
/* make li 25% width */
max-width: 25%;
width: 25%;
margin: 0;
padding: 0;
}
.meals-showcase figure {
/* make figure into block and relative position, give this the aspect ratio - here I use 75% padding top */
display: block;
width: 100%;
position: relative;
margin: 0;
padding: 75% 0 0 0;
}
.meals-showcase img {
object-fit: cover; /* add this */
display: block;
position: absolute; /* this is so it fills the figure */
width:100%;
height:100%;
top: 0;
left: 0;
transition: transform 0.5s;
}
.meal-photo img:hover {
transform: scale(1.05);
}
.meals-showcase figure:hover {
z-index:1; /* bring hover figure to front */
}
<section class="section-meals">
<ul class="meals-showcase clearfix">
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/400/300" alt='passport photo'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/200/350" alt='world map'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/250/300" alt='globe in hand'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/300/300" alt='taj mahal'>
</figure>
</li>
</ul>
<ul class="meals-showcase clearfix">
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/300/400" alt='cliff with beautiful houses'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/200/300" alt='eiffel tower'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/200/400" alt='Maya bay'>
</figure>
</li>
<li>
<figure class='meal-photo'>
<img src="https://www.fillmurray.com/200/500" alt='beach'>
</figure>
</li>
</ul>
</section>
推荐阅读
- android - 为什么我的应用程序能够在较新的设备上运行,但不能在较旧的设备上运行(向后兼容 build.gradle 问题)?
- c++ - 将静态数组复制到动态数组c ++
- javascript - 在 Bootstrap-Vue 范围滑块上需要仅拖动
- postgresql - 无法连接到现有的本地 postgresql 数据库,密码验证失败
- tree - 森林包:跳过空节点
- javascript - 如何在 Gerrit 的变更集页面中找到动态元素
- python - 将字符串中的嵌套列表转换回嵌套列表
- javascript - UseRef 选择实际的父组件
- c# - MStest 所有测试用例都被跳过 .net 应用程序
- node.js - 错误:为构建模式提供的类型之一缺少名称