【问题标题】:CSS - Rotated text with overflow ellipsisCSS - 带有溢出省略号的旋转文本
【发布时间】:2019-11-25 14:27:12
【问题描述】:

如果文本太长,我需要旋转和截断:

但如果我在上面应用省略号:

overflow: hidden;
text-overflow: ellipsis;

旋转后的文字会太短:

.box {
  position: relative;
  width: 300px;
}

.box-header {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  padding: 10px;
  font-weight: bold;
  background: #ccc;
  color: red;
  min-width: 0;
}

.box-header > div {
  transform: rotate(-90deg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 1px solid red;
}

.box-content {
  margin-left: 40px;
  padding: 10px;
  background: #eee;
}

.some-content {
  height: 100px;
}
<div class="box">
  <div class="box-header">
    <div>Too long header text</div>
  </div>
  <div class="box-content">
    <div class="some-content">Some content</div>
  </div>
</div>

【问题讨论】:

    标签: html css css-transforms ellipsis


    【解决方案1】:

    考虑writing-mode 来切换文本的方向,然后添加height:100% 来限制高度并允许省略号起作用。最后添加一个180deg 旋转来获得你想要的文本:

    .box {
      position: relative;
      width: 300px;
    }
    
    .box-header {
      position: absolute;
      top: 0;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 20px;
      padding: 10px;
      font-weight: bold;
      background: #ccc;
      color: red;
      min-width: 0;
    }
    
    .box-header > div {
      writing-mode:vertical-lr;
      transform:rotate(180deg);
      height: 100%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      border: 1px solid red;
    }
    
    .box-content {
      margin-left: 40px;
      padding: 10px;
      background: #eee;
    }
    
    .some-content {
      height: 100px;
    }
    <div class="box">
      <div class="box-header">
        <div>Too long header text</div>
      </div>
      <div class="box-content">
        <div class="some-content">Some content</div>
      </div>
    </div>


    您的代码的问题是旋转只会进行视觉转换,不会真正改变文本的方向。省略号/溢出会将代码视为没有旋转。

    .box {
      position: relative;
      width: 300px;
    }
    
    .box-header {
      position: absolute;
      top: 0;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 20px;
      padding: 10px;
      font-weight: bold;
      background: #ccc;
      color: red;
      min-width: 0;
    }
    
    .box-header > div {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      border: 1px solid red;
    }
    
    .box-content {
      margin-left: 40px;
      padding: 10px;
      background: #eee;
    }
    
    .some-content {
      height: 100px;
    }
    <div class="box">
      <div class="box-header">
        <div>Too long header text</div>
      </div>
      <div class="box-content">
        <div class="some-content">Some content</div>
      </div>
    </div>

    【讨论】:

    • 但是如果需要特定的角度变换怎么办?不仅仅是垂直(90 度),比如说 45 度
    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2012-04-13
    • 2013-07-20
    • 2015-07-06
    相关资源
    最近更新 更多