html - 当父 div 为百分比时保持图像比例

标签 html css image

我试图让图像跨越屏幕的宽度并同时保持它们的图像比例。基本上,有 4 张图像需要在屏幕宽度上均匀分布,并且需要同时保持图像比例,图像之间没有空间或填充。

我做了一个Codepen我尝试做的事情。

HTML:

<div class="featured-products">
  <ul>
    <li>
       <p>
        <img src=" http://placehold.it/320x410" alt="" />
        <img src=" http://placehold.it/320x410" alt="" />
       </p>
   </li>
   <li>
       <p>
        <img src=" http://placehold.it/320x410" alt="" />
        <img src=" http://placehold.it/320x410" alt="" />
       </p>
   </li>
   <li>
       <p>
        <img src=" http://placehold.it/320x410" alt="" />
        <img src=" http://placehold.it/320x410" alt="" />
       </p>
   </li>
   <li>
       <p>
        <img src=" http://placehold.it/320x410" alt="" />
        <img src=" http://placehold.it/320x410" alt="" />
       </p>
   </li>
 </ul>
 </div>

更少/CSS:

.featured-products {
    width: 100%;
    position: relative;
    font-size: 0;
    display: block;
    ul {
        margin: 0;
        padding: 0;
        width: 100%;
        text-align: center;
        li {
            display: inline-block;
            width: 25%;
            p {
                width: 100%;
                height: auto;
                img:nth-child(1){
                    background-size: cover;
                    max-width: 100%;
                    max-height: 100%;
                }
                img:nth-child(2){
                    display: none;
                }
            }
        }
    }
}

最佳答案

img {
    width:100%;
    height:auto;
}

应该可以解决问题。演示:http://codepen.io/anon/pen/RPzpeN?editors=110

关于html - 当父 div 为百分比时保持图像比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32118563/

相关文章:

jquery - CSS 和 jQuery 当前链接导航 : how to update the <li> class

javascript - 链接上的单击事件,但只有一个

html - 不同宽度字段的 CSS 最佳实践

jquery - 添加类以隐藏 div 时动画不起作用

html - 部署后响应式设计在 iPhone 上不起作用

java - Android GridView 项目在按下时更改图像并更改回来

javascript - 为图片生成框架

Javascript child 游戏

html - 完美居中(响应式?)div

c - 将图像文件读入 char 数组