Text ellipsis at start of string with CSS? - html

Getting this effect with CSS it's easy:
This is a very long para...
We just just use text-overflow:ellipsis.
However the reverse
... is a very long paragraph.
seems less obvious.
I have read this comprehensive article but the solution give there is still less than ideal.
Here's the CSS to implement it
.reverse-ellipsis {
text-overflow: clip;
position: relative;
background-color: white;
}
.reverse-ellipsis:before {
content: '\02026';
position: absolute;
z-index: 1;
left: -1em;
background-color: inherit;
padding-left: 1em;
margin-left: 0.5em;
}
.reverse-ellipsis span {
min-width: 100%;
position: relative;
display: inline-block;
float: right;
overflow: visible;
background-color: inherit;
text-indent: 0.5em;
}
.reverse-ellipsis span:before {
content: '';
position: absolute;
display: inline-block;
width: 1em;
height: 1em;
background-color: inherit;
z-index: 200;
left: -.5em;
}
The main problem with it is its length and the fact that the ellipsis looks a bit off.
Does anyone know of a shorter solution that keeps the ellipsis in line?

As per this documentation its possible now🤓. I have added an working example below.
{
direction: rtl;
text-overflow: ellipsis;
}
div {
margin: 5px;
background: #ccc;
padding: 10px;
width: 500px;
}
.box {
line-height: 50px;
border: 1px solid #000;
overflow: hidden;
white-space: nowrap;
font-family: sans-serif;
padding: 0 0.5em;
text-align: left;
}
.el-normal {
text-overflow: ellipsis;
}
.el-reverse {
direction: rtl;
text-overflow: ellipsis;
}
<div>
<h3>Normal</h3>
<p class="box el-normal">
Getting this effect with CSS it's easy: This is a very long para We just just use text-overflow:ellipsis. However the reverse is a very long paragraph. seems less obvious.
</p>
<h3>Reverse</h3>
<p class="box el-reverse">
Getting this effect with CSS it's easy: This is a very long para We just just use text-overflow:ellipsis. However the reverse is a very long paragraph. seems less obvious.
</p>
</div>

Related

Span width is not reduced after ellipsis in CSS

I have 1 parent li element with 2 child spans in it.
My first span has large title so i applied ellipsis to the parent li.
My issue is when the 1st span is ellipsis, The 2nd span which should aligned next to it is being place after the full width of 1st span.
To be more clear, 1st span is title and 2nd span is count.
#title {
color: black;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 200px;
}
#view-count {
font-size: 10px;
margin-left: 4px;
border-radius: 10px;
line-height: 1;
position: absolute;
top: 30%;
}
<li id="title" title="Documentationxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
<span>Documentationxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span>
<span id="view-count">370</span>
</li>
Below is the JSFiddle for it.
JSFiddle
Any help?
You have to apply ellipsis to first span and have to remove position from second span.
Check snippet.
#title {
color: black;
position: relative;
width: 240px;
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 200px;
}
span {
display: inline-block;
}
#view-count {
font-size: 10px;
margin-left: 4px;
border-radius: 10px;
line-height: 1;
position: absolute;
z-index: 1;
background: #ff0000;
top: -5px;
right: 0;
}
<li id="title" title="Documentationxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
<span class="ellipsis">Documentationxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</span>
<span id="view-count">370</span>
</li>
You can use flex for this:
.title {
display: flex;
width: 200px;
}
.title > span:nth-child(1) {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: 1 1 auto;
background-color: #FC0;
}
.title > span:nth-child(2) {
flex: 0 0 auto;
background-color: #CF0;
}
<ul>
<li class="title">
<span>Documentation documentation documentation documentation documentation documentation</span>
<span>370</span>
</li>
<li class="title">
<span>Documentation</span>
<span>370</span>
</li>
</ul>

How to maintain layered absolute div alignment and sizing while scrolling and resizing?

