Html 如何使用 CSS 跨浏览器绘制垂直文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1080792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:07:06  来源:igfitidea点击:

How can I draw vertical text with CSS cross-browser?

htmlcsscross-browser

提问by usr

I want to rotate a single word of text by 90 degrees, with cross-browser (>= IE6, >= Firefox 2, any version of Chrome, Safari, or Opera) support. How can this be done?

我想将一个文本单词旋转 90 度,并支持跨浏览器(>= IE6、>= Firefox 2、任何版本的 Chrome、Safari 或 Opera)。如何才能做到这一点?

采纳答案by Robert K

Updated this answer with recent information (from CSS Tricks). Kudos to Matt and Douglas for pointing out the filter implementation.

用最近的信息更新了这个答案(来自CSS Tricks)。感谢 Matt 和 Douglas 指出过滤器的实现。

.rotate {
  -webkit-transform: rotate(-90deg);
  -moz-transform: rotate(-90deg);
  -ms-transform: rotate(-90deg);
  -o-transform: rotate(-90deg);
  transform: rotate(-90deg);

  /* also accepts left, right, top, bottom coordinates; not required, but a good idea for styling */
  -webkit-transform-origin: 50% 50%;
  -moz-transform-origin: 50% 50%;
  -ms-transform-origin: 50% 50%;
  -o-transform-origin: 50% 50%;
  transform-origin: 50% 50%;

  /* Should be unset in IE9+ I think. */
  filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
}

Old answer:

旧答案:

For FF 3.5 or Safari/Webkit 3.1, check out: -moz-transform(and -webkit-transform). IE has a Matrix filter(v5.5+), but I'm not certain how to use it. Opera has no transformation capabilities yet.

对于 FF 3.5 或 Safari/Webkit 3.1,请查看:-moz-transform(和 -webkit-transform)。IE 有一个矩阵过滤器(v5.5+),但我不确定如何使用它。Opera 还没有转换功能。

.rot-neg-90 {
  /* rotate -90 deg, not sure if a negative number is supported so I used 270 */
  -moz-transform: rotate(270deg);
  -moz-transform-origin: 50% 50%;
  -webkit-transform: rotate(270deg);
  -webkit-transform-origin: 50% 50%;
  /* IE support too convoluted for the time I've got on my hands... */
}

回答by Choesang

I am using the following code to write vertical text in a page. Firefox 3.5+, webkit, opera 10.5+ and IE

我正在使用以下代码在页面中编写垂直文本。Firefox 3.5+、webkit、opera 10.5+ 和 IE

.rot-neg-90 {
    -moz-transform:rotate(-270deg); 
    -moz-transform-origin: bottom left;
    -webkit-transform: rotate(-270deg);
    -webkit-transform-origin: bottom left;
    -o-transform: rotate(-270deg);
    -o-transform-origin:  bottom left;
    filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
}

回答by Jenny O'Reilly

Another solution is to use an SVG text nodewhich is supported by most browsers.

另一种解决方案是使用大多数浏览器支持SVG 文本节点

<svg width="50" height="300">
    <text x="28" y="150" transform="rotate(-90, 28, 150)" style="text-anchor:middle; font-size:14px">This text is vertical</text>
</svg>

Demo:https://jsfiddle.net/bkymb5kr/

演示:https : //jsfiddle.net/bkymb5kr/

More on SVG text:http://tutorials.jenkov.com/svg/text-element.html

更多关于 SVG 文本:http : //tutorials.jenkov.com/svg/text-element.html

回答by Oriol

The CSS Writing Modesmodule introduces orthogonal flows with vertical text.

CSS写作模式模块引入了垂直文本正交流动。

Just use the writing-modeproperty with the desired value.

只需使用writing-mode具有所需值的属性即可。

