【问题标题】:How do you make a flex item break to the next line?如何让弹性项目跳到下一行?
【发布时间】:2022-11-16 22:05:13
【问题描述】:

我有一个基本的 html 标记,我正在尝试使用最小的 html 包装器来实现设计。
底部的 button 不应该对齐,它应该始终留在底部。
所以我的目标是不添加更多的 html 包装器,使用 flex,强制一个 flex 项目(button)放到下一行。 block title 留在图片旁边。
你可以明白我的意思是在移动断点上检查它。 这是flex-wrap: wrap的截图

这是flex-wrap: nowrap

如您所见,在第一个示例中,按钮应位于底部,但 block title 被放到下一行,而在第二个示例中 (flex-wrap: wrap) block title 位置正确,但按钮不正确在底部。

这是sandbox link和代码示例

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;
  /* //try nowrap */
  width: 100%;
}

.logo-image {
  align-self: flex-start;
}

.headline {
  color: white;
}

.text {
  color: white;
  font-size: 16px;
  margin-bottom: 20px;
}

.btn {
  display: flex;
  width: 100%;
}

button {
  align-items: center;
  background-color: black;
  color: white;
  flex: 0 0 100%;
  justify-content: center;
  margin: 0;
}
<div class="container">
  <img src="download.png" width="50px" class="logo-image" alt="img" />
  <span class="content">
        <h4 class="headline">
          Block Title
        </h4>
        <p class="text">
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Sapiente
          aliquid sit, cupiditate
        </p>
      </span>
  <div class="btn">
    <button>link</button>
  </div>
</div>

任何帮助将不胜感激

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以将 span 设置为块级元素并将 flex-grow 设置为 1,但将 flex-basis 设置为较小的值,例如 50%,这样它会尝试成为宽度的 50%,但会增长以适应宽度。这意味着当收缩时它会尽量保持在同一条线上。

    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%;
    }
    /* added this --v */
    .content {
      display: block;
      flex: 1 0 50%;
    }
    
    .logo-image {
      align-self: flex-start;
    }
    
    .headline {
      color: white;
    }
    
    .text {
      color: white;
      font-size: 16px;
      margin-bottom: 20px;
    }
    
    .btn {
      display: flex;
      width: 100%;
    }
    
    button {
      align-items: center;
      background-color: black;
      color: white;
      flex: 0 0 90%;
      justify-content: center;
      margin: 0;
    }
    <div class="container">
      <img src="https://www.fillmurray.com/200/200" width="50px" class="logo-image" alt="img" />
      <span class="content">
            <h4 class="headline">
              Block Title
            </h4>
            <p class="text">
              Lorem ipsum dolor sit amet consectetur adipisicing elit. Sapiente
              aliquid sit, cupiditate
            </p>
          </span>
      <div class="btn">
        <button>link</button>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 2014-01-22
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 2023-03-28
      • 2020-07-25
      • 1970-01-01
      • 2020-06-16
      相关资源
      最近更新 更多