如何用Flex布局让第三个元素从第二列起排列且不新增HTML容器
问题:用Flex布局让第三个项目从第二列开始排列(不新增HTML容器)
我有基础的HTML标记,希望使用最少的HTML容器来实现设计。我的目标是不新增HTML容器,仅通过CSS Flex布局,让第三个Flex项目从第二列开始排列,预期效果如下:
1 2 3
当然可以通过为第三个元素添加padding/margin-left来实现,但我寻求仅使用CSS Flex且HTML标记最少的解决方案。我想要实现的效果是标题和文本需从同一列开始,可查看以下代码片段进行测试:
CSS代码
body { margin: 0; padding: 0; } .container { background-color: grey; overflow: auto; padding: 20px; align-items: center; display: flex; position: relative; column-gap: 15px; flex-wrap: wrap; width: 100%; } .content { display: flex; flex-wrap: wrap; } .logo-image { align-self: flex-start; padding-top: 10px; order: 1; } .headline { color: white; order: 2; padding-left: 10px; } .text { color: white; font-size: 16px; margin-bottom: 20px; order: 3; } .btn { display: flex; width: 100%; } button { align-items: center; background-color: black; color: white; flex: 0 0 90%; justify-content: center; margin: 0; }
HTML代码
<div class="container"> <div class="content"> <h4 class="headline"> Block Title </h4> <img src="https://www.fillmurray.com/200/200" width="50px" class="logo-image" alt="img" /> <p class="text"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Sapiente aliquid sit, cupiditate </p> </div> <div class="btn"> <button>click</button> </div>
内容的提问来源于stack exchange,提问作者FD3
相关产品推荐
相关产品推荐

