【问题标题】:CSS: container to show PRE tag with scroll barsCSS:使用滚动条显示 PRE 标记的容器
【发布时间】:2014-04-24 10:27:15
【问题描述】:

我有一些服务器端代码可以在 html <pre></pre> 标签中输出内容,我希望能够将它们放入带有滚动条的容器中。 (特别是表格单元格 - 宽度相等)。显然我想保留<pre> 标签空白格式。

(see this jsfiddle):

HTML:

<table>
<tr>
    <th>Col 1</th>
    <th>Col 2</th>
</tr>
    <tr>
        <td><pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre></td>
        <td><pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre></td>
    </tr>
</table>

CSS:

table { 
    width: 100%; 
    border: 1px solid red;
}
td { 
    width: 50%;
    overflow: scroll; 
    border: 1px solid blue;
}

(我正在 Firefox 中测试它)。我究竟做错了什么?谢谢

【问题讨论】:

  • 你想达到什么目标?你的小提琴怎么了?
  • 表格不适合窗口,我希望它完全可见,减少列宽。在整个页面的代码中,表格位于一个固定宽度的 div 中,并且表格右侧超出 div 宽度的部分被隐藏。这在您对 JSFiddle 的看法中不明显吗?我现在也在 IE11 中对其进行了测试,我得到了与 Firefox 相同的结果

标签: html css scrollbar pre


【解决方案1】:

更新

解决此问题的最简单方法是将:table-layout:fixed;(讽刺)添加到 table 的 CSS 中

Fiddle


原答案:

table 元素中使用pre 标记是出了名的问题,即由于相对宽度计算的问题,这会直接导致滚动和实际宽度设置。因此,我会避免使用基于表格的设置,尤其是因为您也没有显示表格数据(aaah 语义!)。

我会根据以下内容更改您的布局:

Demo Fiddle

HTML

<div class='container'>
    <div class='col'> <pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre>

    </div>
    <div class='col'> <pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre>

    </div>
</div>

CSS

* {
    box-sizing:border-box;
}
html, body {
    margin:0;
    padding:0;
}
.container {
    border: 1px solid red;
    overflow:hidden;
}
.col {
    border: 1px solid blue;
    float:left;
    width:50%;
    overflow: auto;
}

更新

要伪造多行(行)表格布局,请使用div 元素并酌情设置dislpay

Demo Fiddle

HTML

<div class='table'>
    <div class='row'>
        <div class='cell'> <pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre>

        </div>
        <div class='cell'> <pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre>

        </div>
    </div>
    <div class='row'>
        <div class='cell'> <pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre>

        </div>
        <div class='cell'> <pre>
function thisIsATest(bla){
    Testing 123456789 Testing 123456789 Testing 123456789
}
        </pre>

        </div>
    </div>
</div>

CSS

* {
    box-sizing:border-box;
}
html, body {
    margin:0;
    padding:0;
}
.table {
    border: 1px solid red;
    display:table;
    table-layout:fixed;
    width:100%;
}
.cell {
    border: 1px solid blue;
    overflow: auto;
    display:table-cell;
}
.row {
    display:table-row;
}

【讨论】:

  • 谢谢。只是出于好奇,我怎样才能实现相当于多行(单元格的顶部和底部对齐)?编辑:啊,我是愚蠢的。我每行只有另一个容器 div
  • 见这里:jsfiddle.net/78s87/9 你可以使用display“伪造”一个表格布局
  • @Jodes - 这件事让我很恼火,请查看我的答案顶部的修订版,这可能会让您不必重新构建 HTML。也就是说,从语义上讲,我倾向于避免使用table,但你可以让它工作。
猜你喜欢
  • 2012-09-07
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 2013-07-26
  • 2017-11-19
  • 1970-01-01
  • 2012-06-13
相关资源
最近更新 更多