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


当前回答

我改编自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]-->

其他回答

更新这个答案与最近的信息(从CSS技巧)。感谢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);
}

旧的回答:

对于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... */
}

如果你使用Bootstrap 3,你可以使用它的一个mixins:

.rotate(degrees);

例子:

.rotate(-90deg);

我的解决方案,将工作在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*/      
}

另一种解决方案是使用大多数浏览器都支持的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>

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

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

如果CSS写作模式:sideway -lr是你喜欢的,而你恰巧遇到了基于chromium/chrome的浏览器。你可以试试

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

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

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