代码之家  ›  专栏  ›  技术社区  ›  Florian Jenn

HTML表格中的垂直(旋转)文本

  •  118
  • Florian Jenn  · 技术社区  · 16 年前

    是否有一种(可移植的)方法可以将HTML表格单元格中的文本旋转90°?

    (我有一个表,有很多列,标题有很多文本,所以我想垂直写以节省空间。)

    11 回复  |  直到 16 年前
        1
  •  105
  •   Álvaro González    7 年前

    .box_rotate {
         -moz-transform: rotate(7.5deg);  /* FF3.5+ */
           -o-transform: rotate(7.5deg);  /* Opera 10.5 */
      -webkit-transform: rotate(7.5deg);  /* Saf3.1+, Chrome */
                 filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
             -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
        }
    <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vitae porta lectus. Suspendisse dolor mauris, scelerisque ut diam vitae, dictum ultricies est. Cras sit amet erat porttitor arcu lacinia ultricies. Morbi sodales, nisl vitae imperdiet consequat, purus nunc maximus nulla, et pharetra dolor ex non dolor.</div>
    <div class="box_rotate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vitae porta lectus. Suspendisse dolor mauris, scelerisque ut diam vitae, dictum ultricies est. Cras sit amet erat porttitor arcu lacinia ultricies. Morbi sodales, nisl vitae imperdiet consequat, purus nunc maximus nulla, et pharetra dolor ex non dolor.</div>
    <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vitae porta lectus. Suspendisse dolor mauris, scelerisque ut diam vitae, dictum ultricies est. Cras sit amet erat porttitor arcu lacinia ultricies. Morbi sodales, nisl vitae imperdiet consequat, purus nunc maximus nulla, et pharetra dolor ex non dolor.</div>

    http://css3please.com/

    截至2017年,上述网站已简化了规则集,以删除旧版Internet Explorer过滤器,并更多地依赖now标准 transform property :

    .box_rotate {
      -webkit-transform: rotate(7.5deg);  /* Chrome, Opera 15+, Safari 3.1+ */
          -ms-transform: rotate(7.5deg);  /* IE 9 */
              transform: rotate(7.5deg);  /* Firefox 16+, IE 10+, Opera */
    }
    <<div class=“长方体旋转”>Lorem ipsum dolor sit amet,是一位杰出的献身者。生命之门。多洛·莫里斯悬钩子,十字勋章,别有用心的格言等等。这是一个很好的例子。Morbi sodales、nisl vitae imperdiet consequat、purus nunc maximus nulla和pharetra dolor ex non dolor.</部门>
    <部门>Lorem ipsum dolor sit amet,是一位杰出的献身者。生命之门。多洛·莫里斯悬钩子,十字勋章,别有用心的格言等等。这是一个很好的例子。Morbi sodales、nisl vitae imperdiet consequat、purus nunc maximus nulla和pharetra dolor ex non dolor.</部门>
        2
  •  35
  •   Community CDub    4 年前

    替代方案?

    这样地:

    S  
    O  
    M  
    E  
    
    T  
    E  
    X  
    T  
    

    我认为这会容易得多——你可以把一串文本分开,然后在每个字符后面插入一个换行符。

    这可以通过浏览器中的JavaScript实现,如下所示:

    "SOME TEXT".split("").join("\n")
    

    此外,用户无需侧着头阅读:

    使现代化

    This thread 是关于使用jQuery来实现这一点的。

        3
  •  14
  •   Hugo Dozois    12 年前

    在最初的答案和我之前的答案中,IE8行中有一段引语,正好在分号附近。伊克斯和巴阿德!下面的代码正确设置了旋转,并且工作正常。你必须在IE中浮动才能应用过滤器。

    <div style="
        float: left; 
        position: relative;
        -moz-transform: rotate(270deg);  /* FF3.5+ */        
        -o-transform: rotate(270deg);  /* Opera 10.5 */   
        -webkit-transform: rotate(270deg);  /* Saf3.1+, Chrome */              
        filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=3);  /* IE6,IE7 */          
        -ms-filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3); /* IE8 */           
    "
    >Count & Value</div>;
    
        4
  •  4
  •   Stefan Steiger Marco van de Voort    12 年前

    例如(得票最多的答案):

     filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083);  /* IE6,IE7 */
         -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */
    

    所有其他提到的方法也不起作用,至少当您必须设置表格标题单元格的固定高度/宽度(带有背景色)时是不起作用的,在该单元格中,它应该自动调整到最高元素的大小。

    因此,为了详细说明Nathan Long提出的服务器端映像生成,这实际上是唯一通用的工作方法,下面是我的VB.NET通用处理程序(*.ashx)代码:

    Imports System.Web
    Imports System.Web.Services
    
    
    Public Class GenerateImage
        Implements System.Web.IHttpHandler
    
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            'context.Response.ContentType = "text/plain"
            'context.Response.Write("Hello World!")
            context.Response.ContentType = "image/png"
    
            Dim strText As String = context.Request.QueryString("text")
            Dim strRotate As String = context.Request.QueryString("rotate")
            Dim strBGcolor As String = context.Request.QueryString("bgcolor")
    
            Dim bRotate As Boolean = True
    
            If String.IsNullOrEmpty(strText) Then
                strText = "No Text"
            End If
    
    
            Try
                If Not String.IsNullOrEmpty(strRotate) Then
                    bRotate = System.Convert.ToBoolean(strRotate)
                End If
            Catch ex As Exception
    
            End Try
    
    
            'Dim img As System.Drawing.Image = GenerateImage(strText, "Arial", bRotate)
            'Dim img As System.Drawing.Image = CreateBitmapImage(strText, bRotate)
    
            ' Generic error in GDI+
            'img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
    
            'Dim bm As System.Drawing.Bitmap = New System.Drawing.Bitmap(img)
            'bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
    
            Using msTempOutputStream As New System.IO.MemoryStream
                'Dim img As System.Drawing.Image = GenerateImage(strText, "Arial", bRotate)
                Using img As System.Drawing.Image = CreateBitmapImage(strText, bRotate, strBGcolor)
                    img.Save(msTempOutputStream, System.Drawing.Imaging.ImageFormat.Png)
                    msTempOutputStream.Flush()
    
                    context.Response.Buffer = True
                    context.Response.ContentType = "image/png"
                    context.Response.BinaryWrite(msTempOutputStream.ToArray())
                End Using ' img
    
            End Using ' msTempOutputStream
    
        End Sub ' ProcessRequest
    
    
        Private Function CreateBitmapImage(strImageText As String) As System.Drawing.Image
            Return CreateBitmapImage(strImageText, True)
        End Function ' CreateBitmapImage
    
    
        Private Function CreateBitmapImage(strImageText As String, bRotate As Boolean) As System.Drawing.Image
            Return CreateBitmapImage(strImageText, bRotate, Nothing)
        End Function
    
    
        Private Function InvertMeAColour(ColourToInvert As System.Drawing.Color) As System.Drawing.Color
            Const RGBMAX As Integer = 255
            Return System.Drawing.Color.FromArgb(RGBMAX - ColourToInvert.R, RGBMAX - ColourToInvert.G, RGBMAX - ColourToInvert.B)
        End Function
    
    
    
        Private Function CreateBitmapImage(strImageText As String, bRotate As Boolean, strBackgroundColor As String) As System.Drawing.Image
            Dim bmpEndImage As System.Drawing.Bitmap = Nothing
    
            If String.IsNullOrEmpty(strBackgroundColor) Then
                strBackgroundColor = "#E0E0E0"
            End If
    
            Dim intWidth As Integer = 0
            Dim intHeight As Integer = 0
    
    
            Dim bgColor As System.Drawing.Color = System.Drawing.Color.LemonChiffon ' LightGray
            bgColor = System.Drawing.ColorTranslator.FromHtml(strBackgroundColor)
    
            Dim TextColor As System.Drawing.Color = System.Drawing.Color.Black
            TextColor = InvertMeAColour(bgColor)
    
            'TextColor = Color.FromArgb(102, 102, 102)
    
    
    
            ' Create the Font object for the image text drawing.
            Using fntThisFont As New System.Drawing.Font("Arial", 11, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel)
    
                ' Create a graphics object to measure the text's width and height.
                Using bmpInitialImage As New System.Drawing.Bitmap(1, 1)
    
                    Using gStringMeasureGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpInitialImage)
                        ' This is where the bitmap size is determined.
                        intWidth = CInt(gStringMeasureGraphics.MeasureString(strImageText, fntThisFont).Width)
                        intHeight = CInt(gStringMeasureGraphics.MeasureString(strImageText, fntThisFont).Height)
    
                        ' Create the bmpImage again with the correct size for the text and font.
                        bmpEndImage = New System.Drawing.Bitmap(bmpInitialImage, New System.Drawing.Size(intWidth, intHeight))
    
                        ' Add the colors to the new bitmap.
                        Using gNewGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpEndImage)
                            ' Set Background color
                            'gNewGraphics.Clear(Color.White)
                            gNewGraphics.Clear(bgColor)
                            gNewGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
                            gNewGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
    
    
                            ''''
    
                            'gNewGraphics.TranslateTransform(bmpEndImage.Width, bmpEndImage.Height)
                            'gNewGraphics.RotateTransform(180)
                            'gNewGraphics.RotateTransform(0)
                            'gNewGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault
    
    
                            gNewGraphics.DrawString(strImageText, fntThisFont, New System.Drawing.SolidBrush(TextColor), 0, 0)
    
                            gNewGraphics.Flush()
    
                            If bRotate Then
                                'bmpEndImage = rotateImage(bmpEndImage, 90)
                                'bmpEndImage = RotateImage(bmpEndImage, New PointF(0, 0), 90)
                                'bmpEndImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
                                bmpEndImage.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone)
                            End If ' bRotate
    
                        End Using ' gNewGraphics
    
                    End Using ' gStringMeasureGraphics
    
                End Using ' bmpInitialImage
    
            End Using ' fntThisFont
    
            Return bmpEndImage
        End Function ' CreateBitmapImage
    
    
        ' http://msdn.microsoft.com/en-us/library/3zxbwxch.aspx
        ' http://msdn.microsoft.com/en-us/library/7e1w5dhw.aspx
        ' http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=286
        ' http://road-blogs.blogspot.com/2011/01/rotate-text-in-ssrs.html
        Public Shared Function GenerateImage_CrappyOldReportingServiceVariant(ByVal strText As String, ByVal strFont As String, bRotate As Boolean) As System.Drawing.Image
            Dim bgColor As System.Drawing.Color = System.Drawing.Color.LemonChiffon ' LightGray
            bgColor = System.Drawing.ColorTranslator.FromHtml("#E0E0E0")
    
    
            Dim TextColor As System.Drawing.Color = System.Drawing.Color.Black
            'TextColor = System.Drawing.Color.FromArgb(255, 0, 0, 255)
    
            If String.IsNullOrEmpty(strFont) Then
                strFont = "Arial"
            Else
                If strFont.Trim().Equals(String.Empty) Then
                    strFont = "Arial"
                End If
            End If
    
    
            'Dim fsFontStyle As System.Drawing.FontStyle = System.Drawing.FontStyle.Regular
            Dim fsFontStyle As System.Drawing.FontStyle = System.Drawing.FontStyle.Bold
            Dim fontFamily As New System.Drawing.FontFamily(strFont)
            Dim iFontSize As Integer = 8 '//Change this as needed
    
    
            ' vice-versa, because 270° turn
            'Dim height As Double = 2.25
            Dim height As Double = 4
            Dim width As Double = 1
    
            ' width = 10
            ' height = 10
    
            Dim bmpImage As New System.Drawing.Bitmap(1, 1)
            Dim iHeight As Integer = CInt(height * 0.393700787 * bmpImage.VerticalResolution) 'y DPI
            Dim iWidth As Integer = CInt(width * 0.393700787 * bmpImage.HorizontalResolution) 'x DPI
    
            bmpImage = New System.Drawing.Bitmap(bmpImage, New System.Drawing.Size(iWidth, iHeight))
    
    
    
            '// Create the Font object for the image text drawing.
            'Dim MyFont As New System.Drawing.Font("Arial", iFontSize, fsFontStyle, System.Drawing.GraphicsUnit.Point)
            '// Create a graphics object to measure the text's width and height.
            Dim MyGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpImage)
            MyGraphics.Clear(bgColor)
    
    
            Dim stringFormat As New System.Drawing.StringFormat()
            stringFormat.FormatFlags = System.Drawing.StringFormatFlags.DirectionVertical
            'stringFormat.FormatFlags = System.Drawing.StringFormatFlags.DirectionVertical Or System.Drawing.StringFormatFlags.DirectionRightToLeft
            Dim solidBrush As New System.Drawing.SolidBrush(TextColor)
            Dim pointF As New System.Drawing.PointF(CSng(iWidth / 2 - iFontSize / 2 - 2), 5)
            Dim font As New System.Drawing.Font(fontFamily, iFontSize, fsFontStyle, System.Drawing.GraphicsUnit.Point)
    
    
            MyGraphics.TranslateTransform(bmpImage.Width, bmpImage.Height)
            MyGraphics.RotateTransform(180)
            MyGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault
            MyGraphics.DrawString(strText, font, solidBrush, pointF, stringFormat)
            MyGraphics.ResetTransform()
    
            MyGraphics.Flush()
    
            'If Not bRotate Then
            'bmpImage.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone)
            'End If
    
            Return bmpImage
        End Function ' GenerateImage
    
    
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property ' IsReusable
    
    
    End Class
    

    请注意,如果您认为此部分

            Using msTempOutputStream As New System.IO.MemoryStream
                'Dim img As System.Drawing.Image = GenerateImage(strText, "Arial", bRotate)
                Using img As System.Drawing.Image = CreateBitmapImage(strText, bRotate, strBGcolor)
                    img.Save(msTempOutputStream, System.Drawing.Imaging.ImageFormat.Png)
                    msTempOutputStream.Flush()
    
                    context.Response.Buffer = True
                    context.Response.ContentType = "image/png"
                    context.Response.BinaryWrite(msTempOutputStream.ToArray())
                End Using ' img
    
            End Using ' msTempOutputStream
    

    可以替换为

    img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)
    

    因为它在开发服务器上工作,所以如果您将它部署到Windows 2003 IIS 6服务器上,那么您就大错特错地认为相同的代码不会引发通用GDI+异常。。。

    然后像这样使用它:

    <img alt="bla" src="GenerateImage.ashx?no_cache=123&text=Hello%20World&rotate=true" />
    
        5
  •  4
  •   omardiaze    10 年前

    我对社区的第一个贡献是,仅使用html和css旋转简单文本和表头。

    enter image description here

    HTML

    <div class="rotate">text</div>
    

    CSS

    .rotate {
      display:inline-block;
      filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
      -webkit-transform: rotate(270deg);
      -ms-transform: rotate(270deg);
      transform: rotate(270deg);
    }
    

    Here an example in jsfiddle

        6
  •  3
  •   eyelidlessness    16 年前

    IE filters CSS转换( Safari Firefox ).

    IE的支持是最早的,Safari在3.1.2中有[至少一些?]支持,Firefox在3.1之前不会有支持。

        7
  •  2
  •   Cine    13 年前

    下面是一个可用于纯文本的服务器端处理:

    public string RotateHtmltext(string innerHtml)
    {
       const string TRANSFORMTEXT = "transform: rotate(90deg);";
       const string EXTRASTYLECSS = "<style type='text/css'>.r90 {"
                                     + "-webkit-" + TRANSFORMTEXT
                                     + "-moz-" + TRANSFORMTEXT
                                     + "-o-" + TRANSFORMTEXT
                                     + "-ms-" + TRANSFORMTEXT
                                     + "" + TRANSFORMTEXT
                                     + "width:1em;line-height:1ex}</style>";
       const string WRAPPERDIV = "<div style='display: table-cell; vertical-align: middle;'>";
    
       var newinnerHtml = string.Join("</div>"+WRAPPERDIV, Regex.Split(innerHtml, @"<br */?>").Reverse());
    
       newinnerHtml = Regex.Replace(newinnerHtml, @"((?:<[^>]*>)|(?:[^<]+))",
                                     match => match.Groups[1].Value.StartsWith("<")
                                                 ? match.Groups[1].Value
                                                 : string.Join("", match.Groups[1].Value.ToCharArray().Select(x=>"<div class='r90'>"+x+"</div>")),
                                     RegexOptions.Singleline);
       return EXTRASTYLECSS + WRAPPERDIV + newinnerHtml + "</div>";
    }
    

    它给出了类似于:

    <style type="text/css">.r90 {
    -webkit-transform: rotate(90deg);
    -moz-transform: rotate(90deg);
    -o-transform: rotate(90deg);
    -ms-transform: rotate(90deg);
    transform: rotate(90deg);
    width: 1em;
    line-height: 1ex; 
    }</style>
    <div style="display: table-cell; vertical-align: middle;">
    <div class="r90">p</div>
    <div class="r90">o</div>
    <div class="r90">s</div>
    </div><div style="display: table-cell; vertical-align: middle;">
    <div class="r90">(</div>
    <div class="r90">A</div>
    <div class="r90">b</div>
    <div class="r90">s</div>
    <div class="r90">)</div>
    </div>
    

    http://jsfiddle.net/TzzHy/

        8
  •  1
  •   Jeff Ancel    11 年前

    我使用的是Font Awesome库,通过在任何html元素中添加以下内容,我能够实现这一效果。

    <div class="fa fa-rotate-270">
      My Test Text
    </div>
    

        9
  •  1
  •   arkod    10 年前

    另一个解决方案:

    (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);
    
    }) ();
    

    我绝对承认这是一个非常粗糙的方法,但是如果你想避免你的css膨胀的话,这是一个简单的解决方案。

        10
  •  0
  •   MrFusion    13 年前
    -moz-transform: rotate(7.5deg);  /* FF3.5+ */
    -o-transform: rotate(7.5deg);  /* Opera 10.5 */
    -webkit-transform: rotate(7.5deg);  /* Saf3.1+, Chrome */
    filter:  progid:DXImageTransform.Microsoft.BasicImage(rotation=1);  /* IE6,IE7 allows only 1, 2, 3 */
    -ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=1)"; /* IE8 allows only 1 2 or 3*/
    
        11
  •  0
  •   Community CDub    8 年前

    完全是一个很酷的解决方案,只用于css的共鸣

    谢谢你的建议

    heres the link

    这是我遇到的堆栈溢出链接 link meow

             .vertical-text-vibes{
    
                    /* this is for shity "non IE" browsers
                       that dosn't support writing-mode */
                    -webkit-transform: translate(1.1em,0) rotate(90deg);
                       -moz-transform: translate(1.1em,0) rotate(90deg);
                         -o-transform: translate(1.1em,0) rotate(90deg);
                            transform: translate(1.1em,0) rotate(90deg);
                    -webkit-transform-origin: 0 0;
                       -moz-transform-origin: 0 0;
                         -o-transform-origin: 0 0;
                            transform-origin: 0 0;  
                 /* IE9+ */    ms-transform: none;    
                       -ms-transform-origin: none;    
            /* IE8+ */    -ms-writing-mode: tb-rl;    
       /* IE7 and below */    *writing-mode: tb-rl;
    
                }