简体   繁体   English

当表格单元格太长时,有没有办法减少表格单元格中的文本?

[英]It's any way to decrease text in a table cell when it is too long?

I'm developing a calendar and I need to write events (text) in some days of the week(cell's of the table).我正在开发一个日历,我需要在一周的某些天(表格的单元格)编写事件(文本)。 The problem is when the text in a cell is very large and the cell increases accordingly.问题是当单元格中的文本非常大并且单元格相应增加时。

I need to have fixed size cells and fixed size events (div with text).我需要固定大小的单元格和固定大小的事件(带有文本的 div)。 在此处输入图片说明 Code:代码:

 table { font-size: 0.875rem; font-weight: 400; line-height: 1.5; direction: ltr; text-align: center !important; color: #6c757d !important; box-sizing: border-box; border-collapse: collapse; width: 100%; margin-bottom: 1rem; background-color: transparent; border: 1px solid #dee2e6; table-layout: fixed; } .timetable_event { width: 100%; line-height: normal; }
 <table class="table table-bordered table-responsive-sm" id="calendar"> <thead> <tr> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> <th>Sun</th> </tr> </thead> <tbody id="calendar-body"> <tr> <td></td> <td></td> <td></td> <td>1 <div class="timetable_event"> <small>Subject: [00000111] AUDICION Y LENGUAJE / LOGOPEDIA</small> <br> <small>From: 02:00</small> <br> <small>To: 05:33</small> <br> </div> </td> <!--more code --> </tbody> </table>

Any suggestion?有什么建议吗?

Thanks for reading!谢谢阅读!

You can use line-clamp like so你可以像这样使用线夹

display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;

This allows you to limit the number of lines in the cell by changing -webkit-line-clamp: 3;这允许您通过更改-webkit-line-clamp: 3;来限制单元格中的行数-webkit-line-clamp: 3; and works on all browsers except IE11.适用于除 IE11 之外的所有浏览器

Additionally, if you add the value to the title attribute of the cell, the entire text will show when you hover it.此外,如果您将值添加到单元格的title属性,则将其悬停时将显示整个文本。

 table { font-size: 0.875rem; font-weight: 400; line-height: 1.5; direction: ltr; text-align: center !important; color: #6c757d !important; box-sizing: border-box; border-collapse: collapse; width: 100%; margin-bottom: 1rem; background-color: transparent; border: 1px solid #dee2e6; table-layout: fixed; } .timetable_event { width: 100%; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; }
 <table class="table table-bordered table-responsive-sm" id="calendar"> <thead> <tr> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> <th>Sun</th> </tr> </thead> <tbody id="calendar-body"> <tr> <td></td> <td></td> <td></td> <td>1 <div class="timetable_event" title="Subject: [00000111] AUDICION Y LENGUAJE / LOGOPEDIA From: 02:00 To: 05:33"> <small>Subject: [00000111] AUDICION Y LENGUAJE / LOGOPEDIA</small> <br> <small>From: 02:00</small> <br> <small>To: 05:33</small> <br> </div> </td> <!--more code --> </tbody> </table>

One option is to wrap the text in a div inside the td , and give that a fixed height along with overflow-y: auto;一种选择是包裹在一个文本div里面td ,并给予一个固定的height以及overflow-y: auto; to make overflowing content accessible via scrolling:通过滚动访问溢出的内容:

 table { font-size: 0.875rem; font-weight: 400; line-height: 1.5; direction: ltr; text-align: center !important; color: #6c757d !important; box-sizing: border-box; border-collapse: collapse; width: 100%; margin-bottom: 1rem; background-color: transparent; border: 1px solid #dee2e6; table-layout: fixed; } .timetable_event { width: 100%; line-height: normal; max-height: 2rem; overflow-y: scroll; }
 <table class="table table-bordered table-responsive-sm" id="calendar"> <thead> <tr> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> <th>Sun</th> </tr> </thead> <tbody id="calendar-body"> <tr> <td></td> <td></td> <td></td> <td>1 <div class="timetable_event"> <small>Subject: [00000111] AUDICION Y LENGUAJE / LOGOPEDIA</small> <br> <small>From: 02:00</small> <br> <small>To: 05:33</small> <br> </div> </td> </tr> </tbody> </table>

Please note that you cannot fixate the height of a td - its height always is a result of the content in the highest td in that row.请注意,您无法固定td的高度 - 它的高度始终是该行中最高td内容的结果。

To decrease font-size depending upon text length, you need Javascript:要根据文本长度减小字体大小,您需要使用 Javascript:

 document.addEventListener('DOMContentLoaded', function() { const events = document.querySelectorAll('.timetable_event'); for (event of events) { const factor = 1 / ((event.innerText.length || 1) / 35); if (factor < 1) event.style.fontSize = (factor * 0.875) + 'rem'; } })
 table { font-size: 0.875rem; font-weight: 400; line-height: 1.5; direction: ltr; text-align: center !important; color: #6c757d !important; box-sizing: border-box; border-collapse: collapse; width: 100%; margin-bottom: 1rem; background-color: transparent; border: 1px solid #dee2e6; table-layout: fixed; } .timetable_event { width: 100%; line-height: normal; max-height: 2rem; overflow: hidden; }
 <table class="table table-bordered table-responsive-sm" id="calendar"> <thead> <tr> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> <th>Sun</th> </tr> </thead> <tbody id="calendar-body"> <tr> <td></td> <td>1 <div class="timetable_event"> <small>Subject: ABC</small> <br> <small>From: 02:00</small> <br> <small>To: 05:33</small> <br> </div> </td> <td></td> <td>2 <div class="timetable_event"> <small>Subject: [00000111] AUDICION Y LENGUAJE / LOGOPEDIA</small> <br> <small>From: 02:00</small> <br> <small>To: 05:33</small> <br> </div> </td> </tr> </tbody> </table>

have you tried to set the font-size using percentage value eg您是否尝试使用百分比值设置字体大小,例如

table {
  font-size: 100%; /*or 80% */
  font-weight: 400;
  line-height: 1.5;
  direction: ltr;
  text-align: center !important;
  color: #6c757d !important;
  box-sizing: border-box;
  border-collapse: collapse;
  width: 100%;
  margin-bottom: 1rem;
  background-color: transparent;
  border: 1px solid #dee2e6;
  table-layout: fixed;
}

try to play around using percentage value for the font size that could help you... i am not sure if you want to use javascript, because you can use javascript, to loop through the cells and get the text length on every cell and then set condition if length exceeds certain character use specific size percentage...尝试使用可以帮助您的字体大小的百分比值...我不确定您是否要使用 javascript,因为您可以使用 javascript,循环遍历单元格并获取每个单元格上的文本长度,然后如果长度超过特定字符使用特定大小百分比设置条件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM