Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PRE tab width in different web browsers

Tags:

css

tabs

width

pre

There's a way to set PRE tab width in Firefox and Opera, but there isn't a well-known way to do this in IE or Chrome, and hard-tabbed code in PRE tags suffers as a result.

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%;
}
like image 868
mcandre Avatar asked Jul 13 '11 23:07

mcandre


People also ask

What is the default size of tab?

The default value for the tab-size property is 8 space characters, and it can accept any positive integer value.

What is the best tab size?

Tablets that feature 7-inch or 8-inch screens are best for portability. These small devices usually weigh in under a pound and easily slip into purses or bookbags without much thought and without much weight being added. Seven-inch tablets are also the most affordable — several models come in under $100.


1 Answers

According to MDN's tab-size page, the proper format is :

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

JavaScript fallback :

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
    });
});
like image 91
OneOfOne Avatar answered Sep 20 '22 17:09

OneOfOne