Hide overflow of inline element [closed] - html

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can the following be achieved (purely in CSS)?
In a block of fixed width:
display text, and an other character/image (✤) after it.
However, when text is too long, hide its overflow,
while still displaying the character/image (✤) after it.
Visually:
It should not:
wrap the text
grow the block
hide the character/image (✤)
always display the character at right
Markup (but feel free to suggest using other markup if that helps):
<div class=outer>
<span class=copy>Text abc def ghi jkl mno pqr stu</span>
<span class=symbol>✤</span>
</div>

Here is a solution using positioning - you can notice that when the text is short, the symbol will stay to the right end:
div {
border: 1px solid red;
padding 10px;
padding-right: 15px;
white-space: nowrap;
overflow:hidden;
width: 100px;
text-overflow: ellipsis;
position: relative;
}
div:after {
content: '✤';
position: absolute;
right:0;
}
<div>well, some text here</div>
<div>text here</div>
Here is a solution using flexbox where I got it working fully:
div {
border: 1px solid red;
padding 10px;
width: 100px;
display: flex;
}
div span {
white-space: nowrap;
overflow:hidden;
text-overflow: ellipsis;
}
div:after {
content: '✤';
padding-left: 5px;
}
<div><span>well, some text here</span></div>
<br/>
<div><span>text here</span></div>

