如何防止不换行的可滚动元素使页面过宽?

huangapple go评论57阅读模式
英文:

How to prevent scrollable elements without word wrap from making the page too wide?

问题

I'm trying to create a documentation page that does not grow beyond the size of the screen. The example below works, but once I add a <pre><code> block with lots of text, this text is not wrapped and causes a horizontal scrollbar to become visible.

我试图创建一个不会超出屏幕大小的文档页面。下面的示例有效,但一旦我添加了一个包含大量文本的<pre><code>块,这些文本就不会换行,并导致水平滚动条可见。

英文:

I'm trying to create a documentation page that does not grow beyond the size of the screen. The example below works, but once I add a &lt;pre&gt;&lt;code&gt; block with lots of text, this text is not wrapped and causes a horizontal scrollbar to become visible.

I tried to fix this by adding overflow-x: auto to the &lt;code&gt; element, and this partially works, but now the text doesn't shrink anymore when the page is resized, still causing a scrollbar on mobile devices.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

checkbox.addEventListener(&quot;change&quot;, () =&gt; {
  code.style.display = checkbox.checked ? &quot;&quot; : &quot;none&quot;;
});

<!-- language: lang-css -->

.resize-wrapper {
  resize: horizontal;
  border: 1px solid black;
  overflow-x: auto;
}

.page {
  display: flex;
  justify-content: center;
  padding: 0 20px;
}

main {
  max-width: 600px;
}

code {
  display: block;
  overflow-x: auto;
}

<!-- language: lang-html -->

&lt;div class=&quot;resize-wrapper&quot;&gt;
  &lt;div class=&quot;page&quot;&gt;
    &lt;main&gt;
      &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;

      &lt;label&gt;&lt;input id=&quot;checkbox&quot; type=&quot;checkbox&quot; /&gt;show code block&lt;/label&gt;
      &lt;pre id=&quot;code&quot; style=&quot;display: none&quot;&gt;
        &lt;code&gt;
          // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        &lt;/code&gt;
      &lt;/pre&gt;
    &lt;/main&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

In the example above, .resize-wrapper represents the full viewport, I just added it to make it easier to try out on Stack Overflow.

Is there a way I can tell the &lt;pre&gt;&lt;code&gt; section to not grow beyond the size of its parent?

答案1

得分: 1

在你的 code 元素的 CSS 中添加 white-space: pre-wrap;

code {
  display: block;
  overflow-x: auto;
  white-space: pre-wrap;
}
英文:

Add white-space: pre-wrap; to your code element css

code {
  display: block;
  overflow-x: auto;
  white-space: pre-wrap;

}

huangapple
  • 本文由 发表于 2023年4月20日 06:23:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定