Text-overflow elipsis disappearing when I change height - html

I need to get elipsis to work for a paragraph, but when I change the height from height:2.5em; to height:1.25em;, the elipsis go away.
.a {
font-size: 13px;
display: block;
width: 100%;
height: 2.5em;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow-y: hidden;
text-overflow: ellipsis;
white-space: normal;
margin-bottom: 10px;
}
.b {
height:1.25em !important;
}
<p class="a">
This is a cool text document and it has more than one line of informaion that gets displayed in the document manager. Please write about conjoined twins as well as the soccer team FC Barcelona. The more overlap between these two subjects mentioned and researched, the higher likliehood of your pitch getting acepted. Also, it should be in strictly QDAS format with the one and only George W. Bush
</p>
<p class="a b">
This is a cool text document and it has more than one line of informaion that gets displayed in the document manager. Please write about conjoined twins as well as the soccer team FC Barcelona. The more overlap between these two subjects mentioned and researched, the higher likliehood of your pitch getting acepted. Also, it should be in strictly QDAS format with the one and only George W. Bush
</p>

By decreasing the height, you decreased the number of lines that can be shown. So you have to decrease the -webkit-line-clamp value to match the maximum number of lines. In this case, it is 1:
.a {
font-size: 13px;
display: block;
width: 100%;
height: 1.25em;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow-y: hidden;
text-overflow: ellipsis;
white-space: normal;
margin-bottom: 10px;
}
<p class="a">
This is a cool text document and it has more than one line of informaion that gets displayed in the document manager. Please write about conjoined twins as well as the soccer team FC Barcelona. The more overlap between these two subjects mentioned and researched, the higher likliehood of your pitch getting acepted. Also, it should be in strictly QDAS format with the one and only George W. Bush
</p>

The problem is that you have -webkit-line-clamp set to 2. You will need to change that to 1.
Keep in mind that this method is quite fragile and will break if you add padding to your anchor and may be hard to maintain in different viewports.
There are several important things you should consider here.
First you can set a line-height. Then you will want to multiply each line-clamp times the line-height, and set that value to max-height.
For example:
max-height = (line-height)(-webkit-line-clamp)
max-height = (13px)(2)
max-height = 26px
.a {
font-size: 13px;
line-height: 13px;
max-height: 13px;
display: block;
width: 100%;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow-y: hidden;
text-overflow: ellipsis;
white-space: normal;
margin-bottom: 10px;
}
<p class="a">
This is a cool text document and it has more than one line of informaion that gets displayed in the document manager. Please write about conjoined twins as well as the soccer team FC Barcelona. The more overlap between these two subjects mentioned and
researched, the higher likliehood of your pitch getting acepted. Also, it should be in strictly QDAS format with the one and only George W. Bush
</p>

Related

Unable to control overflow of a paragraph after two lines using ellipsis