With the idea that your block has a fixed width, I'd just display the extra character/image with a position: absolute; as per:
Extra char as Pseudo-Element
div {
position: relative;
display: inline-block;
padding: 5px;
padding-right: 25px;
border: 2px solid black;
width: 150px;
}
span {
position:relative;
display: inline-block;
padding-right: 20px;
max-width: 100%; /* or 120px or whatever you want */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
span:after {
content: '★';
position: absolute;
top: -2px;
right: 0px;
}
<div>
<span>text text text</span>
</div>
Declared the extra icon as a pseudo-element.
https://jsfiddle.net/6stc9hf0/1/
Different approach still considering declared static widths
div {
position: relative;
display: inline-block;
padding: 5px;
padding-right: 25px;
border: 2px solid black;
width: 150px;
}
.text {
position:relative;
display: inline-block;
max-width: calc(100% - 20px);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.icon {
position: relative;
top: -2px;
right: 0px;
}
<div>
<span class="text">text text text text</span>
<span class="icon">★</span>
</div>
https://jsfiddle.net/6stc9hf0/2/

Related

Centering truncated text with offset - CSS only

I'm trying to center a title according to the screen's center, while it's container doesn't take 100% of the screen's width.
I'm also need to text to be truncated and don't want to leave a padding on the right.
This is what I've got so far - JSFiddle. You can see that the text in the yellow div is not aligned with the text bellow. If I add a padding-right to the yellow div, upon resize, the text won't take 100% of the yellow div. Any suggestions?
HTML
<div class="cont">
<div class="left-h">
place holder
</div>
<div class="middle-h">
my very long long title goes here
</div>
</div>
<div class="real-center">
my very long long title goes here
</div>
CSS
.cont{
width: 100%;
border: 1px solid black;
display: flex;
text-align: center;
}
.left-h{
flex-basis: 150px;
background-color: #e5e5e5;
}
.middle-h{
background-color: yellow;
flex-grow: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.real-center{
width: 100%;
text-align:center;
}
I finally figured this one out, and once again a pseudo helped me achive impossible things
By adding a width and a min-width it will keep the text centered according to your requirements
.middle-h::after{
content: '';
display: inline-block;
width: calc(100% - 150px);
max-width: 148px; /* 150px - 2px border */
}
Fiddle sample
Stack snippet
.cont{
width: 100%;
border: 1px solid black;
display: flex;
text-align: center;
}
.left-h{
flex-basis: 150px; /* width/height - initial value: auto */
background-color: #e5e5e5;
}
.middle-h{
background-color: yellow;
flex: 1 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.middle-h::after{
content: '';
display: inline-block;
width: calc(100% - 150px);
max-width: 148px; /* 150px - 2px border */
}
.real-center{
width: 100%;
text-align:center;
}
<div class="cont">
<div class="left-h">
place holder
</div>
<div class="middle-h">
my very long long title goes here
</div>
</div>
<div class="real-center">
my very long long title goes here
</div>
Updated
Found yet another way when answering another question which had both a left and a right item
The upside with this is one, it doesn't need predefined width.
Fiddle sample
Stack snippet
.cont {
display: flex;
text-align: center;
border: 1px solid black;
}
.cont > * {
white-space: nowrap;
padding: 2px 4px;
background: lightgray;
}
.cont > .center {
background: yellow;
overflow: hidden;
text-overflow: ellipsis;
}
.cont .left,
.cont::after {
content: '';
flex: 1;
}
.real-center{
width: 100%;
padding: 2px 4px;
text-align:center;
}
<div class="cont">
<div class="left">
place holder
</div>
<div class="center">
my very long long title goes here
</div>
</div>
<div class="real-center">
my very long long title goes here
</div>
This is tricky because you want to center the contents of middle-h within the viewport and as explained here the best way inside a flexbox container is to use absolute position so that it centers relative to the viewport. But, it's harder to get text-overflow: ellipsis; working with an absolute position element.
This is the closest approach I have found..
<div class="cont">
<div class="left-h">
place holder
</div>
<div class="middle-h">
<span class="abs-center">my very long long title goes here</span>
</div>
</div>
<div class="real-center">
my very long long title goes here
</div>
.abs-center {
position: absolute;
left: 150px;
right: 150px;
z-index: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: inline-block;
margin-left: 8px;
margin-right: 8px;
}
http://www.codeply.com/go/S2sw2jrn7p

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/

Text ellipsis at start of string with CSS?

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>

Adapting text to width of container

I'm trying to adapt a few a elements to the total width of the container they are in.
This is the code that I currently have: http://codepen.io/anon/pen/FsgvI
HTML
<div class="box">
<div class="title">
<div class="something">
<h3>
This is foo
>
and then bar
>
and then test
</h3>
</div>
</div>
<div class="content">
asdijfg asoidf oasidf aosidf
</div>
</div>
CSS
.box {
border: 1px solid red;
display: inline-block;
}
.content {
border: 1px solid blue;
width: 170px;
height: 100px;
}
.title {
height: 30px;
}
h3, h3 > a {
text-overflow: ellipsis;
overflow: hidden;
}
How can I make the text adapt to the width of the box container? And by "adapt" I mean keep the current font size and cut (ellipsis) the text that doesn't fit inside.
Regards
Edit:
Sorry for the missunderstanding. I can't set a width to the box class because I don't know how wide it will be. I need it to be as wide as many items (content box) as there are.
This is possible with a few changes to your CSS.
Make .title position: relative; this will make h3 position relative to it
Make h3 position: absolute; to take it out of the document flow and give it width: 100%
Add white-space: nowrap; to h3 stop the contents wrapping onto the new line
Remove width: 170px; from .content to allow it to take up as much space as it needs
.box {
border: 1px solid red;
display: inline-block;
}
.content {
border: 1px solid blue;
height: 100px;
}
.title {
position: relative;
height: 50px;
width: 100%;
}
h3 {
position: absolute;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
width: 100%;
}
<div class="box">
<div class="title">
<div class="something">
<h3>
This is foo
>
and then bar
>
and then test
</h3>
</div>
</div>
<div class="content">
asdijfg asoidf oasidf aosidf
</div>
</div>
Codepen: http://codepen.io/anon/pen/GgkJr
I had to add a width to the containing element and add the white-space: nowrap; to the .something div.
.box {
border: 1px solid red;
display: inline-block;
width:170px;
}
.content {
border: 1px solid blue;
width: 170px;
height: 100px;
}
.title {
height: 30px;
}
.something{
white-space: nowrap;
overflow:hidden;
text-overflow: ellipsis;
}