How To Break Long Words In An HTML (or CSS) Table - Design-fluide
Có thể bạn quan tâm
If you’re reading this you’re either curious or you’ve got serious issue trying to handle long words in a table cell. As I had. So, here is a full review of my investigations to save you some time.
Following solutions work on both HTML and CSS tables, and are supported by modern browsers and IE8+.
- [#] Breaking words with word-wrap and max-width
- [#] Breaking words with word-wrap and table-layout
- [#] Breaking words with word-break
- [#] Make breaks more elegant using CSS hyphens
Breaking words with word-wrap and max-width
word-wrap prevents a long word from overflowing its container by breaking the text onto the next line. It works fine when applied on a block element (such as a <div> or a <p>), but has no effect within a table.
To get word-wrap working on a table cell, max-width: 1px is the magic fix you need:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis dui. Duis tempor ligula scelerisque sodales faucibus. Quisque sodales leo nec. | Loremipsumdolorsitametconsectetur |
Note this max-width trick also works to make an ellipsis within a table.
Should I use word-wrap or overflow-wrap?
word-wrap is the historic and nonstandard property. It has been renamed to overflow-wrap but remains an alias browers must support in future. Many browsers (especially the old ones) don’t support overflow-wrap and require word-wrap as a fallback (which is supported by all).
If you want to please the W3C you should consider associate both in your CSS. If you don’t, using word-wrap alone is just fine.
Breaking words with word-wrap and table-layout
Associate word-wrap and table-layout: fixed works to break long words in a table cell, but this solution has some constraints you should consider carefully.
By using table-layout with fixed value you change the algorithm used to lay out table cells, rows, and columns:
- In Firefox, paddings and borders are not taken into account in the calculation of the column widths. That means columns won’t size exactly the way you defined them.
- In all browsers, if no width has been defined, columns will all get the same size which is maybe not what you want.
** Not 75% in Firefox ** Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis dui. | Loremipsumdolorsitamet |
Breaking words with word-break
word-break specifies how words should be broken when reaching the end of the line.
Used with the break-all value, browsers will insert line break between any two characters, while word-wrap will only create a break if the word can’t stand on its own line.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis dui. Duis tempor ligula scelerisque sodales faucibus. | Lorem ipsum lorem ipsum lorem ipsum |
Make breaks more elegant using CSS hyphens
hyphens property allows text to be hyphenated when words are too long to fit in one line. Hyphenation opportunities depend on the language of your content.
Native support is not that good at the moment. Worst thing is hyphens is not working at all in Windows Chrome (it does work on Android and Mac OS plateforms). Proprietary prefixes and one of the word-wrap fix (max-width or table-layout) as a complement are necessary to make this solution viable.
Since hyphenation rules are language-specific, you also need the lang attribute to be defined on a parent element (mostly on the <html> tag).
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis dui. Duis tempor ligula scelerisque sodales faucibus. Quisque sodales leo nec. | loremipsumdolorsitametconsectetur |
Từ khóa » Html Table Fixed Column Width Not Word Wrap
-
Html - Table Cell Widths - Fixing Width, Wrapping/truncating Long Words
-
How To Wrap The Content Of A Table Cell - W3docs
-
Wrap Or Break Long Word (text) In HTML Table Cell - Dev Bay
-
Table-layout - CSS: Cascading Style Sheets - MDN Web Docs
-
Overflow-wrap - CSS: Cascading Style Sheets - MDN Web Docs
-
Table Column Header Text Wrap Code Example
-
CSS Table-layout Property - W3Schools
-
Wrap Long Text (without Space) In Html Table Cell - MSDN - Microsoft
-
How To Wrap Table Cell
Content Using CSS ? - GeeksforGeeks DataTable Width & Column Width | Dash For Python Documentation
Sql Server Email Html Table Column Wrap - Microsoft Q&A
Table Layout And Word Wrap - WpDataTables Tables And Charts Plugin
Fixed Table Layouts | CSS-Tricks
[Solved -11 Answers] HTML – Word-wrap In An HTML Table - Wikitechy
Copyright © 2022 | Thiết Kế Truyền Hình Cáp Sông Thu