Display ellipsis effect when table cell content exceeds (implementation code)

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate

In front-end development, you often encounter situations where you need to limit the cell width and display an ellipsis for the part where the content exceeds the limit. Here is a brief introduction on how to achieve this effect.

Preparatory knowledge

1. Control text without line break

white-space: nowrap;

2. When the length is exceeded, an ellipsis appears

overflow:hidden;

text-overflow:ellipsis

3. Modify the table layout algorithm

table-layout:fixed; The default value of table-layout is automatic, which means that the column width is set by the cell content. Fixed means that the column width is set by the table width and the column width.

That is to say, when you set the column width for the table, it actually does not work. When there is too much content in the cell, the width will still be expanded. If you want the table column width to be determined by the column width you define for the cell, you must use the fixed value.

Note: 1. The table width must be set. 2. If you only set the table width without setting the column width, the column widths will be evenly distributed.

Code Demonstration

As shown in the following code, the table has four columns: name, age, gender, and address. The lengths of these columns are 10%, 20%, 30%, and 40% respectively.

XML/HTML CodeCopy content to clipboard
  1. <!doctype html >   
  2. < html   lang = "en" >   
  3. < head >   
  4.      < meta   charset = "UTF-8"   />   
  5.      < title > Table Demonstration </ title >   
  6.      < style   type = "text/css" >   
  7. table{
  8. width: 100%;
  9. table-layout: fixed;
  10. }
  11. .name{
  12. width: 10%;
  13. }
  14. .age{
  15. width: 20%;
  16. }
  17. .sex{
  18. width: 30%;
  19. }
  20. .addr{
  21. width: 40%;
  22. }
  23.            
  24.      </ style >   
  25. </ head >   
  26. < body >   
  27.      < table   border = "1"   cellspacing = "0"   cellpadding = "0" >   
  28.          < thead >   
  29.              < tr >   
  30.                  < th   class = " name " > Name </th>   
  31.                  < th   class = " age " > age </th>   
  32.                  < th   class = " sex " > Gender </th>   
  33.                  < th   class = " addr " > Address </th>   
  34.              </ tr >   
  35.          </ thead >   
  36.          < tbody >   
  37.              < tr >   
  38.                  < td > Li Si </ td >   
  39.                  < td > 13 </ td >   
  40.                  < td > Male </ td >   
  41.                  < td > Shandong </ td >   
  42.              </ tr >   
  43.              < tr >   
  44.                  < td > Li Si </ td >   
  45.                  < td > 13 </ td >   
  46.                  < td > Male </ td >   
  47.                  < td > Shandong </ td >   
  48.              </ tr >   
  49.              < tr >   
  50.                  < td > Li Si </ td >   
  51.                  < td > 13 </ td >   
  52.                  < td > Male </ td >   
  53.                  < td > Shandong </ td >   
  54.              </ tr >   
  55.          </ tbody >   
  56.      </ table >   
  57. </ body >   
  58. </ html >   

The display effect is as follows:

It is easy to see that the lengths of the name, age, gender, and address columns are 10%, 20%, 30%, and 40% respectively.

If the content of the first name is increased, the effect is simply unbearable to look at (>﹏<)!

I can’t bear to look at it (>﹏<)! !

How to display the part exceeding a single line as ellipsis? You just need to set the cell properties as follows:

XML/HTML CodeCopy content to clipboard
  1. white-space: nowrap;/*Control single line display*/
  2. overflow: hidden;/*Exceeding hidden*/
  3. text-overflow: ellipsis;/*Hidden characters are represented by ellipsis*/

Without further ado, let’s get to the code!
XML/HTML CodeCopy content to clipboard
  1. <!doctype html >   
  2. < html   lang = "en" >   
  3. < head >   
  4.      < meta   charset = "UTF-8"   />   
  5.      < title > Table Demonstration </ title >   
  6.      < style   type = "text/css" >   
  7. table{
  8. width: 100%;
  9. table-layout: fixed;
  10. }
  11. .name{
  12. width: 10%;
  13. }
  14. .age{
  15. width: 20%;
  16. }
  17. .sex{
  18. width: 30%;
  19. }
  20. .addr{
  21. width: 40%;
  22. }
  23. td{
  24. white-space: nowrap;/*Control single line display*/
  25. overflow: hidden;/*Exceeding hidden*/
  26. text-overflow: ellipsis;/*Hidden characters are represented by ellipsis*/
  27. }
  28.      </ style >   
  29. </ head >   
  30. < body >   
  31.      < table   border = "1"   cellspacing = "0"   cellpadding = "0" >   
  32.          < thead >   
  33.              < tr >   
  34.                  < th   class = " name " > Name </th>   
  35.                  < th   class = " age " > age </th>   
  36.                  < th   class = " sex " > Gender </th>   
  37.                  < th   class = " addr " > Address </th>   
  38.              </ tr >   
  39.          </ thead >   
  40.          < tbody >   
  41.              < tr >   
  42.                  < td   class = "name2" > Li Sisssssssssssssssssssssssssssssssssssssssssss </ td >   
  43.                  < td > 13 </ td >   
  44.                  < td > Male </ td >   
  45.                  < td > Shandong </ td >   
  46.              </ tr >   
  47.              < tr >   
  48.                  < td > Li Si </ td >   
  49.                  < td > 13 </ td >   
  50.                  < td > Male </ td >   
  51.                  < td > Shandong </ td >   
  52.              </ tr >   
  53.              < tr >   
  54.                  < td > Li Si </ td >   
  55.                  < td > 13 </ td >   
  56.                  < td > Male </ td >   
  57.                  < td > Shandong </ td >   
  58.              </ tr >   
  59.          </ tbody >   
  60.      </ table >   
  61. </ body >   
  62. </ html >   

After modification, the effect is as follows:

The above article about displaying ellipsis effect when table cell content exceeds the limit (implementation code) is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

<<:  Detailed explanation of three solutions to the website footer sinking effect

>>:  How to solve the problem of clicking tomcat9.exe crashing

Recommend

Analysis of the process of deploying pure HTML files in Tomcat and WebLogic

1. First, the pure HTML file must have an entry i...

Use crontab to run the script of executing jar program regularly in centOS6

1. Write a simple Java program public class tests...

CentOS 7 installation and configuration tutorial under VMware10

If Ubuntu is the most popular Linux operating sys...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

Introduction to the process of installing MySQL 8.0 in Linux environment

Table of contents Preface 1. Linux changes the yu...

The most basic code for web pages

◆Add to favorites illustrate Click to add your we...

Linux beginners in virtual machines configure IP and restart the network

For those who are new to virtual machines or have...

Tutorial on installing MySQL 5.7.9 using RPM package under CentOS 7

Recorded MySQL 5.7.9 installation tutorial, share...

Two methods to implement Mysql remote connection configuration

Two methods to implement Mysql remote connection ...

How familiar are you with pure HTML tags?

The following HTML tags basically include all exis...

How to install MySQL via SSH on a CentOS VPS

Type yum install mysql-server Press Y to continue...

Comparing Node.js and Deno

Table of contents Preface What is Deno? Compariso...