10

在 Firefox 和 Opera 中有一种设置 PRE 选项卡宽度的方法,但在 IE 或 Chrome 中没有一种众所周知的方法来执行此操作,因此 PRE 标记中的硬选项卡代码会受到影响。

pre {
    white-space: -moz-pre-wrap;
    white-space: -pre-wrap;
    white-space: -o-pre-wrap;
    white-space: pre-wrap;
    word-wrap: break-word;
    -moz-tab-size: 1.5em;
    -o-tab-size: 1.5em;

    margin: 1em 0 0 0;
    padding: 1em 1em 1em 1em;
    width: 65%;
}
4

2 回答 2

7

根据 MDN's tab-size page,正确的格式是:

tab-size: 4;
-moz-tab-size: 4;
-o-tab-size: 4;

JavaScript 后备:

var fix_tab_size = function(pre, size) {
    if(typeof fix_tab_size.supports === 'undefined') {
        var bs = document.body.style;
        fix_tab_size.supports = ('tab-size' in bs || '-o-tab-size' in bs || '-ms-tab-size' in bs || '-moz-tab-size'  in bs);
    }
    if(!fix_tab_size.supports) {
        if(typeof pre.each === 'function') { //jquery 
            $('pre').each(function() {
                var t = $(this);
                t.html(t.html().replace(/\t/g, new Array(size+1).join(' ')));
            });
        } else if(typeof pre.innerHTML === 'string') {
            pre.innerHTML = pre.innerHTML.replace(/\t/g, new Array(size+1).join(' '));
        }
    }
}
$(function() {
    fix_tab_size($('pre'),4);
    //or
    $.getJSON(src, function(data) {
        fix_tab_size($data_pre.html(data.code)); //etc
    });
});
于 2013-09-14T13:42:11.693 回答
-2

CSS 3 文本功能tab-size太新(而且还不够标准化),到目前为止还没有看到广泛的部署(加上标签通常不是很流行)。

对于硬标签代码的情况,只需在将其放在页面上之前通过制表符到空格的转换运行它。

基本布局比圆角更有用

选项卡从未打算执行通用布局任务(并且完全不适合这样做)。有更强大的 CSS3 布局功能被吹捧——例如 IE10 中的 Grid 和 Flexbox。

于 2011-07-13T23:28:20.233 回答