请查看以下代码:

<div style="display: flex; background: black; max-width: 600px;">
  <div style="background: yellow">AAA AAA AAA AAA AAA AAA</div>
  <div style="min-width: 0;">
    <div style="background: cyan">BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB</div>
    <div style="background: magenta; overflow-x:auto; width:100%; min-width: 0;">
      <div style="width:3000px"> 
        CCC CCC
      </div>
    </div>
    <div style="background: green">DDD DD DDDDDD D</div>
  </div>
</div>

我有一个双亲FLEX分成2列.

在右栏中,其中一个元素(洋红元素)非常大,但这对我来说没问题,我想保留滚动行为.

但是,因为洋红元素很宽,黄色元素缩小了,这让我很困扰.

我希望左列和右列之间的比例为exactly,与此代码片段中的比例相同:

<div style="display: flex; background: black; max-width: 600px;">
  <div style="background: yellow">AAA AAA AAA AAA AAA</div>
  <div style="min-width: 0;">
    <div style="background: cyan">BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB</div>
    <div style="background: green">DDD DD DDDDDD D</div>
  </div>
</div>

如何继续"忽略"洋红色元素的宽度,这样黄色列的大小才不会改变?

注:I don't want to use fixed widths!我希望我的列根据它们包含的内容共享可用空间(只有洋红色元素应该被忽略)

谢谢

推荐答案

Use 100. flex-basis应该设置在较长的元素上-如果我想非常简单地解释它,它类似于max-width,但特定于flex.

The 100 property determines the initial size of a flex item before the remaining space is distributed by the system during layout.

例如,if we set the 100 property of a flex item to 101, it means that the item's initial size will be 200 pixels(不管其内容或其他元素的大小).然后,与其他灵活的项目和可用空间一起,Flexbox模型会自动在内容项目中以适当的比例分配适当的空间.

<!-- 解 -->

<h3>With Too Large Element - Use "flex-basis"</h3>
<div style="display: flex; background: black; max-width: 600px;">
  <!-- First Column -->
  <div style="background: yellow;">
    AAA AAA AAA AAA AAA AAA
  </div>
  <!-- Second Column -->
  <div style="min-width: 0; flex-basis: 100%;"> <!-- Use "flex-basis: 100%" on too width element -->
    <!-- Cyan -->
    <div style="background: cyan;">
      BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB
    </div>
    <!-- Magenta (too large) -->
    <div style="background: magenta; overflow-x:auto; width: 100%; min-width: 0;">
      <div style="width: 3000px;"> 
        CCC CCC
      </div>
    </div>
    <!-- Green -->
    <div style="background: green">
      DDD DD DDDDDD D
    </div>
  </div>
</div>


<!-- To Compare -->

<h3>Original (Without Too Large Element)</h3>
<div style="display: flex; background: black; max-width: 600px;">
  <!-- First Column -->
  <div style="background: yellow;">
    AAA AAA AAA AAA AAA AAA
  </div>
  <!-- Second Column -->
  <div style="min-width: 0; flex-basis: 100%;">
    <!-- Cyan -->
    <div style="background: cyan;">
      BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB
    </div>
    <!-- Green -->
    <div style="background: green">
      DDD DD DDDDDD D
    </div>
  </div>
</div>


<!-- To Compare -->

<h3>Original (With Too Large Element)</h3>
<div style="display: flex; background: black; max-width: 600px;">
  <!-- First Column -->
  <div style="background: yellow;">
    AAA AAA AAA AAA AAA AAA
  </div>
  <!-- Second Column -->
  <div style="min-width: 0;">
    <!-- Cyan -->
    <div style="background: cyan;">
      BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB BBB
    </div>
    <!-- Magenta (too large) -->
    <div style="background: magenta; overflow-x:auto; width: 100%; min-width: 0;">
      <div style="width: 3000px;"> 
        CCC CCC
      </div>
    </div>
    <!-- Green -->
    <div style="background: green">
      DDD DD DDDDDD D
    </div>
  </div>
</div>

Html相关问答推荐

html中背景色的全单元格R中的Rmarkdown表

使用固定的HTML代码创建两个可向右滚动的行

Angular 15 p-对话框不使用组件 Select 器呈现HTML

如何在元素的三条边中间换行边框?

如何收集<;p&>元素下的<;p>;子元素

使用响应迅速的网格系统,将两列保持在同一行,同时允许更多列用于更大的屏幕

如何使不断增长的div不溢出其包含固定div的父级

减小HTML负载中base64字符串的大小

如何在没有包装元素的情况下在React(Next.js)中呈现HTML注释

Swift WKUserContentController 和 WKScriptMessageHandler 设置不正确

在屏幕上每行 css 中的特定列居中

是否有语义 HTML 可以澄清含义?

为什么 CSS 网格超出了父级?

Google Apps 脚本 - RegEx 适用于小文本,但不适用于大文件?

发送带有图像的Google文档作为HTML格式的邮件正文不再起作用

使用flex wrap时,如何设置另一个元素的宽度与换行后的元素对齐?

为什么使用 Css 只能看到 1 列而不是 3 列?

调整大小时 CSS 溢出

打开 Bootstrap 模式时无法获得模糊的背景

尽管设置了 width:100% left:0,但居中对齐的 CSS 动画并不对称