Home > Web Front-end > HTML Tutorial > In-depth analysis of HTML table tags and related line breaks_HTML/Xhtml_Web page production

In-depth analysis of HTML table tags and related line breaks_HTML/Xhtml_Web page production

WBOY
Release: 2016-05-16 16:36:09
Original
2388 people have browsed it

What is table:
table is an Html table, the carrier of data.
The following is a relatively standard way of writing table code:

XML/HTML CodeCopy content to clipboard
  1. <table border="0" cellspacing="0" cellpadding="0" width="100%">
  2. <tr>
  3.  <th>Monthth> 
  4.  <th>Dateth> 
  5. tr>
  6. <tr>
  7.  <td>AUGtd>
  8.  <td>18td>
  9. tr>
  10. table>

A simple HTML table consists of the table element and one or more tr, th or td elements. The tr element defines the table row, the th element defines the header cell, and the td element defines the table cell. The border attribute specifies the width of the table border, cellpadding specifies the space between the cell edge and its content, and cellspacing specifies the space between cells. We generally set these three attributes to 0 manually to avoid browser differences. The width attribute specifies the width of the table, because the width of the table changes with the width of the internal elements. In common situations, we want the table to be the same width as the external container, so the default width is often set to 100% to fill the container.


I have to say the table-layout: fixed attribute
table-layout: auto (default) | fixed.
Parameters:
auto: 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.
The 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.

Common but unfamiliar table tags
thead, tfoot and tbody
These three tags are the products of the so-called xhtml, which mainly enable you to edit the rows in the table. Make groups. 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. The
thead 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:

XML/HTML CodeCopy content to clipboard
  1. <table border="0" cellspacing="0" cellpadding="0" width="100%">  
  2.   <thead>  
  3.     <tr>  
  4.       <th>Monthth>  
  5.       <th>Dateth>  
  6.     tr>  
  7.   thead>  
  8.   <tfoot>  
  9.     <tr>  
  10.       <th>Month Liststh>  
  11.       <th>Date Liststh>  
  12.     tr>  
  13.   tfoot>  
  14.   <tbody>  
  15.     <tr>  
  16.       <td>AUGtd>  
  17.       <td>18td>  
  18.     tr>  
  19.   tbody>  
  20. table>  

Personally, I think this thing is useless, useless and a pity to discard. Small projects can add some semantics, but because they have encountered the dilemma of having multiple different headers displayed in the same table, which limits future development, formal projects use these tags with caution from the perspective of scalability.

col and colgroup
are also products of xhtml. They are powerful and compatible. The
col tag defines attribute values ​​for one or more columns in the table. The
colgroup tag is used to group columns in the table for formatting.
Their main function is to control the width of cells, which saves the trouble of defining each cell separately. In the past, we often defined the width on th or td in the first row to specify the width of each column. , and col can not only define the width but also define other attributes at the same time. For example, you can use col to control the sum of the widths of several columns, and you can also control the background color of this column. But the ideal is full, and the reality is backbone. As mentioned before, the greater the function, the greater the compatibility. According to existing tests, there are only two applications that can play a role and ensure compatibility with col and colgroup. :width and background. For width, I personally prefer to use the conventional method, set the width in the first row and ensure the column width. As for background, in practice it is rare for tables to use different backgrounds in large areas. Therefore, I personally think: don’t use it as much as possible.

Where to use table
Personally, I think that in a container where the data is very dense and very serialized, it is correct to use table. The most common example is our common shopping order settlement page, which lists the details of your order: product name, unit price, purchase quantity, amount subtotal, shipping fee, etc. Finally, there will be a result of the final order amount at the bottom, table It can be said that it is like a fish in water here, and it has achieved the magical effect of a data carrier.


Line wrapping of table intricacies
Sometimes there is a headache when displaying data in a table, that is, displaying a certain text without wrapping, especially when used in table headers The most places. In fact, the headache you have is not line wrapping, but the browser compatibility behind it makes line wrapping much more difficult.
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.

Guidelines on forced line wrapping and forced non-line wrapping
The problem of forced line wrapping and forced non-line wrapping once bothered me. Whenever I encountered the problem of line wrapping, it was the beginning of painful memories. Now I finally learn from the pain. Let’s work together to solve this long-standing and stubborn problem.
Attributes used to force line breaks and force no line breaks
We generally use three CSS attributes to control line breaks: word-wrap; word-break; white-space. These three attributes can be said to be created specifically for text line breaking. First we have to know what these three attributes are used for:


word-wrap syntax
word-wrap: normal (default) | break-word
Each browser can recognize it
Parameters:
normal: Allows content to extend beyond the specified container boundaries.
break-word: Content will break within boundaries. Word-break will be triggered when necessary (note: please distinguish clearly that word-break and break-word are different things, one is an attribute and the other is a parameter).
Description:
word-wrap controls whether to "break lines for words", and sets or retrieves whether to break lines when the current line exceeds the boundary of the specified container. There is no problem with Chinese, and there is no problem with English sentences. But for long strings of English, it doesn't work.
Example:
The word congratulation belongs to a long string of English words. Word-wrap:break-word treats the entire word as a whole. If the end of the line is not wide enough to display the entire word, it will automatically put the entire word on the next line. , without truncating the words, which is why it doesn't work for long strings of text. word-wrap:normal is the default, and English words are not split.
Conclusion:
The scope of action is only standard block-level elements such as div. Table elements such as th and td are recognized but have no effect (if td, th plus width word-wrap can work under IE Effective, but from the perspective of complete compatibility and convenient memory, the previous conclusion shall prevail).


