html - Flexbox - 如何防止长文本向右移动 div?

标签 html css flexbox css-grid

<分区>

我有一个正在处理的布局,它结合使用了 CSS 网格和 Flexbox。网格正在设置结构(左边的主图和右边的 4 行内容)。

在每个内容行中,我都有一个 flexbox,用于处理右侧缩略图的放置,以及左侧的三行文本 - 日期、标题和作者。

但是,当任何一行文本太长时,它们会开始凸起缩略图,如此处的第 2 行所示:

Example of Problem

显然,我希望缩略图固定在原位,而文本在他们触摸它之前就换行。我已经尝试了一大堆潜在的策略——使用溢出、 float 而不是 flexbox,但找不到诀窍。有什么想法吗?

这是代码,为简洁起见省略了第 3 行和第 4 行,以及一个 Codepen 链接:

https://codepen.io/anon/pen/dBWyzb

.wrapper {
  width: 1200px;
  margin: 0 auto;
}

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(2, minmax(100px, auto));
  grid-template-areas:
    "hero row1"
    "hero row2"
    "hero row3"
    "hero row4";
  grid-gap: 10px;
}

.hero {grid-area: hero;}
.row1 {grid-area: row1;}
.row2 {grid-area: row2;}

.flex {
  display: flex;
  background-color: lightblue;
  align-items: center;
}

.thumbnail {
  width: 100px;
  height: 100px;
  margin-left: auto;
}

img {
  object-fit: cover;
  height: 100%;
  width: 100%;
}
<div class="wrapper">
  <div class="grid">
    
    <div class="hero">
      <img src="https://dummyimage.com/600x400/000/fff"/>
    </div>
    
    <!-- Row 1 -->
    <div class="row1">
      <div class="flex">
        
        <div class="text">
          <div class="date">12 JULY 2019</div>
          <h3>Title</h3>
          <div class="author">Author Name</div>
        </div>
        
        <div class="thumbnail">
          <img src="https://dummyimage.com/600x400/000/fff"></img>
        </div>
      
      </div> <!-- end flex -->
    </div> <!-- end row -->
    
    <!-- Row 2 -->
    <div class="row2">
      <div class="flex">
        
        <div class="text">
          <div class="date">12 JULY 2019</div>
          <h3>A really long title seems to be shifting the right-hand image up, which is not ideal! Any ideas, internet? ------> </h3>
          <div class="author">Author Name</div>
        </div>
        
        <div class="thumbnail">
          <img src="https://dummyimage.com/600x400/000/fff"></img>
        </div>
      
      </div> <!-- end flex -->
    </div> <!-- end row -->


  </div>
</div>

最佳答案

flex: 1; 添加到 .text div,以便 flexbox 可以管理它可以占用多少空间:

.wrapper {
  width: 1200px;
  margin: 0 auto;
}

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(2, minmax(100px, auto));
  grid-template-areas: "hero row1" "hero row2" "hero row3" "hero row4";
  grid-gap: 10px;
}

.hero {
  grid-area: hero;
}

.row1 {
  grid-area: row1;
}

.row2 {
  grid-area: row2;
}

.flex {
  display: flex;
  background-color: lightblue;
  align-items: center;
}

.text {
  flex: 1;
}

.thumbnail {
  width: 100px;
  height: 100px;
  margin-left: auto;
}

img {
  object-fit: cover;
  height: 100%;
  width: 100%;
}
<div class="wrapper">
  <div class="grid">

    <div class="hero">
      <img src="https://dummyimage.com/600x400/000/fff" />
    </div>

    <!-- Row 1 -->
    <div class="row1">
      <div class="flex">

        <div class="text">
          <div class="date">12 JULY 2019</div>
          <h3>Title</h3>
          <div class="author">Author Name</div>
        </div>

        <div class="thumbnail">
          <img src="https://dummyimage.com/600x400/000/fff"></img>
        </div>

      </div>
      <!-- end flex -->
    </div>
    <!-- end row -->

    <!-- Row 2 -->
    <div class="row2">
      <div class="flex">

        <div class="text">
          <div class="date">12 JULY 2019</div>
          <h3>A really long title seems to be shifting the right-hand image up, which is not ideal! Any ideas, internet? ------> </h3>
          <div class="author">Author Name</div>
        </div>

        <div class="thumbnail">
          <img src="https://dummyimage.com/600x400/000/fff"></img>
        </div>

      </div>
      <!-- end flex -->
    </div>
    <!-- end row -->


  </div>
</div>

关于html - Flexbox - 如何防止长文本向右移动 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56718268/

上一篇:html - 将元素固定在粘性元素下方

下一篇:jquery - 调整宽度 <p> 以显示特定行数

相关文章:

javascript - 为什么这个slideUp功能不流畅?

javascript - 为什么所有跨度都被更改而不是一个?

.net - 如何使 ASP.NET ListView 水平重复分组元素?

css - 仅具有父滚动的 flexbox 嵌套 Pane

javascript - 在响应式 HTML 中向 PHP 表单添加验证

html - CSS - 以相对于其最大宽度的 % 设置跨度的宽度

javascript - 使用 ZeroClipboard 悬停时出现黄色边框

html - 嵌套的flexbox不收缩

html - <pre> 内容不收缩的 Flex 元素

html - 为什么我的现场设置容器会在移动设备 View 中溢出,但不会在更大的屏幕尺寸下溢出?