【问题标题】:Rotated content overlapping the cell border与单元格边框重叠的旋转内容
【发布时间】:2016-02-13 13:24:21
【问题描述】:

如何防止内容与单元格边框重叠,使内容留在单元格中。我不希望设置fixed 宽度,因为内容的长度可能会有所不同。

查看示例:http://jsfiddle.net/1mmh7510/5/

CSS

 .rotate {
    height: 400px;

}

.rotate > div {
    -webkit-writing-mode:vertical-rl; 
    -ms-writing-mode:tb-rl; 
    writing-mode:vertical-rl; 
  transform: rotate(-180deg);
}

HTML

<table style="background-color:#e4e6ea;" border="0" cellspacing="1">
    <tbody>
    <tr class="tableheader-row-dashboard">
        <td style="background-color:white;"width="90px"> Date</td>      
        <td style="background-color:white" width="90px">Phone No</td>
        <td style="background-color:white; vertical-align: bottom;" class="rotate" width="20px"><div>Test.. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Test.. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</div></td>
    </tr>
    <tr class="tablerow-rowd">
        <td class="text">06/11/2015</td>
        <td class="text">1234567890</td>
        <td class="text">X</td>
    </tr>
    </tbody>
</table>

编辑:

看起来 CSS 是不可能的。使用jQuery如何获取内容的宽度,然后将其设置为td的固定宽度?

【问题讨论】:

  • 您是否有理由旋转 -180 并应用书写模式,而不是旋转 -90?通过使用transform-origin: top left; transform: rotate(-90deg);,至少文本保留在框内。据我所知,如果不使用 JS,就不可能使盒子完美贴合。
  • @gummbahla 啊,我明白了。在这种情况下我需要在 JS 中做什么?
  • @I'll-Be-Back 这能解决你的问题吗jsfiddle.net/1mmh7510/8 请告诉我谢谢
  • @GibboK 不,您包含了固定宽度。这无济于事,因为每个字段都有不同的内容长度,例如 jsfiddle.net/1mmh7510/9 - 具有自动宽度将是最佳选择。

标签: javascript jquery css


【解决方案1】:

结合 JS (jQuery) 和 CSS 可以实现你想要的,虽然它不是很漂亮:http://jsfiddle.net/praz92ss/6/

HTML:

<table style="background-color:#e4e6ea;" border="0" cellspacing="1">
    <tbody>
    <tr class="tableheader-row-dashboard">
        <td style="background-color:white;"width="90px"> Date</td>      
        <td style="background-color:white" width="90px">Phone No</td>
        <td style="background-color:white;vertical-align:bottom" class="rotate"><div class="content">Test.. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. Test.. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</div></td>
    </tr>
    <tr class="tablerow-rowd">
        <td class="text">06/11/2015</td>
        <td class="text">1234567890</td>
        <td class="text">X</td>
    </tr>
    </tbody>
</table>

基本上,你需要做三件事:

1。使用 CSS 旋转文本

.rotate > div {
  //Initially, ensure that the text will not get wider than 
  //the height of our container
  max-width:400px; 

  //Do transformation around top left corner
  transform-origin: top left;
  transform: rotate(-90deg);    
}

2。用 JS 固定宽度和高度

$(document).ready(function() {
  //Fix width of container (table-cell)
  $('.rotate').css('max-width', $('.content').width());

  //Fix width and height of content
  $('.content').css('width', $('.content').width());
  $('.content').css('height', $('.rotate').width()); 
});

3。将旋转后的文本重新定位到其容器内的正确位置

  $('.content').css('margin-bottom', -$('.rotate').width());

【讨论】:

  • 有点工作但并不完美。我默认设置高度300px。然而,宽度应该扩大一点以正确填充框中的所有内容。例如:jsfiddle.net/praz92ss/5
  • 抱歉,我忘记在示例中包含 max-width 部分。现在应该修好了。
  • 几乎 :) 如果我添加更多内容更大的字段,它将不起作用:jsfiddle.net/praz92ss/7
  • 那是因为最后一列的宽度是由前一列中文字的高度决定的。您必须为单元格和内容提供不同的 id 或类来规避此问题。见jsfiddle.net/phtgx0ht
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2023-03-05
  • 2023-03-15
  • 2013-12-05
  • 2015-03-21
  • 1970-01-01
  • 2015-09-10
相关资源
最近更新 更多