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

jQuery implements a simple carousel effect

Hello everyone, today I will share with you the i...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Preface: This article is based on the experience ...

React Hooks Common Use Scenarios (Summary)

Table of contents 1. State Hook 1. Basic usage 2....

A brief discussion on CSS3 animation jamming solutions

Why is it stuck? There is a premise that must be ...

Simple Implementation of HTML to Create Personal Resume

Resume Code: XML/HTML CodeCopy content to clipboa...

CentOS 6 Compile and install ZLMediaKit analysis

Install ZLMediaKit on centos6 The author of ZLMed...

How to run a project with docker

1. Enter the directory where your project war is ...

Detailed explanation of MySQL covering index

concept If the index contains all the data that m...

Pure HTML+CSS to achieve Element loading effect

This is the effect of the Element UI loading comp...

How to query the latest transaction ID in MySQL

Written in front: Sometimes you may need to view ...

How to deploy MySQL master and slave in Docker

Download image Selecting a MySQL Image docker sea...

Detailed explanation of data types and schema optimization in MySQL

I'm currently learning about MySQL optimizati...