Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Rotated text with overflow ellipsis

I have a text that I need to rotate and truncate if it's too long:

Overflowing rotated text

But if I apply ellipsis on it:

overflow: hidden;
text-overflow: ellipsis;

The rotated text will be too short:

Too short rotated text

.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>
like image 775
Koralek M. Avatar asked Jul 16 '19 19:07

Koralek M.


People also ask

How do I fix text-overflow in CSS?

A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.

How do you change text-overflow on ellipsis?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do you rotate text in CSS?

Rotate text can be done by using the rotate() function. We can rotate the text in clockwise and anti-clockwise direction. The rotate function can also rotate HTML elements as well.

What is ellipsis in CSS?

The CSS ellipsis kind of is the value that mostly is used by the property: text-overflow. What is CSS Ellipsis? How to use CSS Ellipsis? How CSS ellipsis value is used by text-overflow property? Why text-overflow ellipsis doesn’t work? What is CSS Ellipsis?

What is the ellipsis of an email address?

The ellipsis is the 3 dots … Now the user can see the layout properly and thanks to the CSS ellipsis they’re aware that there’s more to the email addresses than is being shown. Note: this works only when the overflow and text-overflow properties are used together.

How do I handle text overflow in a container?

When a string of text overflows the boundaries of a container it can make a mess of your whole layout. Here’s a cool trick to handle text overflow by truncating long strings with a CSS ellipsis. During this quick tip we’ll use the following demo to show how text overflow works:

How can I change the direction of the text in HTML?

Consider writing-mode to switch the direction of the text then add height:100% to restrict the height and allow the ellipsis to work. Finally add a 180deg rotation to have the text like you want: The issue with your code is that the rotation will only do a visual transformation and will not really change the direction of the text.


1 Answers

Consider writing-mode to switch the direction of the text then add height:100% to restrict the height and allow the ellipsis to work. Finally add a 180deg rotation to have the text like you want:

.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>

CSS rotated text with overflow ellipsis


The issue with your code is that the rotation will only do a visual transformation and will not really change the direction of the text. The ellipsis/overflow will consider the code as if there is no rotation.

.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>
like image 59
Temani Afif Avatar answered Sep 22 '22 21:09

Temani Afif