首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何省略网格布局中的内容自动宽度容器单元格?

如何省略网格布局中的内容自动宽度容器单元格?
EN

Stack Overflow用户
提问于 2020-05-10 13:46:08
回答 2查看 176关注 0票数 0

有一个具有网格布局和固定宽度的div。

它的一个子级是一个容器,并且通过模板具有自动宽度。

在容器内部,这是我想要省略的内容。

似乎容器的宽度不正确,它不等于300px - 30px - 50px。我怎样才能改变这一点?

代码语言:javascript
复制
div {
  box-sizing: border-box;
}

.grid-layout {
  width: 300px;
  display: grid;
  grid-template-columns: 30px auto 50px;
}

.left {
  grid-column-start: 1;
}

.ellipsis-container {
  grid-column-start: 2;
}

.ellipsis-content {
  white-space: nowrap;
  overflow-x: hidden;
  text-overflow: ellipsis;
}

.right {
  grid-column-start: 3;
}
代码语言:javascript
复制
<div class="grid-layout">
  <div class="left">Left</div>
  <div class="ellipsis-container">
    <div class="ellipsis-content">Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long</div>
  </div>
  <div class="right">Right</div>
</div>

EN

回答 2

Stack Overflow用户

发布于 2020-05-10 13:56:11

overflow添加到容器中是可行的。

问题

然而,您的问题更多地是针对为什么我的容器中auto的值不能解析为300px-30px-50px__?它应该是220px,但为什么要扩展到600px+?

回答

这是由于white-space: nowrap造成的。文本无法换行,导致它将宽度扩展到一个很长的值。但是,为什么容器在其子容器扩展时也会扩展呢?auto不是应该解析为220px吗,即使它的子扩展超过了那个值?这是因为在这种情况下,auto is identical to the value max-content。这意味着auto值将解析为max-content。然后,max-content将解析为该特定列的最大max-content contribution值。那么max-content contribution是什么呢?它是列的“子元素”的宽度值,导致容器的max-content size发生变化(在本例中,容器是列)。max-content本身指的是允许的最小大小,但仍然适合中的所有内容,而不会溢出。您的nowrap文本非常长,其max-content大小为600px+。这就是为什么容器也解析为600px+ (因为auto解析为最大的 max-content contribution)。

代码语言:javascript
复制
div {
  box-sizing: border-box;
}

.grid-layout {
  width: 300px;
  display: grid;
  grid-template-columns: 30px auto 50px;
}

.left {
  grid-column-start: 1;
}

.ellipsis-container {
  grid-column-start: 2;
  overflow-x: hidden;
}

.ellipsis-content {
  white-space: nowrap;
  overflow-x: hidden;
  text-overflow: ellipsis;
}

.right {
  grid-column-start: 3;
}
代码语言:javascript
复制
<div class="grid-layout">
  <div class="left">Left</div>
  <div class="ellipsis-container">
    <div class="ellipsis-content">Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long</div>
  </div>
  <div class="right">Right</div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2020-05-10 13:57:21

也将overflow添加到容器.ellipsis-container

代码语言:javascript
复制
.ellipsis-container {
  grid-column-start: 2;
  overflow-x: hidden;
}

代码语言:javascript
复制
div {
  box-sizing: border-box;
}

.grid-layout {
  width: 300px;
  display: grid;
  grid-template-columns: 30px auto 50px;
}

.left {
  grid-column-start: 1;
}

.ellipsis-container {
  grid-column-start: 2;
  overflow-x: hidden;
}

.ellipsis-content {
  white-space: nowrap;
  overflow-x: hidden;
  text-overflow: ellipsis;
}

.right {
  grid-column-start: 3;
}
代码语言:javascript
复制
<div class="grid-layout">
  <div class="left">Left</div>
  <div class="ellipsis-container">
    <div class="ellipsis-content">Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long</div>
  </div>
  <div class="right">Right</div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61707724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档