Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS line-clamp does not work in Safari on inner block level elements

Tags:

html

css

safari

CSS

.line-clamp{
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden; 
}

HTML

<div class="line-clamp">
  <div>Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div>Line 4</div>
  <div>Line 5</div>
</div>

Chrome works fine
enter image description here

Safari does not work
enter image description here

like image 607
DIGITALSQUAD Avatar asked Oct 24 '25 04:10

DIGITALSQUAD


2 Answers

The latest version of safari appears to have bugs in regards to overflow: hidden.

line clamp does still work for a single div in safari if you are able to reduce the number of div's used in your code.

<div class="line-clamp">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

Alternatively, this css would get the overflow to work, but does not add the ellipsis

.line-clamp{
  overflow: hidden;
  display: block;
  font-size: 1rem;
  line-height: 1.5rem;
  height: 4.5rem;
}
like image 67
J Davies Avatar answered Oct 25 '25 18:10

J Davies


As J Davies wrote, Safari does not hide overflow contents in inner block elements. So just added height and adjusted font-size and line-height properties ad J Davies wrote.

CSS

.line-clamp{
  display: -webkit-box; // Notice display: block doesn't add ellipsis in Chrome
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden; 
  // added below
  font-size: 1rem;
  line-height: 1.5rem;
  height: 4.5rem;
}

Chrome still works fine

enter image description here

Safari does not add ellipsis always, but sometimes do.

enter image description here

like image 22
DIGITALSQUAD Avatar answered Oct 25 '25 17:10

DIGITALSQUAD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!