Text overflow hides text when it shouldn't in Safari - css

I want to have various tags in a container and have them display ellipsis when the tag text is too big (i.e. when it would stretch beyond the width of the container). The problem I am facing is that in Safari, the ellipsis are displayed even though the tag has space to display the full content.
This is the code that shows what I'm trying to achieve:
.tag {
height: 30px;
background: #F67;
line-height: 30px;
display: block;
float: left;
max-width: calc(100% - 20px);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding: 0 5px;
margin: 5px 5px 0;
border-radius: 16px;
}
.content {
float: left;
max-width: calc(100% - 20px);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.icon {
float: right;
background: blue;
width: 20px;
text-align: center;
color: white;
text-decoration: none;
}
.container {
border: 2px solid blue;
width: 300px;
height: 400px;
padding: 10px;
}
<div class="container">
<div class="tag">
<span class="content">Some tag</span>
X
</div>
<div class="tag">
<span class="content">Some tag</span>
X
</div>
<span class="tag">
blahbsalkfnewijfnewifbwiefnbijfneifjnweifniwjenfewi
</span>
<div class="tag">
<span class="content">Some tags</span>
X
</div>
</div>
If you're running the snippet above in Safari(v8.0.8 is the one I am using) you can see the last tag shows ellipsis even though it still has space to stretch and display the full text. If you can't see what I am talking about here is a screenshot of the issue:
text-overflow problem on safari image
Small mention about the 'X' is that it is intended as an icon someone could click on and delete the tag, but that functionality is not the subject of this question.

I'm using this trick: adding a non-break space right after the text. You can add it directly into your html, like <div class="ellipsis">Test </div> or you can use the :after pseudo element. Here's the .ellipsis class that I'm using:
.ellipsis {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.ellipsis:after {
content: "\0000a0";
display: inline-block;
width: 0;
}
The use of :after has another advantage, it's hidden by setting width: 0;, so you won't notice a larger gap between this element and the thing next to it (another element or a border).

When you remove the following lines from your .content element it works fine by me.
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
example: http://codepen.io/WartClaes/pen/ZQxaKW?editors=1100
Edit: although when looking further I see that you don't always use the same HTML structure? Which results in the double text overflow declaration. Isn't it possible to always use the same structure?

You need to add a "width" for the text, down to the text-overflow:
width: 100%; /Or the size you need/
It works for me in the safari 13.

Related

Adjust width to parent's width and truncate when higher

I have a panel with information and above it there's additional information displayed, but I'd like it to be subtle and a one-liner, so when the text that's contained in it is wider than the parent's width, it should be truncated.
See jsfiddle; this is a mock of what I am after.
This is the HTML for that extra information:
<div class="main-container">
<div class="extra-information">
<span>Information</span> –
<span>When this is too long it should be truncated</span>
</div>
<div class="main">Main panel</div>
</div>
And this is the CSS I am trying so far:
.main-container {
width: 350px;
// ...
}
.main {
// ...
}
.extra-information {
display: block;
box-sizing: border-box;
width: inherit;
max-width: inherit;
white-space: nowrap;
text-overflow: ellipsis;
}
Check it out in another jsfiddle.
You may use text-overflow: ellipsis with overflow: hidden in CSS
text-overflow
The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user.
Other possible values for this property are:
clip : Default value. Clips the text
ellipsis : Render an ellipsis ("...") to represent clipped text
string : Render the given string to represent clipped text
overflow
The overflow property specifies what happens if content overflows an element's box.
This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area.
Note
The overflow property only works for block-level elements.
.main-container {
width: 350px;
}
.extra-information {
white-space: nowrap;
overflow:hidden !important;
text-overflow: ellipsis;
}
<div class="main-container">
<div class="extra-information">
<span>Information</span> –
<span>When this is too long it should be truncated</span>
</div>
<div class="main">Main panel</div>
</div>
Something like this?:
.main-container {
font-family: Calibri, serf;
width: 350px;
padding: 10px;
border: 1px solid #ccc;
}
.main {
border-top: 2px solid #ccc;
margin-top: 10px;
padding-top: 10px;
}
.extra-information {
display: block;
box-sizing: border-box;
width: inherit;
max-width: inherit;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
<div class="main-container">
<div class="extra-information">
<span>Information</span> –
<span>When this is too long it should be truncated</span>
</div>
<div class="main">Main panel</div>
</div>
Just added overflow: hidden;.

Why does Chrome cutting text with ellipsis while Firefox doesn't?

So I have a simple markup with fixed-width container, one floating right badge and text inside container. I would like text to be cutted with ellipsis when it reaches badge, but it won't work for Firefox.
<div class="container">
<span class="to-right">toRight</span>
<span class="left">Cutting text very very long</span>
</div>
Css file:
.container {
border: solid 1px silver;
width: 200px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.to-right {
float: right;
background-color: green;
}
Why and what should I do to make it work consistent?
http://codepen.io/huston007/pen/gPLOaq

Cross-browser CSS text-overflow with floating elements and fluid text width

The scenario I have here: http://jsfiddle.net/b2xLgkqu/4/ (approximate minimal reproduction of my actual usage scenario)
Basically, I have a base element with width: 100% which has three children - leftside segment, rightside segment and main text. The left and right side have a known width in rems, but it's completely fluid otherwise. The text-overflow works nicely in Chrome, IE11 and Chrome for Android, but not on the latest Firefox. I'm looking for something that would make it work there too, without breaking any of the other browsers. (I also can't use workarounds like hiding the main text with a background-color on the side elements due to the containing element having semitransparent background color, and I'd like to keep it that way.)
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
.head {
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
position: relative;
}
.left, .right {
width: 5rem;
padding: 0 0.1rem;
position: relative;
z-index: 1;
}
.left {
float: left;
}
.right {
float: right;
}
span {
position: relative;
z-index: 0;
}
<div class="head">
<div class="left">FOO BAR</div>
<div class="right">FOO BAR</div>
<span>
long text
<span style="color: red">that</span>
should end up
<b>wrapping with</b>
text overflow ellipsis blah blah more blogging here
</span>
</div>
You set some styles to .head but you should set them to .head > span instead:
.head > span {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.left, .right {
width: 5rem;
padding: 0 0.1rem;
}
.left {
float: left;
}
.right {
float: right;
}
.head > span {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="head">
<div class="left">FOO BAR</div>
<div class="right">FOO BAR</div>
<span>
long text
<span style="color: red">that</span>
should end up
<b>wrapping with</b>
text overflow ellipsis blah blah more blogging here
</span>
</div>
Also note it needs display: block in order to interact like you want with the floating elements.

CSS inline-block causing extra space between 2 vertical divs [duplicate]

This question already has answers here:
Why does my image have space underneath?
(3 answers)
Closed 7 years ago.
I have several div (class="badge") to display in the vertical. Not sure why I got extra space between 2 div in FF and IE (Chrome works fine).
I need them to display either no space or equal space in all browsers.
http://jsfiddle.net/2hxak/1/
HTML:
<div class="stat-badges">
<div class="badge">
<div class="stat-num">123456</div>
</div>
<div class="badge">
<div class="stat-num">0</div>
</div>
</div>
CSS:
.stat-badges {
text-align: center;
width: 55px;
}
.badge {
display: inline-block;
padding: 2px 4px;
color: #ffffff;
vertical-align: baseline;
white-space: nowrap;
background-color: #999999;
}
.badge .stat-num {
max-width: 30px;
min-width: 20px;
padding: 3px 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
The space will disappear if I remove overflow: hidden;. I keep overflow: hidden with ellipse to crop long text.
Change vertical-align: baseline; to vertical-align: top; in your badge class rule.
jsFiddle example
display: inline-block; is messing this up. Use float: left; instead (possibly with clear: left; to make sure every badge is on a new line). (jsFiddle)

Showing the part of a hidden DIV within a DIV

I'm missing something obvious today, guys - would appreciate some help please.
I've got a horizontal row of DIVs inside another DIV. I want the third DIV to show as partly hidden by the top DIV. But it isn't showing at all.
Here's the CSS:
.outer {
background: #800;
height: 90px;
width: 300px;
overflow: hidden;
white-space: nowrap;
}
.label {
float: left;
display: block;
background: #888;
width: 75px;
height: 50px;
margin: 10px;
padding: 10px;
line-height: 50px;
font-size: 45px;
text-align: center;
}
Here's the HTML:
<div class="outer">
<div class="label">1</div>
<div class="label">2</div>
<div class="label">3</div>
<div class="label">4</div>
</div>
Thanks for your help!
I'm missing something obvious today, guys - would appreciate some help
please.
The "obvious" thing you're missing is that the third and fourth inner divs are dropping underneath because there is not enough horizontal space. For instance, if I check it using Chrome's Developer Tools:
The simplest way to fix this is to switch from float: left to display: inline-block, with white-space: nowrap (you already have it!) on the containing element:
http://jsfiddle.net/rGfNY/
you need to wrap them in a new div, give the div a width, (bigger than your outer div) it will be cut off by the outer div's overflow hidden.
on thing to note: the width of that inner div is not adjusting with the content, either you specifically set it very high, or you have to calculate it to the content either just put it hardcoded in css, or use javascript.
html:
<div class="outer">
<div class="inner">
<div class="label">1</div>
<div class="label">2</div>
<div class="label">3</div>
<div class="label">4</div>
</div>
</div>
css:
.outer {
background: #800;
height: 90px;
width: 300px;
overflow: hidden;
white-space: nowrap;
}
.inner {
width: 460px;
}
.label {
float: left;
display: block;
background: #888;
width: 75px;
height: 50px;
margin: 10px;
padding: 10px;
line-height: 50px;
font-size: 45px;
text-align: center;
}
working example:
http://jsfiddle.net/f2wpm/

Resources