Vertical (rotated) text in HTML table

前端 未结 11 1554
长发绾君心
长发绾君心 2020-11-28 20:30

Is there a (portable) way to rotate text in a HTML table cell by 90°?

(I have a table with many columns and much text for the headings, so I\'d like to write it vert

11条回答
  •  -上瘾入骨i
    2020-11-28 21:17

    Another solution:

    (function () {
    
        var make_rotated_text = function (text)
        {
            var can = document.createElement ('canvas');
            can.width = 10;
            can.height = 10;
            var ctx=can.getContext ("2d");
            ctx.font="20px Verdana";
            var m = ctx.measureText(text);
            can.width = 20;
            can.height = m.width;
            ctx.font="20px Verdana";
            ctx.fillStyle = "#000000";
            ctx.rotate(90 * (Math.PI / 180));
            ctx.fillText (text, 0, -2);
            return can;
        };
    
        var canvas = make_rotated_text ("Hellooooo :D");
        var body = document.getElementsByTagName ('body')[0];
        body.appendChild (canvas);
    
    }) ();
    

    I do absolutely admit that this is quite hackish, but it's a simple solution if you want to avoid bloating your css.

提交回复
热议问题