Table once occupied an important position in web development - layout. Even in Web2.0, we can still see its layout. However, technology is constantly improving, and the Div CSS combination method has finally knocked on the door of old-fashioned layout and set off a new wave of layout. What followed was new and old grudges. Many people, whether they had opinions about table or not, also began to criticize table - bloated code, unsemantic tags, complicated writing methods, etc. . Remember, tables were not originally created for layout, but for displaying data. Abandoning the table layout does not mean abandoning the table itself. What can I do to save you, my table?
What is table:
Table is also an Html table, the carrier of data.
The following is a relatively standard way of writing table code:
Month th> | Date |
---|---|
AUG | 18< /td> |
I have to mention the table-layout:fixed attribute
table-layout: auto (default) | fixed.
Parameters:
auto: The default automatic algorithm. The layout will be based on the contents of each cell. The table will not be displayed until each cell is read and calculated, which is very slow.
fixed: Fixed layout algorithm. In this algorithm, the horizontal layout is only based on the width of the table, the width of the table border, the cell spacing, and the width of the columns, and has nothing to do with the table content. The parsing speed is fast.
Working steps of the fixed layout model:
1. All column elements whose width attribute value is not auto will set the width of the column according to the width value.
2. The cell width of this column in the first row of the table, set the width of this column according to the cell width. If the cell spans multiple columns, the width is divided evenly across the columns.
3. After the above two steps, if the width of the column is still auto, its size will be automatically determined so that its width is as equal as possible. At this time, the width of the table is set to the width value of the table or the sum of the column widths (whichever is larger). If the table width is greater than the sum of its column widths, divide the difference by the number of columns and add the resulting width to each column.
This method is fast because all column widths are defined by the first row of the table. Cells in all rows after the first row are sized according to the column width defined by the first row. Cells in these later rows do not change the column width. This means that any width values specified for these cells will be ignored.
Generally when making complex table HTML, sometimes you will find that no matter how you adjust the width of each column in the first row, the column width will still change unexpectedly (such as a long string of English text , and if there is no space in the middle, you want to limit the width of this column, so that the long text is forced to wrap without breaking the table, and often the result is that the appropriate width cannot be adjusted no matter how you try it). At this time, in desperation, you You can use table-layout:fixed.
Line breaks for difficult table problems
Using a table to display data sometimes has a headache, that is, displaying a certain text without line breaks, especially in the header th, which is used the most. In fact, the headache you have is not line wrapping, but the browser compatibility behind it makes line wrapping much more difficult. Here, you can check out the article Guide to forcing line breaks and forcing no line breaks to get inspiration. The article discusses in detail how to solve line breaks in different situations.
Generally speaking, the recommended way to wrap lines in a table is: first set table-layout: fixed for the table. Basically, after setting this attribute, the basic line wrapping problem can be solved without td in the table. ,Th due to the amount of each content inside, the situation of competing for the width of other td and th occurs. If you still have the problem of forced line breaks at this time, then add a layer of div inside the td, and then use the two CSS methods word-wrap:break-word; word-break:break-all; to solve the line break problem.
A few common but unfamiliar table tags
thead, tfoot and tbody
These three tags are a product of what is called xhtml, and mainly give you the ability to group rows in a table. When you create a table, you probably want to have a header row, some rows with data, and a total row at the bottom. This division gives the browser the ability to support table body scrolling independently of table headers and footers. When long tables are printed, the table header and footer can be printed on each page that contains the table data. Personally, I think its main purpose is suitable for display optimization of very long tables.
Thethead tag represents the HTML table header
the head thead of the table. You can use a separate style to define the table header, and you can print the table header at the top of the page when printing.
thead tag represents the HTML footer
The footer tfoot of the table, you can use a separate style to define the footer (footnote or table note), and when printing, you can print the page at the lower part of the page break foot.
tbody tag represents the HTML table body
When the browser displays the table, it usually downloads the table completely and then displays it in full. Therefore, when the table is very long, you can use tbody to display it in segments.
Note: If you use thead, tfoot, and tbody elements, you must use all of them. The order in which they appear is: thead, tfoot, tbody, so that the browser can render the header and footer before receiving all the data. You must use these tags inside the table element, and thead must have the tr tag inside it. Therefore, the more standard way to write a table is the following code:
Date | |
---|---|
Month Lists | Date Lists |
AUG | 18 |