百分比+flex设置左右间距

需求:实现两个元素左右贴边,宽度自适应且中间留有固定空隙
实现方案:flex+百分比
具体实现:把中间的间距平分给左右元素,设置每个元素宽度时用calc( 百分比 - 平分的间距 )
实现效果:


image.png
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .lists{
            display: flex;
            justify-content: space-between;
        }
        .list{
            height:100px;
            display: flex;
            justify-content: center;
            align-items: center;
            color:white;
        }
        .list:first-child{
            width: calc( 80% - 30px );
            background: red;
        }
        .list:last-child{
            width: calc( 20% - 30px );
            background: green;
        }
    </style>
</head>
<body>
    <div class="lists">
        <div class="list">80%</div>
        <div class="list">20%</div>
    </div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容