单行截断

  1. div {
  2. white-space: nowrap;
  3. overflow: hidden;
  4. text-overflow: ellipsis;
  5. }

多行省略

  1. div {
  2. display: -webkit-box;
  3. overflow: hidden;
  4. -webkit-line-clamp: 2;
  5. -webkit-box-orient: vertical;
  6. }

:after+absolute+gradient操作

来源链接:https://juejin.im/post/5c8864e9e51d4562de4677d1

  1. p {
  2. position: relative;
  3. line-height: 18px;
  4. height: 36px;
  5. overflow: hidden;
  6. }
  7. p::after {
  8. content:"...";
  9. font-weight:bold;
  10. position:absolute;
  11. bottom:0;
  12. right:0;
  13. padding:0 20px 1px 45px;
  14. /* 为了展示效果更好 */
  15. background: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
  16. background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  17. background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  18. background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  19. background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  20. }

:after+float+gradient

来源链接:https://juejin.im/post/5c8864e9e51d4562de4677d1

  1. <div class="wrap">
  2. <div class="text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos labore sit vel itaque delectus atque quos magnam assumenda quod architecto perspiciatis animi.</div>
  3. </div>
  1. .wrap {
  2. height: 40px;
  3. line-height: 20px;
  4. overflow: hidden;
  5. }
  6. .wrap .text {
  7. float: right;
  8. margin-left: -5px;
  9. width: 100%;
  10. word-break: break-all;
  11. }
  12. .wrap::before {
  13. float: left;
  14. width: 5px;
  15. content: '';
  16. height: 40px;
  17. }
  18. .wrap::after {
  19. float: right;
  20. text-align: right;
  21. content: "...";
  22. height: 20px;
  23. line-height: 20px;
  24. /* 为三个省略号的宽度 */
  25. width: 3em;
  26. /* 使盒子不占位置 */
  27. margin-left: -3em;
  28. /* 移动省略号位置 */
  29. position: relative;
  30. left: 100%;
  31. top: -20px;
  32. padding-right: 5px;
  33. /* 显示更好的效果 */
  34. background: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
  35. background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  36. background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  37. background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  38. background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
  39. }