{ PS : i referred to Unable to get ellipsis for Multiline Paragraph }
Expected Output:
Focus with soft study music in
the background and make sure...
[after two lines, ellipsis is showing and text stops]
(https://i.stack.imgur.com/BeBdt.png)
..................................................................................................................................................................................
Obtained Output:
But after researching and checking many answers and tutorials I obtain the below result
I get this output with text flowing after the ellipsis
Focus with soft study music
in the background and make sure ...
it caters to all your needs.
....................................................................................................................................................................................
Main picture:
This is the css code
.text-tocut{
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
width:170px;
height: 60px;
font-size: 13px;
margin-bottom: 10px;
}
This is html code
<div class="sub-box">
<img class ="sub-image" src="thumbnails\c.jpg">
<img class="playsong-button" src="buttons\playsong-button.jpg">
<p class="sub-desc">
Instrumental Study
</p>
<p class="text-tocut">
Focus with soft study music in the background and make sure it caters to all your needs.
</p>
</div>
How do I get the output that I need?
which is
enter image description here
I thought we had to put the "para" into a div with a fixed width and height
or make the para the child class and apply
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
also tried using text overflow,
But, unfortunately, this didn't work either
I found similar question but none of them matched mine and I haven't found an answer so far
not clear exactly what you are asking but it looks like you just need to make sure your text actually overflows
.text-tocut {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
width: 173px;
height: 30px;
font-size: 13px;
margin-bottom: 10px;
border:solid 1px red;
}
<div class="sub-box">
<img class="sub-image" src="thumbnails\c.jpg">
<img class="playsong-button" src="buttons\playsong-button.jpg">
<p class="sub-desc">
Instrumental Study
</p>
<p class="text-tocut">
Focus with soft study music in the background and make sure it caters to all your needs.
</p>
</div>

Keep hanging characters visible in spite of `line-height: 1` and `overflow: hidden`

Target
<div class="Card">
<div class="Card-FullNameLabel">Gregg Sims</div>
<div class="Card-OrganizationNameLabel">Compubotics</div>
</div>
The .Card-FullNameLabel has font-size: 16px and line-height: 1.
The .Card-OrganizationNameLabel has font-size: 12px and line-height: 1.
The vertical space between .Card-FullNameLabel and .Card-OrganizationNameLabel must be exactly 6px.
Below CSS rule must work and must NOT be changed.
.Card-FullNameLabel + .Card-OrganizationNameLabel {
margin-top: 6px;
}
Both .Card-FullNameLabel and .Card-OrganizationNameLabel must have overflow tolerance (e. g. if this content will be
something like ÀÇĤjgpfhjklbĜiEstosTreMalfaci and so on it must not overhang from the parrent).
All letters must be fully visible despite to line-height: 1.
The mental arithmetic (magic numbers and/or hard coded offsets and other values which must be pre-computed) in CSS code are not allowed.
What is O'K to do: use the functionality of Pug pre-processor for markup and CSS pre-processors for styles.
🌎 Inital fiddle does not satisfied to the condition number 5: currently the card is not overflow-tolerant.
About line-height: 1, the bad practice
I has been repeatedly told about I must set line-height to value more than 1.
It becomes obvious that setting line-height: 1 is a bad practice. I
remind you that unitless values are font-size relative, not
content-area relative, and dealing with a virtual-area smaller than
the content-area is the origin of many of our problems.
Deep dive CSS: font metrics, line-height and vertical-align
Well, I don't going to dispute about it. All I want is the working solution for the reaching of my target (descripted above).
The usage of it is my responsibility and I will not reсcommend this solution if you agree that line-height must be more than 1.
But why I don't want increase the line-height so persistently?
Reason 1: The precise defining of the vertical space between two elements will become too complicated
The rule .Card-FullNameLabel + .Card-OrganizationNameLabel { margin-top: 6px; } is clear, intuitive and expresses the guidelines (represented in the picture above) by CSS. "The .Card-OrganizationNameLabel must retire from .Card-FullNameLabel by 6 pixels", and nothing more.
But what if we need to define the same vertical space between .Card-FullNameLabel and .Card-OrganizationNameLabel when line height is more than 1 (or they have the top and bottom paddings)? The value of the margin-top (visualized by non-overlayed pink area in the picture below) of .Card-FullNameLabel + .Card-OrganizationNameLabel rule now be the difference of:
The desired range (6px)
The extra vertical space below .Card-FullNameLabel (designated as l_b)
The extra vertical space above .Card-OrganizationNameLabel (designated as l_a)
As I told above, the mental arithmetic is not allowed because it devalues the programming (CSS preprocessors capabilities in CSS case) and makes flexibility/maintainability impact (if we change the line-height or font-size or desired vertical space between labels, everything need to be mentally re-computed).
Although the preprocessor's variables (today became available in native CSS) can solve this problem, it will be too complicated to maintain it. To compute the non-intersecting red pink in the image above, we need to:
Variablelize the font-size of .Card-FullNameLabel
Variablelize the line-height of .Card-FullNameLabel
Compute the extra space below .Card-FullNameLabel.
Variablelize the font-size of .Card-OrganizationNameLabel
Variablelize the line-height of .Card-OrganizationNameLabel
Compute the extra space below .Card-OrganizationNameLabel
Variablelize the desired range between .Card-FullNameLabel and .Card-OrganizationNameLabel (6 pixels in this example).
After this, we can finally compute the margin-top for the rule .Card-FullNameLabel + .Card-OrganizationNameLabel. And same for each pair of elements like .Card-FullNameLabel and .Card-OrganizationNameLabel!! Too poor technology for the web development in 2020s.
Reason 2: It does not require for each language
In below example, the Japanese symbols are perfectly fits to line with line-height: 1 (16px):
I suppose same will be for the Chinese, Korean and many other languages with non-latin characters.
But: in the small percentage of cases, there the foreign symbols could be mixed:
If to talk about high quality, this case must be supported.
I don't want increase the line height just for this exception. It's OK that the vertical space between lines actually became not 6px: the tails of j or À has a small weight and it will not break the geometric aesthetics.
My efforts
Attempt 1: usage of :before and :after
The SASS-mixin TextTruncation accepts the parameter $extraSpace which adding top and bottom paddings. The :before and :after pseudo elements compensates this paddings by negative margins.
#mixin TextTruncation($extraSpace, $displayEllipsis: false) {
overflow: hidden;
white-space: nowrap;
#if ($displayEllipsis) {
text-overflow: ellipsis;
} #else {
text-overflow: clip;
}
padding-top: $extraSpace;
padding-bottom: $extraSpace;
&:before,
&:after {
content: "";
display: block;
}
&:before {
margin-top: -$extraSpace;
}
&:after {
margin-bottom: -$extraSpace;
}
}
body {
padding: 12px;
}
* {
line-height: 1;
font-family: Arial, sans-serif;
}
.Card {
display: flex;
flex-direction: column;
align-items: center;
width: 240px;
height: 320px;
padding: 6px 12px 12px;
background: white;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.5);
}
.Card-FullNameLabel {
max-width: 100%; /* Required when the flex parent has `align-items: center` */
#include TextTruncation($extraSpace: 2px, $displayEllipsis: true);
font-size: 16px;
color: #707070;
}
.Card-OrganizationNameLabel {
max-width: 100%; /* Required when the flex parent has `align-items: center` */
#include TextTruncation($extraSpace: 2px, $displayEllipsis: true);
font-size: 12px;
color: #A2A2A2;
}
.Card-FullNameLabel + .Card-OrganizationNameLabel {
margin-top: 6px;
}
Unfortunately, It does not work: the effect is same as if no margins and no paddings has been defined:
🌎 CodePen
Attempt 2: usage of the wrapper
If the combination of overflow-x: hidden and overflow-y: visible works, it was the solution. But it does no work and this problem has been considered in the question CSS overflow-x: visible; and overflow-y: hidden; causing scrollbar issue.
I want to avid the wrappers as possible, but here it looks like the wrapper will be the last resort. To avoid of writing two tags each time, I created the Pug mixin:
mixin SingleLineLabel
span.SingleLineLabel&attributes(attributes)
span.SingleLineLabel-Text
block
Well, the SingleLineLabel now a component. Besides the Pug mixin it's required to define the basic styles and SASS mixin allows to customize the label individually:
// Constant styles
.SingleLineLabel {
overflow-y: visible;
&:before,
&:after {
content: "";
display: block;
}
&-Text {
display: block;
overflow-x: hidden;
white-space: nowrap;
}
}
// Variable styles
#mixin SingleLineLabel($truncatedVerticalSpaceCompensation, $displayEllipsis: false) {
&:before {
margin-top: -$truncatedVerticalSpaceCompensation
}
&:after {
margin-bottom: -$truncatedVerticalSpaceCompensation
}
.SingleLineLabel-Text {
padding-top: $truncatedVerticalSpaceCompensation;
padding-bottom: $truncatedVerticalSpaceCompensation;
#if ($displayEllipsis) {
text-overflow: ellipsis;
} #else {
text-overflow: clip;
}
}
}
Now we can apply it:
.Card-FullNameLabel {
max-width: 100%; /* Required when the flex parent has `align-items: center` */
#include SingleLineLabel($truncatedVerticalSpaceCompensation: 1px, $displayEllipsis: true);
font-size: 16px;
color: #707070;
}
.Card-OrganizationNameLabel {
max-width: 100%; /* Required when the flex parent has `align-items: center` */
#include SingleLineLabel($truncatedVerticalSpaceCompensation: 2px, $displayEllipsis: true);
font-size: 12px;
color: #A2A2A2;
}
.Card-FullNameLabel + .Card-OrganizationNameLabel {
margin-top: 6px;
}
It seems like the target has been reached:
🌎 CodePen
Unfortunately, it has the bug which occurrence regularity is unclear.
Sometimes the small vertical scrollbar appearing.
I really don't know how to reproduce it, but in the past experiment it has occurred, for example, if to switch the browser to device simulation mode by development tools and then exit from this mode. Most likely, you will not get the same effect if repeat same experiment in fiddle.
Finally
The solution based on your great answers will be included to growing #yamato-daiwa/frontend library.
If you have the full list of the problematic symbols like g, p, À, Ĥ and so on, please share it - I'll use it for the tests and also add them to the future pug functionality for the overflow tolerance testing.
I know you explicitly stated that you needed to keep line-height: 1 and margin-top: 6px, but as you identified with the documented overflow CSS issue, you're kind of stuck with your current restrictions.
If it is at all possible to be flexible about those restrictions, I have a solution that is visually identical to what you were originally after.
Original State
I started with your Initial Fiddle and added ellipsis truncation CSS and problematic text in the html.
.Card-FullNameLabel,
.Card-OrganizationNameLabel {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
The result is this state which I am calling "Original" since it leaves your line-height and margin values unmodified. Note that I have added a single overflow: hidden rule in place of the problematic mixing of overflow-x and overflow-y rules.
Proposed Fix
I propose the following CSS changes. This increases line-height to 1.5 which allows all of the font's ascenders and descenders to be visible. Then I added negative offset margins to compensate:
.Card-FullNameLabel,
.Card-OrganizationNameLabel {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/* Shows all ascenders and descenders */
line-height: 1.5;
}
.Card-FullNameLabel {
font-size: 16px;
color: #707070;
/* Compensates for line-height */
margin: -4px 0;
}
.Card-OrganizationNameLabel {
font-size: 12px;
color: #A2A2A2;
/* Compensates for line-height */
margin: -3px 0;
}
.Card-FullNameLabel + .Card-OrganizationNameLabel {
/* 6px visually (minus 3px) */
margin-top: 3px;
}
The result can be seen in action here which I am referring to as "Proposed Fix". I have confirmed the results are consistent in latest desktop Chrome, Firefox, and Safari on MacOS and Mobile Safari on iOS.
Comparison
I made a simple animation from "before" and "after" screenshots that demonstrate that the output is visually identical except that the proposed fix does not cut off the font's ascenders and descenders.
Note that you can click the animation to see a full-size, 1:1 pixel-accurate version.
I did some additional tests with what I'm calling "in-between elements" to demonstrate that the proposed fix would behave the same as the original even if there were elements in-between.
Update: Automation
As was made clear in the comments, one of the requirements is that there be no "hard-coded" or "magic" numbers in the CSS. So while the above solution works, it requires manual arithmetic ahead of time.
Here is an updated Codepen that can automatically produce similar CSS to what was shown above by using some SCSS logic that will calculate the offsets based on these input values:
Variable
Current Value
$globalLineHeight
1
$minLineHeight
1.5
$fullNameFontSize
16px
$fullNameLineHeight
$globalLineHeight
$orgNameFontSize
12px
$orgNameLineHeight
$globalLineHeight
$orgNameMarginTop
6px
For demonstration purposes, I added a bit of extra code that will show a "before" and "after" hover effect so you can see how the SCSS logic behaves compared to the original CSS. As is indicated in both the HTML and CSS, you can delete anything below the lines that begin with #DELETE-ME.

Reversed characters when providing another string to text-overflow: ellipsis;

I'm trying to create a text-overflow: ellipsis; from the beginning, but in some specific context, it reverses the characters.
This is a CodePen to illustrate the problem: https://codepen.io/DWboutin/pen/yLaoxog
HTML:
<div class="ellipsis">Path to you prefered files that you love so much forever and ever fuck yeah</div>
<div class="ellipsis">1":"#323130",messageLink:t?"#6CB8F6":"#005A9E",messageLinkHovered:t?"#82C7FF":"#004578",infoIcon:t?"#</div>
CSS:
div {
margin: 10px 0;
border: 1px solid black;
}
.ellipsis {
width: 400px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
direction: rtl;
}
I tried all word-break properties, wrapping the string into another span to force it to be ltr but it don't work.
Thank you for your help
unicode-bidi might help you with an extra wrapper to handle text direction:
https://developer.mozilla.org/en-US/docs/Web/CSS/unicode-bidi
The unicode-bidi CSS property, together with the direction property, determines how bidirectional text in a document is handled. For example, if a block of content contains both left-to-right and right-to-left text, the user-agent uses a complex Unicode algorithm to decide how to display the text. The unicode-bidi property overrides this algorithm and allows the developer to control the text embedding.
div {
margin: 10px 0;
border: 1px solid black;
width:max-content;
}
.ellipsis {
width: 400px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
direction: rtl;
}
.ellipsis span {
direction: ltr;
unicode-bidi: bidi-override;
}
<div class="ellipsis">Path to you prefered files that you love so much forever and ever fuck yeah</div>
<div class="ellipsis"><span>1":"#323130",messageLink:t?"#6CB8F6":"#005A9E",messageLinkHovered:t?"#82C7FF":"#004578",infoIcon:t?"#</span></div>
original text :
<div>Path to you prefered files that you love so much forever and ever fuck yeah</div>
<div><span>1":"#323130",messageLink:t?"#6CB8F6":"#005A9E",messageLinkHovered:t?"#82C7FF":"#004578",infoIcon:t?"#</span></div>
Note, that extra wrapper needs to remain an inline element (display:inline) , an inline-box will not be part of the ellipsis rule but will overflow to the left, a block element will overflow on the right.

How to limit the character count in CSS

I am creating a multilingual website where the plugin in I am using only provides the option to show the language name entirely. I want to limit the character count to only 3 characters. Example: If the language is English I only want to display it as ENG, When it's Vietnamese it will be VIE.
I already tried
span {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 50px;
display: inline-block;
text-transform: uppercase;
font-size: 15px;
}
<p>
<span>English</span>
</p>
<p>
<span>Vietnamese</span>
</p>
I don't want the ... appear with the first three letters and setting a width or max-width is not effective to show only 3 characters in some languages as you see in the above snippet. Is there are any alternative approaches there? Let me know, please.
that ? with font-family:monospace;
span {
font-size : 30px;
display : inline-block;
width : 3ch;
font-family : monospace;
overflow : hidden;
text-transform : uppercase;
}
<p>
<span>English</span>
</p>
<p>
<span>Vietnamese</span>
</p>

How to wrap and truncate a long string in to three lines in css

I want to wrap my long article in to three to four lines and a more button below. Currently, i am using the below css code.
.truncate {
width: auto;
text-align: justify;
word-break: keep-all;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
The above css class does what i want . However, it shortens the artcile to a single line code code only. I tried everything possible to make it in to three or four and half lines and din't succeed. I thought of adding a height property and didn't change. please how do i control the number of lines. ? Any help would be appreciated.
Update
Just like on SO here . A question title, and just two lines from the post. Please how do i achieve that ?
You can use -webkit-line-clamp: 2; -webkit-box-orient: vertical;. But it's only for webkit browsers.
http://jsfiddle.net/tv2mfxe5/1/
.truncate {
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
And if this doesn't work for you. I would recommend using a jQuery plugin, dot dot dot