CSS的”overflow”属性未显示整个单词。

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

CSS "overflow" does not show the whole word

问题

以下是您要翻译的内容:

我在一些窗口中有以下的“divs”排列,如果内容过多,应该可以显示滚动条。然而,内容并没有完全显示。

.main-container {
    height: 100%;
    width: 100%;
    display: grid;
    grid-template-areas:
        'table   button'
        'table   word'
        'table   debugger'
        'graph   debugger'
        'graph   debugger'
        'graph   debugger'
        'code    code';
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 48px 48px 1fr 1fr 48px 48px 48px;
    gap: 2px;
}

.sub-container {
    background-color: #4d7ba9;
    border-radius: 0px;
    display: flex;
    justify-content: center;
}

#table-container {
    grid-area: table;
}

#graph-container {
    grid-area: graph;
}

#button-container {
    grid-area: button;
}

#word-container {
    grid-area: word;
}

#debug-container {
    grid-area: debugger;
}

#code-container {
    grid-area: code;
}

#graph-container {
    display: flex;
}

#table-container {
    display: flex;
    align-items: flex-start;
    text-align: center;
    overflow: auto;
    width: auto;
}
<div class="main-container">
  <div class="sub-container" id="table-container">
    <p id="text">problem</p>
  </div>

  <!-- 所有容器都应保持原样 -->
  <div class="sub-container" id="graph-container">
  </div>

  <div class="sub-container" id="button-container">
  </div>

  <div class="sub-container" id="word-container">
  </div>

  <div class="sub-container" id="debug-container">
  </div>

  <div class="sub-container" id="code-container">
  </div>
</div>

整个单词“problem”应该可以滚动。

如果您在浏览器中查看代码,您会发现“problem”的“p”被截断,单词越大,截断越多。

此外,应该可以选择不同的单词或不同的大小,这意味着不应仅通过“margin”解决。

提前感谢任何解决方案或建设性的批评。

英文:

I have the following arrangement of "divs" in some of these windows, it should be possible to show a scrollbar if the content gets too much. However, the content is then not displayed completely

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

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

.main-container {
    height: 100%;
    width: 100%;
    display: grid;
    grid-template-areas:
        &#39;table   button&#39;
        &#39;table   word&#39;
        &#39;table   debugger&#39;
        &#39;graph   debugger&#39;
        &#39;graph   debugger&#39;
        &#39;graph   debugger&#39;
        &#39;code    code&#39;;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 48px 48px 1fr 1fr 48px 48px 48px;
    gap: 2px;
}

.sub-container {
    background-color: #4d7ba9;
    border-radius: 0px;
    display: flex;
    justify-content: center;
}

#table-container {
    grid-area: table;
}

#graph-container {
    grid-area: graph;
}

#button-container {
    grid-area: button;
}

#word-container {
    grid-area: word;
}

#debug-container {
    grid-area: debugger;
}

#code-container {
    grid-area: code;
}

#graph-container {
    display: flex;
}

#table-container {
    display: flex;
    align-items: flex-start;
    text-align: center;
    overflow: auto;
    width: auto;
}

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

&lt;div class=&quot;main-container&quot;&gt;
  &lt;div class=&quot;sub-container&quot; id=&quot;table-container&quot;&gt;
    &lt;p id=&quot;text&quot;&gt;problem&lt;/p&gt;
  &lt;/div&gt;

  &lt;!-- All containers should remain as they are --&gt;
  &lt;div class=&quot;sub-container&quot; id=&quot;graph-container&quot;&gt;
  &lt;/div&gt;

  &lt;div class=&quot;sub-container&quot; id=&quot;button-container&quot;&gt;
  &lt;/div&gt;

  &lt;div class=&quot;sub-container&quot; id=&quot;word-container&quot;&gt;
  &lt;/div&gt;

  &lt;div class=&quot;sub-container&quot; id=&quot;debug-container&quot;&gt;
  &lt;/div&gt;

  &lt;div class=&quot;sub-container&quot; id=&quot;code-container&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

the whole word "problem" should be scrollable

If you look at the code in the browser, you can see that the "p" of "problem" is truncated, the larger the word, the more truncated.

In addition, it should be possible to choose a different word or a different size, meaning it shouldn't just be solved with "margin".

Thank you in advance for any solutions or constructive criticism

答案1

得分: 2

让我来缩小示例。

<!-- 开始代码段:js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
.main-container {
  margin: auto;
  width: 100px;
  height: auto;
  border: 1px solid black;
}

.sub-container {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    text-align: center;
    overflow: auto;
    width: auto;
}

<!-- 语言: lang-html -->
<div class="main-container">
  <div class="sub-container">
    <p>abc</p>
  </div>

  <div class="sub-container">
    <p>abcdefghijklmnopqrstuvwxyz</p>
  </div>
</div>  

<!-- 结束代码段 -->

这里可以看到第二行从开头被剪切掉,我们无法滚动到极左边。

这是因为在 flexbox 中,项目不会溢出容器的边界。

但这里有一个修复方法。
为了使文本居中,不要在容器上使用 justify-content: center,而是在文本上使用 margin: auto

<!-- 开始代码段:js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-css -->
.main-container {
  margin: auto;
  width: 100px;
  height: auto;
  border: 1px solid black;
}

.sub-container {
    display: flex;
    align-items: flex-start;
    text-align: center;
    overflow: auto;
    width: auto;
}

.sub-container p {
    margin: auto;
}

<!-- 语言: lang-html -->
<div class="main-container">
  <div class="sub-container">
    <p>abc</p>
  </div>
  <div class="sub-container">
    <p>abcdefghijklmnopqrstuvwxyz</p>
  </div>
</div>  

<!-- 结束代码段 -->
英文:

Let me downsize the example.

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

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

.main-container {
  margin: auto;
  width: 100px;
  height: auto;
  border: 1px solid black;
}

.sub-container {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    text-align: center;
    overflow: auto;
    width: auto;
}

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

&lt;div class=&quot;main-container&quot;&gt;
  &lt;div class=&quot;sub-container&quot;&gt;
    &lt;p&gt;abc&lt;/p&gt;
  &lt;/div&gt;

  &lt;div class=&quot;sub-container&quot;&gt;
    &lt;p&gt;abcdefghijklmnopqrstuvwxyz&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;  

<!-- end snippet -->

Here we can see that the second line is clipped off from starting and we cannot scroll to the extreme left.

This happens because in a flexbox, items will not overflow the container's boundaries.

But here is a fix.
To center align the text, instead of using justify-content: center on the container, use margin: auto on the text.

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

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

.main-container {
  margin: auto;
  width: 100px;
  height: auto;
  border: 1px solid black;
}

.sub-container {
    display: flex;
    align-items: flex-start;
    text-align: center;
    overflow: auto;
    width: auto;
}

.sub-container p {
    margin: auto;
}

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

&lt;div class=&quot;main-container&quot;&gt;
  &lt;div class=&quot;sub-container&quot;&gt;
    &lt;p&gt;abc&lt;/p&gt;
  &lt;/div&gt;

  &lt;div class=&quot;sub-container&quot;&gt;
    &lt;p&gt;abcdefghijklmnopqrstuvwxyz&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;  

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 18:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555646.html
匿名

发表评论

匿名网友

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

确定