Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate contenteditable with extra space

Tags:

html

css

I have a simple div with contentEditable true attribute and I also apply ellipsis CSS.

The behavior of contentEditable it's fine when the content is less and not truncated.

enter image description here

But when you write extra content and content starts truncated it'll show extra whitespace on the right side.

enter image description here

enter image description here

div {
  border-bottom: 2px solid #ccc;  
  outline: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-bottom: 5px;
  width: 350px;
}
<div contentEditable="true"></div>
like image 407
Hassan Siddiqui Avatar asked Apr 10 '19 04:04

Hassan Siddiqui


1 Answers

Here is a possible solution for this. you might need to change your HTML structure little. Take a parent element and wrap your truncated element into it and play with focus attribute.

Here is an example.

div {
  overflow: hidden;
  width: 350px;
  display: inline-block;
  position: relative;
  border-bottom: 2px solid #ccc;
}

span {
  outline: 0;
  width: auto;
  white-space: nowrap;
  padding-bottom: 5px;
  display: inline-block;
  overflow: hidden;
}

span:not(:focus) {
  width: 100%;
  margin-bottom: 2px;
  text-overflow: ellipsis;
}
<div>
  <span contenteditable="true"></span>
</div>
like image 65
Ismail Farooq Avatar answered Oct 27 '22 00:10

Ismail Farooq