w3resource

HTML-CSS: Truncate multiline text

HTML-CSS : Exercise-43 with Solution

Using HTML, CSS truncates text that is longer than one line.

  • Use overflow: hidden to prevent the text from overflowing its dimensions.
  • Set a fixed width to ensure the element has at least one constant dimension.
  • Set height: 109.2px as calculated from the font-size, using the formula font-size * line-height * numberOfLines (in this case 26 * 1.4 * 3 = 109.2).
  • Set height: 36.4px as calculated for the gradient container, based on the formula font-size * line-height (in this case 26 * 1.4 = 36.4).
  • Use background with linear-gradient() to create a gradient from transparent to the background-color.

HTML Code:

<!--License: https://bit.ly/3GjrtVF-->
<!DOCTYPE html>
<html>
<head>
<!-- Set character encoding to UTF-8 -->
<meta charset="utf-8">
<!-- Ensure proper rendering on various devices by setting viewport width -->
<meta name="viewport" content="width=device-width">
<!-- Title of the webpage -->
<title>Using HTML, CSS truncates text that is longer than one line</title>
</head>
<body>
<!-- Paragraph with a class for truncating multiline text -->
<p class="truncate-text-multiline">
<!-- Sample text longer than one line -->
    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
  The quick brown fox jumps over the lazy dog.
</p>
</body>
</html>

Explanation:

  • The HTML document begins with standard declaration tags including <!DOCTYPE html> which declares the document type and <html> which signifies the start of the HTML document.
  • Inside the <head> section:
    • <meta charset="utf-8"> sets the character encoding to UTF-8, ensuring proper interpretation of characters.
    • <meta name="viewport" content="width=device-width"> sets the viewport width to the width of the device, ensuring proper scaling on different devices.
    • <title>...</title> sets the title of the webpage which appears in the browser's title bar or tab.
  • The <body> section contains the content of the webpage.
  • <p class="truncate-text-multiline"> defines a paragraph element with a class attribute set to "truncate-text-multiline".

CSS Code:

/* CSS styles */
<style>
/* Style for truncating multiline text */
.truncate-text-multiline {
  /* Set the positioning of the element relative to its normal position */
  position: relative;
  /* Hide any content that overflows its container */
  overflow: hidden;
  /* Display the element as a block-level element */
  display: block;
  /* Set the height of the element */
  height: 109.2px;
  /* Center the element horizontally */
  margin: 0 auto;
  /* Set the font size of the text */
  font-size: 26px;
  /* Set the line height of the text */
  line-height: 1.4;
  /* Set the width of the element */
  width: 400px;
  /* Background color of the element */
  background: #f5f6f9;
  /* Text color */
  color: #333;
}

/* Pseudo-element to create the gradient fade effect */
.truncate-text-multiline:after {
  /* Insert content after the element */
  content: '';
  /* Set the positioning of the pseudo-element */
  position: absolute;
  /* Position the pseudo-element at the bottom-right corner of the element */
  bottom: 0;
  right: 0;
  /* Set the width of the pseudo-element */
  width: 150px;
  /* Set the height of the pseudo-element */
  height: 36.4px;
  /* Create a gradient background from transparent to the background color of the element */
  background: linear-gradient(to right, rgba(0, 0, 0, 0), #f5f6f9 50%);
}
</style>

Explanation:

  • The CSS includes styles for elements with the class .truncate-text-multiline.
  • .truncate-text-multiline class:
    • position: relative;: Sets the positioning of the element relative to its normal position.
    • overflow: hidden;: Hides any content that overflows its container.
    • display: block;: Sets the element to be displayed as a block-level element.
    • height: 109.2px;: Sets the height of the element.
    • margin: 0 auto;: Centers the element horizontally by setting auto margins.
    • font-size: 26px;: Sets the font size of the text.
    • line-height: 1.4;: Sets the line height of the text.
    • width: 400px;: Sets the width of the element.
    • background: #f5f6f9;: Sets the background color of the element.
    • color: #333;: Sets the text color.
  • .truncate-text-multiline:after:
    • content: '';: Inserts content after the element.
    • position: absolute;: Sets the positioning of the pseudo-element.
    • bottom: 0; right: 0;: Positions the pseudo-element at the bottom-right corner of the element.
    • width: 150px; height: 36.4px;: Sets the width and height of the pseudo-element.
    • background: linear-gradient(to right, rgba(0, 0, 0, 0), #f5f6f9 50%);: Creates a gradient background from transparent to the background color of the element, providing a fade effect at the end of the truncated text.

HTML-CSS Editor:

See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.