美文网首页
百分比+flex设置左右间距

百分比+flex设置左右间距

作者: Hasan_hs | 来源:发表于2023-08-20 16:37 被阅读0次

需求:实现两个元素左右贴边,宽度自适应且中间留有固定空隙
实现方案: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>

相关文章

网友评论

      本文标题:百分比+flex设置左右间距

      本文链接:https://www.haomeiwen.com/subject/ddgsmdtx.html