【问题标题】:Applying background to flex children does not work properly [duplicate]将背景应用于 flex 子项无法正常工作 [重复]
【发布时间】:2018-12-23 00:54:23
【问题描述】:

我有以下代码

.parent {
  display: flex;
  width: 100%;
  flex-direction: column;
  overflow-x: auto;
}
.child {
  display: flex;
  flex-flow: row nowrap;
  background: red;
}
.grand-child {
  flex-shrink: 0;
  display: block;
  align-items: center;
  padding: .54rem 1.05rem;
  width: 300px;
  height: 100px;
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="parent">
  <div class="child">
    <div class="grand-child">a</div>
    <div class="grand-child">b</div>
    <div class="grand-child">c</div>
    <div class="grand-child">d</div>
  </div>
</div>

DEMO

问题: 将背景应用于.children 类时,背景不会溢出内容(通过 x 轴滚动时)。我想保持可滚动性,同时仅将背景应用于.children 类。如果这不可能是什么原因?

我在回答 This 时遇到了这个问题。虽然我设法解决了这个问题,但这个特定问题仍然存在

【问题讨论】:

  • 请好好解释一下这个问题...你想要的输出是什么?
  • 嗨 @DreamHunter 请运行 sn-p 并滚动 x 轴,您会看到红色背景没有覆盖整个 .children div。我想知道为什么

标签: html css flexbox


【解决方案1】:

只是因为flex-shrink: 0; grand-child 类上的属性。如果你想在子类中对齐所有孙子,请删除它。

.parent {
  display: flex;
  width: 100%;
  flex-direction: column;
  overflow-x: auto;
}
.child {
  display: flex;
  flex-flow: row nowrap;
  background: red;
  width:1200px;
}
.grand-child {
  /*flex-shrink: 0;*/
  display: block;
  align-items: center;
  padding: .54rem 1.05rem;
  width: 300px;
  height: 100px;
  overflow-x: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="parent">
  <div class="child">
    <div class="grand-child">a</div>
    <div class="grand-child">b</div>
    <div class="grand-child">c</div>
    <div class="grand-child">d</div>
  </div>
</div>

【讨论】:

  • 感谢您的回答,我想保持原样滚动并应用背景,可以吗?
  • 是的,但是为此您需要为父元素(子元素)提供确切的宽度,我已根据您的标准编辑了 sn-p..希望它会对您有所帮助。
  • 300px x 4(孙子总宽度)=1200px
【解决方案2】:

让我们检查一下代码。

https://jsfiddle.net/2cuqL1f3/7/

css:

.child {
  background: red;
  width:100%;
  height:100px;
  overflow:scroll;
  white-space: nowrap;
}
.grand-child {

  display: inline-block;
  min-width: 300px;
  height: 100px;
  overflow-x: hidden;
}

【讨论】:

  • 感谢马克的回答 :) 但这不是我所期待的
猜你喜欢
  • 2021-04-09
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多