span { margin: 20px; }
#vertical-lr { writing-mode: vertical-lr; }
#vertical-rl { writing-mode: vertical-rl; }
#sideways-lr { writing-mode: sideways-lr; }
#sideways-rl { writing-mode: sideways-rl; }
<span id="vertical-lr">
  ↑ (1) vertical-lr 至<br />
  ↑ (2) vertical-lr 至<br />
  ↑ (3) vertical-lr 至
</span>
<span id="vertical-rl">
  ↓ (1) vertical-rl 至<br />
  ↓ (2) vertical-rl 至<br />
  ↓ (3) vertical-rl 至
</span>
<span id="sideways-lr">
  ↓ (1) sideways-lr 至<br />
  ↓ (2) sideways-lr 至<br />
  ↓ (3) sideways-lr 至
</span>
<span id="sideways-rl">
  ↓ (1) sideways-rl 至<br />
  ↓ (2) sideways-rl 至<br />
  ↓ (3) sideways-rl 至
</span>

回答by john

I adapted this from http://snook.ca/archives/html_and_css/css-text-rotation:

我从http://snook.ca/archives/html_and_css/css-text-rotation改编了这个:

<style>
    .Rotate-90
    {
        display: block;
        position: absolute;
        right: -5px;
        top: 15px;
        -webkit-transform: rotate(-90deg);
        -moz-transform: rotate(-90deg);
    }
</style>
<!--[if IE]>
    <style>
        .Rotate-90 {
            filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
            right:-15px; top:5px;
        }
    </style>
    <![endif]-->

回答by Mark Rhodes

I've had problems trying to do it in pure CSS - depending on the font it can look a bit rubbish. As an alternative you can use SVG/VML to do it. There are libraries that help make it cross browser with ease e.g. Raphaeland ExtJS. In ExtJS4 the code looks like this:

我在尝试使用纯 CSS 时遇到了问题 - 根据字体的不同,它可能看起来有点垃圾。作为替代方案,您可以使用 SVG/VML 来做到这一点。有一些库可以帮助它轻松跨浏览器,例如RaphaelExtJS。在 ExtJS4 中,代码如下所示:

    var drawComp = Ext.create('Ext.draw.Component', {
        renderTo: Ext.getBody(), //or whatever..
        height: 100, width: 100 //ditto..
    });
    var text = Ext.create('Ext.draw.Component', {
        type: "text",
        text: "The text to draw",
        rotate: {
            x: 0, y: 0, degrees: 270
        },
        x: -50, y: 10 //or whatever to fit (you could calculate these)..
    });
    text.show(true);

This will work in IE6+ and all modern browsers, however, unfortunately I think you need at least FF3.0.

这将适用于 IE6+ 和所有现代浏览器,但是,不幸的是,我认为您至少需要 FF3.0。

回答by Andreas Fr?wis

If you use Bootstrap 3, you can use one of it's mixins:

如果你使用 Bootstrap 3,你可以使用它的 mixin 之一:

.rotate(degrees);

Example:

例子:

.rotate(-90deg);

回答by Devner

My solution that would work on Chrome, Firefox, IE9, IE10 (Change the degrees as per your requirement):

我的解决方案适用于 Chrome、Firefox、IE9、IE10(根据您的要求更改度数):

.rotate-text {
  -webkit-transform: rotate(270deg);
  -moz-transform: rotate(270deg);
  -ms-transform: rotate(270deg);
  -o-transform: rotate(270deg);
  transform: rotate(270deg);
  filter: none; /*Mandatory for IE9 to show the vertical text correctly*/      
}

回答by aGuegu

If CSS writing-mode: sideways-lris what you prefer, and you happen to run into chromium/chrome based browser. You may try

如果 CSS书写模式:sideways-lr是你喜欢的,而你碰巧遇到了基于铬/铬的浏览器。你可以试试

{
  writing-mode: vertical-rl; 
  transform: rotate(180deg);
}

so all modern browsers support it now.

所以现在所有的现代浏览器都支持它。

reference: https://bugs.chromium.org/p/chromium/issues/detail?id=680331#c4

参考:https: //bugs.chromium.org/p/chromium/issues/detail?id=680331#c4