javascript - 两张宽度不同高度相同的图片,如何自适应左右布局呢?

查看:95
本文介绍了javascript - 两张宽度不同高度相同的图片,如何自适应左右布局呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

有两张宽度不同高度相同的图片,需要自适应左右布局,若是设百分比width,左45%,右55%,两张图片的高度就不同了,怎么通过css实现呢

解决方案

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div>
            <div class="container_left">
                    <img src="small.png" alt="" />
            </div>
            <div class="container_right">
                    <img src="common.png" alt="" />
                </div>
            </div>
        </div>
    </body>
    <style type="text/css">
        .container_left{
            display: inline-block;
            float: left;
            width: 45%;
            
        }
        .container_right{
            display: inline-block;
            float: left;
            width: 55%;
        }
        
        img{
            width: 100%;
        }
        
    </style>
</html>


实现思路:利用pading-top比例值和width比例值都基于父亲,在外层包裹一层div(.img_box),占位,让子元素(img)绝对定位填充满。

调整后:等高

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div>
            <div class="container_left">
                <div class="img_box">
                    <img src="small.png" alt="" />
                </div>
            </div>
            <div class="container_right">
                <div class="img_box">
                    <img src="common.png" alt="" />
                </div>
            </div>
        </div>
    </body>
    <style type="text/css">
        .container_left{
            display: inline-block;
            float: left;
            width: 45%;
            
        }
        .container_right{
            display: inline-block;
            float: left;
            width: 55%;
        }
        
        .container_left .img_box{
            width: 100%;
            padding-top:100% ;
            position: relative;
        }
        
        .container_right .img_box{
            width: 100%;
            padding-top: 82%;
            position: relative;
        }
        
        
        .img_box img{
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        
    </style>
</html>

这篇关于javascript - 两张宽度不同高度相同的图片,如何自适应左右布局呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