word-break syntax
word-break: normal (default) | break-all | keep-all
Parameters:
normal: according to Asian languages ​​and non- Text rules for Asian languages ​​that allow line breaks within words.
break-all: This behavior is the same as normal in Asian languages. Breaks within any word of a line of non-Asian language text are also allowed. This value is suitable for Asian text that contains some non-Asian text.
keep-all: Same as normal for all non-Asian languages. For Chinese, Korean, and Japanese, word breaks are not allowed. Good for non-Asian text that contains a small amount of Asian text.
Explanation:
word-break:break-all, is a break word. When a word reaches a boundary, the next letter automatically goes to the next line. It mainly solves the problem of long strings of English (it just makes up for the above defect that word-wrap:break-word does not work for long strings of text).
Example:
Continuing with the above, the word congratulation belongs to a long string of English words. Word-break:break-all will truncate the word, and the end of the line will become something like conra (the front part of congratulation). Next The backend part of behavioral tulation (conguatulation).
word-break:keep-all refers to Chinese, Japanese, and Korean continuous words. That is to say, if you only use this time without word-wrap, the Chinese will not wrap. (English sentences are normal.)
Conclusion:
only works on standard block-level elements such as div. Table elements such as th and td are recognized but have no effect (tested under Chrome word-break:break-all It is effective, but from the perspective of complete compatibility and convenient memory, the previous conclusion shall prevail). Firefox and Opera cannot recognize word-break, let alone the effect of using word-break in th and td under Firefox.

white-space syntax
white-space: normal (default) | pre | nowrap
Parameters:
normal: default. White space is ignored by the browser.
pre: Blank spaces will be retained by the browser. It behaves like the pre tag in HTML.
nowrap: The text will not wrap. The text will continue on the same line until the br tag is encountered.
Note:
For the pre attribute, multiple consecutive whitespace characters in HTML will be merged, and then in order to prevent them from being merged (the most common occasion is to indicate the indentation of code text), the whitespace characters will be continued. It is retained without requiring us to add additional styles and tags to control its indentation and line wrapping. The principle of the pre tag is the same. There is a white-space: pre by default.
For the nowrap attribute, this is the core of forcing no line breaks. Generally, this attribute is used to force no line breaks. There is no problem in Firefox's div and td, as well as in IE's div. The only flaw is that there is a problem in IE's td. If the td does not specify a width, nowrap is still valid. If the td has a width and there are no punctuation or spaces in the text (such as a long string of Chinese text), nowrap will no longer work. efficient. The solution is to add word-break:keep-all; to solve this problem.

Summary of forced line breaks:
If you need to force line breaks in standard block-level elements such as div, the most common solution is word-wrap:break-word; word-break:break- all; The disadvantage of this method is that if the end of the line happens to be a long string of English words, the word will be torn apart in an awkward manner (and FF does not recognize word-break, but will not tear the word apart). Personally, I think that if you put a long string of addresses similar to URLs in this div, then using this solution is a very good choice (Note: Since FF does not recognize word-break, there is no guarantee that the URL words will be neatly broken at the end of each line. , but this is also a helpless choice). If you are not placing a long string of English that can be broken such as a URL, but an English sentence, then use word-wrap:break-word;. As for the word-wrap:break-word; overflow:hidden; I saw on the Internet, it is said to be compatible with IE and FF, but after personal testing, it seems to have no special effect.

Summary of forced non-line wrapping:
The problem of forced non-line wrapping is relatively easy to analyze. As discussed above, use white-space:nowrap, Firefox's div and td, As well as in the div of IE, there is no problem. The only flaw is that there is a problem in IE's td. If the td does not specify a width, nowrap is still valid. If the td has a width and there are no punctuation or spaces in the text (such as a long string of Chinese text), nowrap will no longer work. efficient. The solution is to add word-break:keep-all; to solve this problem. To sum up, the safer way is to put another layer of div between the text and td, and then use nowrap, which will force no line breaks. Note that there is a high possibility that too much text will overflow the container at this time, so you have to add an overflow:hidden to prevent the container from overflowing, so that it can be compatible with various browsers.
After summarizing so much, I found that it seems that it is just a few attributes that balance the various compatibility in the browser. There seems to be no perfect solution that can be fully compatible with all browsers. The most we can do is Just try to keep the browser display consistent as much as possible. If you still feel that you must be compatible with all browsers, then the final solution is to use JS. In future articles, I will consider meeting this requirement with the smallest JS cost, but this is not within the scope of this article. .


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template