Flex布局一行代码实现最后一个元素右对齐

556 阅读1分钟

image.png

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex布局下实现右对齐</title>
    <style>
        .flex{
            display: flex;
            width: 500px;
            height: 50px;
            line-height: 50px;
            border: tomato 2px solid;
            margin: 0 auto;
            margin-top:200px;
            
        }
        .left{
            width: 100px;
            background-color: orange;
        }
        .center{
            width: 200px;
            background-color: aqua;
        }
        .right{
            background-color: black;
            color: white;
        }
    </style>
</head>
<body>
    <div class="flex">
        <div class="left">左边</div>
        <div class="center"></div>
        <div class="right" style="margin-left:auto;">右边</div>    
    </div>
</body>
</html>

mdn:说明

developer.mozilla.org/zh-CN/docs/…

image.png

image.png