【问题标题】:Flex item with <pre> content not shrinking [duplicate]具有 <pre> 内容的 Flex 项目不缩小 [重复]
【发布时间】:2018-03-13 23:27:17
【问题描述】:

我有一个布局,我需要将&lt;aside&gt; 固定宽度和中间内容作为流体。一切都很好,但是如果您在中间内容中使用了&lt;pre&gt; 元素,并且您将尝试将窗口大小调整为小分辨率,然后&lt;pre&gt; 没有水平滚动并且 flex 元素溢出。 &lt;pre&gt; 元素作为块元素,overflow: auto

任何想法如何解决它?

非常感谢,最好的问候,

jsFiddle

body {
  margin: 0;
}

header {
  background-color: green;
}

footer {
  background-color: red;
}

header,
footer {
  width: 100%;
  height: 100px;
}

main {
  display: flex;
  background-color: purple;
}

aside {
  overflow-x: hidden;
  flex: 0 0 270px;
}

article {
  flex-grow: 1;
  min-height: 100vh;
  background-color: lightgray;
}

pre {
  overflow: auto;
  font-size: 87.5%;
  color: #dc3545;
}
<header>
  header
</header>
<main>
  <aside>
    aside left
  </aside>
  <article>
    <h1>CONTENT</h1>
    <pre>
&lt;button class=&quot;btn btn-primary num-badge num-badge--text-primary&quot; data-num-badge=&quot;9&quot;&gt;Primary button&lt;/button&gt;
&lt;button class=&quot;btn btn-primary num-badge num-badge--text-primary num-badge--border-primary&quot; data-num-badge=&quot;9&quot;&gt;Primary button&lt;/button&gt;
&lt;button class=&quot;btn btn-primary num-badge num-badge--text-primary num-badge--border-primary num-badge--font-awesome&quot; data-num-badge=&quot;&amp;#xf021;&quot;&gt;Primary button&lt;/button&gt;
&lt;a class=&quot;num-badge&quot; data-num-badge=&quot;9&quot;&gt;Basic Link&lt;/a&gt;;</pre>
  </article>
  <aside>
    aside right
  </aside>
</main>
<footer>
  footer
</footer>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    overflow: auto 位于 pre 元素上。

    pre {
      overflow: auto;
      font-size: 87.5%;
      color: #dc3545;
    }
    

    它不能自行溢出。它溢出了父级。

    所以,将overflow 移动到article

    article {
      overflow: auto; /* new */
      flex-grow: 1;
      min-height: 100vh;
      background-color: lightgray;
    }
    

    revised jsfiddle

    这里更详细地解释了问题和解决方案:

    【讨论】:

    • 我不想在文章上有滚动条。尝试在&lt;article&gt; 上设置max-width: 350px;,您会看到&lt;pre&gt; 本身可以有滚动条。
    • 我误解了你一个层次。所以你希望&lt;pre&gt; 的内容溢出&lt;pre&gt;。我以为你想要&lt;pre&gt; 溢出article
    • 然后在article 上使用min-width: 0 并在&lt;pre&gt; 上恢复overflow: auto,而不是overflow: autojsfiddle.net/evwh3mh2/13
    • 所以现在这个问题是我在回答中引用的帖子的完整副本。
    • 没错!天啊。你现在拯救了我的一天 :) 它运作良好!谢谢你。你能解释一下,为什么min-width 有效吗?编辑:欧基,谢谢。我会去读它,因为以前我认为你不理解我。
    猜你喜欢
    • 2018-05-07
    • 2016-11-17
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2020-04-07
    • 2019-08-08
    • 2014-10-14
    • 2013-05-28
    相关资源
    最近更新 更多