I have a need to make wysiwyg editor as such:
A transparent textarea, with a transparent font is the editing component.
It is layered over a div which gets its value from the textarea and renders it.
editing interactions occur in the textarea but the view comes from the underlying div.
This way, we can render syntax highlighted text, while maintaining editing functionality of a textarea.
My problem is my lack of expertise in CSS. So no matter how many variations of this I try, there is always a problem with word wrapping, or scrolling, or alignments getting out of sync.
Supplied below is one variation. The editor component has transparent grey text while the underlying div is orange text, so we can see when they line up.
The challenge is to make them always line up, no matter what the content or scrolling or sizing.
function update(){
document.getElementById("richtext").innerHTML =
document.getElementById("editor").value;
}
.scrollable {
width: 300px;
height: 100px;
overflow: scroll;
border : 1px solid grey;
}
.content {
font-family: monospace;
font-size: 18px;
position: absolute;
top: 0;
left: 0;
border: none;
overflow: hidden;
min-width: 100%;
min-height: 100%;
outline: none;
resize: none;
margin: none;
padding: 0;
box-shadow: none;
box-sizing: border-box;
white-space: pre;
display: block;
overflow: none;
}
#richtext {
z-index: -10;
color: orange;
}
#view {
height: 100%;
position: relative;
display: inline-block;
}
#editor {
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: transparent;
color: #00000055;
caret-color: black;
}
<div class="scrollable">
<div id="view" class="content">
<textarea id="editor" class="content" onInput="update();" class="content"></textarea>
<div id="richtext" class="content"></div>
</div>
</div>
Do you NEED the textarea and underlying div? I was trying to find a good rich text editor recently and noticed that many of the modern ones use, just a div with contenteditable="true" as an HTML5 attribute. Here are some good examples of this in the real world.
If you really need to stick with the two elements, I'd tear them both down to bare metal by resetting ALL attributes:
textarea,
.preview {
all: initial;
}
Then build up the text styling so it's completely the same on both. font-family, font-size, font-weight, line-height, etc. I'd also make sure there's a good CSS reset or normalize in place.
The problem is that browsers treat text rendering differently in a textarea than elsewhere so the goal would be to force them to be the same by being a bit heavy-handed.
Change class .content min-height:100%; to min-height:100vh; and also change white-space: pre; to white-space: pre-wrap;
Here is the updated fiddle:
function update() {
document.getElementById("richtext").innerHTML =
document.getElementById("editor").value;
}
.scrollable {
width: 300px;
height: 100px;
overflow: scroll;
border: 1px solid grey;
}
.content {
font-family: monospace;
font-size: 18px;
position: absolute;
top: 0;
left: 0;
border: none;
overflow: hidden;
min-width: 100%;
min-height: 100vh;
outline: none;
resize: none;
margin: none;
padding: 0;
box-shadow: none;
box-sizing: border-box;
white-space: pre-wrap;
display: block;
overflow: none;
}
#richtext {
z-index: -10;
color: orange;
}
#view {
height: 100%;
position: relative;
display: inline-block;
}
#editor {
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: transparent;
color: #00000055;
caret-color: black;
}
<div class="scrollable">
<div id="view" class="content">
<textarea id="editor" class="content" onInput="update();" class="content"></textarea>
<div id="richtext" class="content"></div>
</div>
</div>
You have used the absolute position. Thus div and textarea are placed on each other. remove position, min-width and max-width for div and textarea. and use display:flex-inline for div#view;
you can change value of white-space for better result.
The alignment gets out of sync because eventually you will exceed the defined height of the textarea and its internal scrolling kicks in.
To avoid this, update the textarea's height, so it extends automatically.
function update(){
richtext.innerHTML = editor.value;
editor.style.width = 'auto';
editor.style.width = editor.scrollWidth+'px';
editor.style.height = 'auto';
editor.style.height = editor.scrollHeight+'px';
}
var richtext = document.getElementById("richtext")
var editor = document.getElementById("editor")
function update(){
richtext.innerHTML = editor.value;
editor.style.width = 'auto';
editor.style.width = editor.scrollWidth+'px';
editor.style.height = 'auto';
editor.style.height = editor.scrollHeight+'px';
}
.scrollable {
width: 300px;
height: 100px;
overflow: scroll;
border : 1px solid grey;
}
.content {
font-family: monospace;
font-size: 18px;
position: absolute;
top: 0;
left: 0;
border: none;
overflow: hidden;
min-width: 100%;
min-height: 100%;
outline: none;
resize: none;
margin: none;
padding: 0;
box-shadow: none;
box-sizing: border-box;
white-space: pre;
display: block;
overflow: none;
}
#richtext {
z-index: -10;
color: orange;
}
#view {
height: 100%;
position: relative;
display: inline-block;
}
#editor {
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: transparent;
color: #00000055;
caret-color: black;
}
<div class="scrollable">
<div id="view" class="content">
<textarea id="editor" class="content" onInput="update();" class="content"></textarea>
<div id="richtext" class="content"></div>
</div>
</div>
Edit: same goes for the width, obviously.

Why is that inline-block parent of an no-wrap ellipsis child always seems to take place for complete text?

