flex换行后space-between的问题

<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <title>demo</title>
    <style>
        div.container{
            width: 280px;
            background-color: lightgray;
            height: 100px;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
        }
        div.left{
            width: 200px;
            background-color: lightgreen;
            height: 40px;
            flex-shrink: 0;
        }
        div.right{
            width: 200px;
            background-color: lightsalmon;
            height: 40px;
            flex-shrink: 0;
            align-self: flex-end;
            justify-self: flex-end;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="left"></div>
        <div class="right"></div>
    </div>
</body>

图片.png

希望第二行的橘色是靠右的,要怎么改?

希望是如果父元素够宽,就一行显示,如果父元素不够宽,就换行,依然是左右显示。

阅读 2.3k
2 个回答

选择第二行的 然后 margin-left: auto;

.container {
  width: 280px;
  background-color: lightgray;
  height: 100px;
  display: flex;
}
.left {
  width: 200px;
  background-color: lightgreen;
  height: 40px;
}
.right {
  width: 200px;
  background-color: lightsalmon;
  height: 40px;
  align-self: flex-end;
}

DEMO

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题