我正在努力实现这样的布局,其中有一个设置了宽度的图像,如果它太长,它旁边的文本会被截断.我还希望Flex容器的内容正确对齐.

enter image description here

我一直在try 将其设置为https://codepen.io/zeckdude/pen/jOQeRoZ,当我在段落上设置flex: 1以确保图像获得全宽时,我想不出如何仍然正确地对齐所有内容.不幸的是,我还是得到了这样的结果:

enter image description here

以下是我在代码中拥有的代码:

.container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 300px;
  padding: 16px;
  background-color: gray;
}

hr {
  width: 100%;
}

.flex-container {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 16px;
  width: 100%;
}

/* Make the image container take 50px width */
.flex-container .image-container {
  width: 50px;
  height: 50px;
  border-radius: 25px;
  overflow: hidden;
}

/* Make the image take its full width and height within the image container */
.flex-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Make the text truncate with ellipsis when there's not enough space */
.flex-container p {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="container">
  <div class="flex-container">
<div class="image-container">
  <img src="https://picsum.photos/id/64/200" alt="Image">
</div>
<p>Your long text goes here and can keep going for a long time</p>
  </div>
  
  <hr />
  
  <div class="flex-container">
<div class="image-container">
  <img src="https://picsum.photos/id/64/200" alt="Image">
</div>
<p>Short text</p>
  </div>
</div>

一些人建议go 掉.flex-container p上的flex: 1,但不幸的是,我需要那里,以确保图像达到完整的宽度.你可以在这个截图中看到这一点,我已经删除了它,图像看起来不正确:

enter image description here

推荐答案

这里有一个不同的解决方案,因为这些内容是动态的.我所做的更改:

.flex-container .image-container {
    /* overflow: hidden; */
}
.flex-container img {
    width: 50px;
    height: 50px;
    border-radius: 25px;
}
.flex-container p {
    /* flex: 1; */
}

    .container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 300px;
  padding: 16px;
  background-color: gray;
}

hr {
  width: 100%;
}

.flex-container {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 16px;
  width: 100%;
}

/* Make the image container take 50px width */
.flex-container .image-container {
  width: 50px;
  height: 50px;
  border-radius: 25px;
  /* overflow: hidden; */
}

/* Make the image take its full width and height within the image container */
.flex-container img {
  width: 50px;
  height: 50px;
  border-radius: 25px;
  object-fit: cover;
}

/* Make the text truncate with ellipsis when there's not enough space */
.flex-container p {
  /* flex: 1; */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="container">
  <div class="flex-container">
    <div class="image-container">
      <div class="image-inner-container">
        <img src="https://picsum.photos/id/64/200" alt="Image">
      </div>
    </div>
    <p>Your long text goes here and can keep going for a long time</p>
  </div>

  <hr />

  <div class="flex-container">
    <div class="image-container">
      <div class="image-inner-container">
        <img src="https://picsum.photos/id/64/200" alt="Image">
      </div>
    </div>
    <p style="flex: initial;">Short text</p>
  </div>
</div>

Css相关问答推荐

为什么我的容器的弹性包裹会导致我的自动填充网格添加额外的空轨道行?

如何在css中创建文件div效果

如何使2个元素的宽度相同?

排除特定元素的目标元素

在网格中整齐地对齐项目

如何为 React Native switch 组件创建标签?

如何使代码块输出在 quarto revealjs 演示文稿中水平滚动

背景上带有文本的按钮的反转 colored颜色

Angular 以Angular 获取当前聚焦的元素

如何给三角形添加渐变?

CSS 媒体查询不为桌面版的圣杯响应式布局包装 Flexbox 容器元素

使用 styled-components,我如何定位组件的子类?

宽度为 x VW 的元素似乎对窗口调整大小没有react

修复导航栏隐藏页面内容

twitter-bootstrap:当鼠标悬停在 -tag 中的 btn-group 上时,如何go 掉带下划线的按​​钮文本?

什么是 ::content/::slotted 伪元素,它是如何工作的?

使用 CSS 强制侧边栏高度 100%(带有粘性底部图像)?

flex-wrap 如何与 align-self、align-items 和 align-content 一起使用?

更改 FontAwesome 图标的字体粗细?

当子元素水平溢出时,为什么忽略父元素的右填充?