'How can I get my text to be two lines with padding and ellipsis?
I'm having issues getting my text to be on two lines with ellipsis. I am able to get my ellipsis working but modifying line-height
and height
do nothing to change how many lines of text are shown. Any help on why I'm unable to change the number of lines would be great.
Solution 1:[1]
Try this out
CSS
.slick-slide {
border-radius: 7px;
height: 200px;
margin: 0 10px;
overflow: hidden;
position: relative;
width: 200px;
img {
bottom: 0;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
}
.info {
background: rgba(black, 0.5);
border: 1.5px solid white;
border-radius: 7px;
bottom: 10px;
color: white;
left: 0;
margin: 0 10px;
padding: 7px 10px;
position: absolute;
text-align: center;
right: 0;
z-index: 1;
h6 {
display: -webkit-box;
margin: 0 auto;
overflow: hidden;
text-align: center;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
}
}
All you need to do is adjust the -webkit-line-clamp
value to however many lines you want visible.
Solution 2:[2]
I updated you code using the -webkit-line-clamp: 2
property.Check the browser support here and here is the codepen
.slick-slide {
margin: 0 10px;
height: 200px;
width: 200px;
overflow: hidden;
position: relative;
border-radius: 7px;
}
.slick-slide img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
.slick-slide .info {
z-index: 1;
position: absolute;
left: 0;
right: 0;
bottom: 10px;
color: white;
text-align: center;
}
.slick-slide .info h6 {
border: 1.5px solid white;
display: inline-block;
background: rgba(0, 0, 0, 0.5);
border-radius: 7px;
text-align: center;
margin: 0 auto;
max-width: 80%;
padding: 7px 10px;
line-height: 20px;
height: 40px;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: block-axis;
overflow: hidden;
text-overflow: ellipsis;
}
<div class="slick-slide">
<div class="info">
<h6>elements that are on fire my dude elements that are on fire my dude elements that are on fire my dude elements that are on fire my dude</h6>
</div>
<img src="http://placekitten.com/g/200/300" class="img-fluid">
</div>
Solution 3:[3]
My solution is to trim all lines with clip-path, that are still visible after '...'.
clip-path: polygon(0 0, 0 calc(100% - 20px), 100% calc(100% - 20px), 100% 0);
Solution 4:[4]
There is a perfect way.
Use transparent border with same size replace of padding.
// before
display: -webkit-box;
overflow: hidden;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
padding: 24px;
// after
display: -webkit-box;
overflow: hidden;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
border: 24px solid transparent;
Solution 5:[5]
.slick-slide .info {
z-index: 1;
position: absolute;
left: 0;
right: 0;
bottom: 15px;
color: white;
text-align: center;
height: 40px;
border: 1.5px solid white;
background: rgba(0, 0, 0, 0.5);
overflow: hidden;
border-radius: 7px;
}
.slick-slide .info h6 {
margin: 0;
padding: 7px 10px;
height: 19px;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
display: block;
display: -webkit-box;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dysfunc |
Solution 2 | |
Solution 3 | Oleg Apostol |
Solution 4 | percy507 |
Solution 5 |