Please check this example JsBin, here we have simple layout we have a child which have too long text and we need to make it no-wrap ellipsis to avoid breaking of layout but parent seems to occupy the width more then (probably equal to the text) the actual displayed text.
Below is the code
HTML
<div class="title-logo-container" >
<span class="logo">
<a href="/" >
<img src="" alt="LOGO IMAGE">
</a>
</span>
<p class="page-title" s>
test test test test test test test test test test test test test test test
</p>
CSS
.title-logo-container {
border: solid 1px #f00;
display:inline-block;
float:left;
}
.logo {
margin: 1.375em 1.5625em 15px;
padding: 0;
position: relative;
width: 5.625em;
z-index: 103;
display: inline-block;
}
.page-title {
max-width:40%;
display: inline-block;
margin: 0;
font-weight: 400;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
Please suggest.
Expected Output
You're specifying a percentage max-width for an inline-block that is a child of a float that doesn't have an explicit width. This results in undefined behavior because there is a circular dependency between the parent (float) width and the child (inline-block) width.
The apparent browser behavior is that the float is shrink-wrapped to the size of its contents — just enough to contain the content in one line — first, so that it has a size on which the inline-block can then base its 40% max-width. The inline-block then clips its content via overflow: hidden. The size of the float is not computed again.
As BoltClock said, I dont think here inline-block works for this situation, you can try table like this:
Demo
.title-logo-container {
clear: both;
border: solid 1px #f00;
background: green;
display: table-cell;
}
.logo {
margin: 1.375em 1.5625em 15px;
padding: 0;
position: relative;
width: 5.625em;
z-index: 103;
display: table-cell;
background: #ff0;
}
.page-title {
max-width: 40%;
display: table-cell;
font-weight: 400;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
background: #ccc;
}
Hope this helps you !
Check out below solution:
Demo
CSS:
.title-logo-container {
border: solid 1px #f00;
display:inline-block;
float:left;
width:100%;
}
.logo {
margin: 1.375em 1.5625em 15px;
padding: 0;
position: relative;
width: 100px;
z-index: 103;
display: inline-block;
}
.page-title {
width: calc(100% - 160px);
display: inline-block;
margin: 0;
font-weight: 400;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}

Text overflow with ellipsis on the left [duplicate]

This question already has answers here:
Text-overflow ellipsis on left side
(10 answers)
Closed 7 years ago.
Consider this html/css snippet:
.l { text-align: left; }
.r { text-align: right; }
p {
width: 150px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
border: solid 1px green;
}
<p class="l">111222333444555666777888999</p>
<p class="r">111222333444555666777888999</p>
It shows two fixed-width containers with some text too long to fit, with overflow set to show an ellipsis to show that some text is hidden. The first container is left justified, the second is right justified.
The result shows that the ellipsis is on the right for both examples.
However, for the second right justified one, I'd like to achieve this:
...4555666777888999
instead of
1112223334445556...
Is this possible?
You can set the direction of text from right to left using css direction property direction: rtl:
.l {
text-align: left;
direction: rtl;
}
.r {
text-align: right;
}
p {
width: 150px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
border: solid 1px green;
}
<p class="l">111222333444555666777888999</p>
<p class="r">111222333444555666777888999</p>
direction
Set the direction CSS property to match the direction of the text: rtl
for languages written from right-to-left (like Hebrew or Arabic) text
and ltr for other scripts. This is typically done as part of the
document (e.g., using the dir attribute in HTML) rather than through
direct use of CSS.
References
MDN direction
To get this effect you have to use a little hack. See the following example:
p {
border:1px solid #000;
width:150px;
}
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.reverse-ellipsis {
text-overflow: clip;
position: relative;
background-color: white;
}
.reverse-ellipsis:before {
content: '\02026';
position: absolute;
z-index: 1;
left: -1em;
background-color: inherit;
padding-left: 1em;
margin-left: 0.5em;
}
.reverse-ellipsis span {
min-width: 100%;
position: relative;
display: inline-block;
float: right;
overflow: visible;
background-color: inherit;
text-indent: 0.5em;
}
.reverse-ellipsis span:before {
content: '';
position: absolute;
display: inline-block;
width: 1em;
height: 1em;
background-color: inherit;
z-index: 200;
left: -.5em;
}
<p class="ellipsis reverse-ellipsis">
<span>111222333444555666777888999</span>
</p>
<p class="ellipsis">111222333444555666777888999</p>
More information about this you can find here: http://hugogiraudel.com/2014/12/16/css-riddle-reverse-ellipsis/

CSS getting text in one line rather than two

I have a small issue with a title where I would like text to display on a single line rather than split onto two as im trying to arrange these blocks as a grid
jsFiddle
html
<div class="garage-row">
<a class="garage-row-title" href="/board/garage_vehicle.php?mode=view_vehicle&VID=4">
<div class="garage-title">1996 Land Rover Defender</div>
<div class="garage-image"><img src="http://enthst.com/board/garage/upload/garage_vehicle-4-1373916262.jpg"></div>
</a>
<div class="user-meta">
<b>
Hobbs92
</b>
</div>
</div>
css
#import url(http://fonts.googleapis.com/css?family=Open+Sans);
.garage-row {
border: 1px solid #FFFFFF;
float: left;
margin-right: 5px;
padding: 12px;
position: relative;
width: 204px;
}
.garage-row img{}
.garage-image {
background-position: center center;
display: block;
float: left;
max-height: 150px;
max-width: 204px;
overflow: hidden;
position: relative;
}
.user-meta {
background: none repeat scroll 0 0 #2C3539;
color: #FFFFFF;
float: left;
padding: 10px;
position: relative;
width: 184px;
}
img {
border-width: 0;
height: auto;
max-width: 100%;
vertical-align: middle;
}
.garage-title {
clear: both;
display: inline-block;
overflow: hidden;
}
.garage-row-title {
font-size: 22px;
font-weight: bold;
}
a:link {
color: #43A6DF;
}
font-family: 'Open Sans',sans-serif;
I would greatly appreciate if someone were able to help me get the title into one line rather than two or even fix it so if the title exceeds the width then it gets ellipses.
Add white-space: nowrap;:
.garage-title {
clear: both;
display: inline-block;
overflow: hidden;
white-space: nowrap;
}
jsFiddle
The best way to use is white-space: nowrap; This will align the text to one line.