PrismJs不会随CSS网格溢出

时间:2019-04-18 01:52:10

标签: html css css3 css-grid prismjs

我有一个页面布局,右边有一个弹性的内容div和一个固定宽度的列。为此,我具有以下基本结构:

<div class="grid">
  <div class="content">
    Content here
  </div>
  <div class="column">
    Fixed right column
  </div>
</div>

和CSS:

.grid {
    display: grid;
    grid-gap: 10px;
    grid-template-columns: auto 280px;
    width: 500px;
}

.content {
  background: tomato;
}

.column {
  background: deepskyblue;
}

我希望auto的内容可以填满总空间减去固定的列宽。确实发生了这种情况,除非div的内容(作为棱镜标记)超出了该宽度。 它没有在PrismJs div上生成水平滚动条,而是仅使内容div溢出并将列向右推。

是否有关于如何使滚动条显示在div而不是按下列的想法?

请注意,如果我为内容宽度设置一个固定值,它将按预期工作。

这里是一个示例:

.grid {
    display: grid;
    grid-gap: 10px;
    grid-template-columns: auto 280px;
    width: 500px;
}

.content {
  background: tomato;
}

.column {
  background: deepskyblue;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/prism/1.2.0/themes/prism.css">
    <link href="https://cdn.jsdelivr.net/prism/1.2.0/themes/prism-coy.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/prism/1.2.0/prism.js"></script>


<div class="grid">
  <div class="content">
    <pre><code class="language-javascript">@Component({
        selector: "main",
        directives: [],
        template: `
            &lt;button
                (tap) = "onTapMe()"
                text = "tap me" /&gt;
        `
    })
    class MainPage {
        onTapMe() {
            alert("Hello from Angular 2");
        }
        // long coment pushing the right column further right !!!
    }
    </code></pre>
  </div>
  <div class="column">
  
  </div>
</div>

这里有个玩弄的小提琴:https://jsfiddle.net/4crm25pq/1/

1 个答案:

答案 0 :(得分:0)

默认情况下,网格项具有min-width: automin-height: auto(就像 flex项)。因此,您可以设置min-width: 0并将oveflow: auto添加到 fix 中-参见下面的演示:

.grid {
    display: grid;
    grid-gap: 10px;
    grid-template-columns: auto 280px;
    width: 500px;
}

.content {
  background: tomato;
  min-width: 0; /* added */
  overflow: auto; /* added */
}

.column {
  background: deepskyblue;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/prism/1.2.0/themes/prism.css">
    <link href="https://cdn.jsdelivr.net/prism/1.2.0/themes/prism-coy.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/prism/1.2.0/prism.js"></script>


<div class="grid">
  <div class="content">
    <pre><code class="language-javascript">@Component({
        selector: "main",
        directives: [],
        template: `
            &lt;button
                (tap) = "onTapMe()"
                text = "tap me" /&gt;
        `
    })
    class MainPage {
        onTapMe() {
            alert("Hello from Angular 2");
        }
        // long coment pushing the right column further right !!!
    }
    </code></pre>
  </div>
  <div class="column">
  
  </div>
</div>

您可以在下面看到这种行为的一些示例: