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

Detailed tutorial on using the tomcat8-maven-plugin plugin in Maven

I searched a lot of articles online but didn'...

Detailed tutorial on how to create a user in mysql and grant user permissions

Table of contents User Management Create a new us...

How to make a List in CocosCreator

CocosCreator version: 2.3.4 Cocos does not have a...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...

React internationalization react-intl usage

How to achieve internationalization in React? The...

Using group by in MySQL always results in error 1055 (recommended)

Because using group by in MySQL always results in...

An article to understand what is MySQL Index Pushdown (ICP)

Table of contents 1. Introduction 2. Principle II...

Basic usage of exists, in and any in MySQL

【1】exists Use a loop to query the external table ...

Have you really learned MySQL connection query?

1. Inner Join Query Overview Inner join is a very...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...