總有那么幾個時候會覺得,table的td不能自適應換行真坑,憑什么只能用tr來換行,經常數據都是連在一起的呀,你叫我怎么把它拆分放到tr里......
那能不能用ul和li來替換?可以是可以,不過有時遇到“牛逼”的領導你能怎么辦,就要用table怎么辦!就問你怎么辦!!!
那就把tr當ul,td當li用咯!
1.效果圖:
2.這是重置樣式normalize.css代碼:

/** * Correct `block` display not defined in IE 8/9. * * 修正IE 8/9 中未定義的塊級元素。 */ article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary { display: block; } /** * Correct `inline-block` display not defined in IE 8/9. * * 修正在 IE 8/9 中未定義的 'inline-block' 元素。 */ audio, canvas, video { display: inline-block; } /** * Prevent modern browsers from displaying `audio` without controls. * Remove excess height in iOS 5 devices. * * 阻止現在瀏覽器顯示未定義 control 播放控件的 'audio' 聲音元素。 * 刪除 IOS 5 設備中顯示的多余的高度。 */ audio:not([controls]) { display: none; height: 0; } /** * Address styling not present in IE 8/9. * * 處理 IE 8/9 中不存在的樣式。 */ [hidden] { display: none; } /* ========================================================================== Base 基本設置 ========================================================================== */ /** * 1. Set default font family to sans-serif. * 2. Prevent iOS text size adjust after orientation change, without disabling * user zoom. * * 1. 設置默認字體類型為 sans-serif. * 2. 當用戶放大或縮小頁面時不改變字體大小。 */ html { font-family: sans-serif; /* 1 */ -ms-text-size-adjust: 100%; /* 2 */ -webkit-text-size-adjust: 100%; /* 2 */ } /** * Remove default margin. * * 刪除默認邊距。 */ body { margin: 0; } /* ========================================================================== Links 鏈接 ========================================================================== */ /** * Address `outline` inconsistency between Chrome and other browsers. * * 處理 Chrome 與其它瀏覽器中關於 'outline' 的不一致性。 */ a:focus { outline: thin dotted; } /** * Improve readability when focused and also mouse hovered in all browsers. * * 為所有瀏覽器改善當激活或懸停在元素上時元素內容的可讀性。 */ a:active, a:hover { outline: 0; } /* ========================================================================== Typography 排版 ========================================================================== */ /** * Address variable `h1` font-size and margin within `section` and `article` * contexts in Firefox 4+, Safari 5, and Chrome. * * 處理多變的 'h1' 字體大小及其在 Firefox 4+, Safari 5, 及 Chrome時瀏覽器中的 * 'section' 與 'article' 元素中的邊距。 */ h1 { font-size: 2em; margin: 0.67em 0; } /** * Address styling not present in IE 8/9, Safari 5, and Chrome. * * 處理在 IE 8/9, Safari 5, 及 Chrome 沒有的樣式。 */ abbr[title] { border-bottom: 1px dotted; } /** * Address style set to `bolder` in Firefox 4+, Safari 5, and Chrome. * * 處理 Firefox 4+, Safari 5, 及 Chrome 中默認的 'bolder' 樣式為 'bold'. */ b, strong { font-weight: bold; } /** * Address styling not present in Safari 5 and Chrome. * * 處理在 Safari 5 和 Chrome 沒有的樣式。 */ dfn { font-style: italic; } /** * Address differences between Firefox and other browsers. * * 處理 Firefox 與其它瀏覽器的差異。 */ hr { -moz-box-sizing: content-box; box-sizing: content-box; height: 0; } /** * Address styling not present in IE 8/9. * * 處理在 IE 8/9 中沒有的樣式。 */ mark { background: #ff0; color: #000; } /** * Correct font family set oddly in Safari 5 and Chrome. * * 修正確 Safari 5 和 Chrome 中古怪的默認字體。 */ code, kbd, pre, samp { font-family: monospace, serif; font-size: 1em; } /** * Improve readability of pre-formatted text in all browsers. * * 為所有瀏覽器改善預格式化文本的可讀性。 */ pre { white-space: pre-wrap; } /** * Set consistent quote types. * * 設置一致的引用格式。 */ q { quotes: "\201C" "\201D" "\2018" "\2019"; } /** * Address inconsistent and variable font size in all browsers. * * 處理所有瀏覽器中字體大小的不一致性[譯者注:原文直譯為:處理所有 * 瀏覽器中的不一致和多變的字體大小]。 */ small { font-size: 80%; } /** * Prevent `sub` and `sup` affecting `line-height` in all browsers. * * 阻止所有瀏覽器中 'sub' 和 'sup' 元素影響 'line-height'. * [譯者注:就是不讓上標與下標影響行高。] */ sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; } sup { top: -0.5em; } sub { bottom: -0.25em; } /* ========================================================================== Embedded content 嵌入的內容 ========================================================================== */ /** * Remove border when inside `a` element in IE 8/9. * * 刪除 IE 8/9 中當內容位於 'a' 中出現的邊框。 */ img { border: 0; } /** * Correct overflow displayed oddly in IE 9. * * 修正 IE 9 中顯示古怪的溢出內容。 */ svg:not(:root) { overflow: hidden; } /* ========================================================================== Figures Figure 圖像/圖表/代碼等 ========================================================================== */ /** * Address margin not present in IE 8/9 and Safari 5. * * 處理在 IE 8/9 和 Safari 5 沒有的邊距。 */ figure { margin: 0; } /* ========================================================================== Forms ========================================================================== */ /** * Define consistent border, margin, and padding. * * 定義一致的邊框、外邊距及內邊距。 */ fieldset { border: 1px solid #c0c0c0; margin: 0 2px; padding: 0.35em 0.625em 0.75em; } /** * 1. Correct `color` not being inherited in IE 8/9. * 2. Remove padding so people aren't caught out if they zero out fieldsets. * 1. 修正在 IE 8/9 中沒有繼承的 'color'. * * [譯者注:說是修正顏色嘛,可下面沒有關於顏色的呀,這也行?求大神解釋!] * 2. 去掉內邊距,避免當用戶清空表單組時認為出錯了。 */ legend { border: 0; /* 1 */ padding: 0; /* 2 */ } /** * 1. Correct font family not being inherited in all browsers. * 2. Correct font size not being inherited in all browsers. * 3. Address margins set differently in Firefox 4+, Safari 5, and Chrome. * * 1. 修正所有瀏覽器中未被繼承的字體類型。 * 2. 修正所有瀏覽器中未被繼承的字體大小。 * 3. 處理 Firefox 4+, Safari 5, 及 Chrome 中默認設置不同的外邊距。 */ button, input, select, textarea { font-family: inherit; /* 1 */ font-size: 100%; /* 2 */ margin: 0; /* 3 */ } /** * Address Firefox 4+ setting `line-height` on `input` using `!important` in * the UA stylesheet. * * 處理 Firefox 4+ 中的客戶端樣式表里使用 '!important' 設置的 'line-height'. */ button, input { line-height: normal; } /** * Address inconsistent `text-transform` inheritance for `button` and `select`. * All other form control elements do not inherit `text-transform` values. * Correct `button` style inheritance in Chrome, Safari 5+, and IE 8+. * Correct `select` style inheritance in Firefox 4+ and Opera. * * 處理 'button' 和 'select' 的 'text-transform' 繼承的不一致性。 * 所有其它表單控件元素不繼承 'text-transform' 的值。 * 修正 Chrome, Safari 5+, 及 IE 8+ 中 'button' 的繼承樣式。 * 修正 Firefox 4+ 和 Opera 中 'select' 的繼承樣式。 */ button, select { text-transform: none; } /** * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` * and `video` controls. * 2. Correct inability to style clickable `input` types in iOS. * 3. Improve usability and consistency of cursor style between image-type * `input` and others. * * 1. 避免 Android 4.0.* 中 WebKit 的一個bug, 防止 'audio' 與 'video' 的播放控件失效。 * 2. 修正 iOS 中不可點擊的 'input' 樣式。 * 3. 改善圖片類型的 'input' 等光標樣式的可用性與一致性。 */ button, html input[type="button"], /* 1 */ input[type="reset"], input[type="submit"] { -webkit-appearance: button; /* 2 */ cursor: pointer; /* 3 */ } /** * Re-set default cursor for disabled elements. * * 重置不可用元素的默認光標樣式。 */ button[disabled], html input[disabled] { cursor: default; } /** * 1. Address box sizing set to `content-box` in IE 8/9. * 2. Remove excess padding in IE 8/9. * * 1. 處理 IE 8/9 中設置為 'content-box' 的盒子模型。 * 2. 刪除 IE 8/9 中多余的內邊距。 */ input[type="checkbox"], input[type="radio"] { box-sizing: border-box; /* 1 */ padding: 0; /* 2 */ } /** * 1. Address `appearance` set to `searchfield` in Safari 5 and Chrome. * 2. Address `box-sizing` set to `border-box` in Safari 5 and Chrome * (include `-moz` to future-proof). * * 1. 處理 Safari 5 和 Chrome 中默認設置為 'appearance' 的 'searchfield'. * 2. 處理 Safari 5 和 Chrome 中默認設置為 'box-sizing' 的 'border-box' * (包括不會過時的 '-moz'). */ input[type="search"] { -webkit-appearance: textfield; /* 1 */ -moz-box-sizing: content-box; -webkit-box-sizing: content-box; /* 2 */ box-sizing: content-box; } /** * Remove inner padding and search cancel button in Safari 5 and Chrome * on OS X. * * 刪除 Safari 5 和 OS X 上的 Chrome 中的輸入框上的內邊距和搜索取消按鈕。 */ input[type="search"]::-webkit-search-cancel-button, input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; } /** * Remove inner padding and border in Firefox 4+. * * 刪除 Firefox 4+ button 與 input 上的內邊距。 */ button::-moz-focus-inner, input::-moz-focus-inner { border: 0; padding: 0; } /** * 1. Remove default vertical scrollbar in IE 8/9. * 2. Improve readability and alignment in all browsers. * * 1. 刪除 IE8/9 中默認的垂直滾動條。 * 2. 改善所有瀏覽器中的可讀性並使文本垂直對齊。 */ textarea { overflow: auto; /* 1 */ vertical-align: top; /* 2 */ } /* ========================================================================== Tables 表格 ========================================================================== */ /** * Remove most spacing between table cells. * * 刪除表格里單元格間的間距。 */ table { border-collapse: collapse; border-spacing: 0; }
3.html:
<div class="table"> <table class="table_box"> <tr> <td> <div class="my_title">this is a title so long that become height</div> <div class="my_content">content1</div> </td> <td> <div class="my_title">title2</div> <div class="my_content">this is the content2,this is the content2,this is the content2,this is the content2</div> </td> <td> <div class="my_title">title3</div> <div class="my_content">this is the content3</div> </td> </tr> </table> </div>
4.css:
.table{margin: 20px;} .table_box{width:100%;font-size:12px;} .table_box td{border:1px solid #000;border-right:none;border-top:none;display:inline-block;padding:0;width: 80px;} .table_box td:last-child{border-right:1px solid #000 !important;} .my_title{border-bottom:1px solid #000;display:flex;align-items:center;line-height:16px;padding:5px;} .my_content{padding: 5px;}
5.js:
function tabFun(){ var tabW = $(".table").width(); // 表格的寬度 var tdW = $(".table_box td").width(); // 一個內容的寬度 var bor = parseInt(tabW/tdW); // 一行有多少個td var cha = tabW - tdW*bor; // 差值 if (cha <= bor) { // 去掉一個邊框的寬度 bor--; } // 清空邊框 $(".table_box td").css({"border-right":"none","border-top":"none"}); // 設置相應的邊框 $(".table_box td:nth-child("+bor+"n)").css({"border-right":"1px solid #000"}); $(".table_box td").slice(0,bor).css({"border-top":"1px solid #000"}); // 所有.my_title的高度集合 var titH = []; // 所有.my_content的高度集合 var conH = []; $(".my_content").each(function(index, el) { // 把高度數據存放到conH數組里面 conH.push($(this).height()); }); $(".my_title").each(function(index, el) { // 把高度數據存放到titH數組里面 titH.push($(this).height()); }); // 獲取最大高度並把其它my_content高度設為最大高度 $(".my_content").height(Math.max.apply(null, conH)); // 獲取最大高度並把其它my_title高度設為最大高度 $(".my_title").height(Math.max.apply(null, titH)); } tabFun(); $(window).resize(function(event) { tabFun(); });
6.最后:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>table的td自動換行</title> <link rel="stylesheet" href="normalize.css"> <style type="text/css"> .table{margin: 20px;} .table_box{width:100%;font-size:12px;} .table_box td{border:1px solid #000;border-right:none;border-top:none;display:inline-block;padding:0;width: 80px;} .table_box td:last-child{border-right:1px solid #000 !important;} .my_title{border-bottom:1px solid #000;display:flex;align-items:center;line-height:16px;padding:5px;} .my_content{padding: 5px;} </style> </head> <body> <div class="table"> <table class="table_box"> <tr> <td> <div class="my_title">this is a title so long that become height</div> <div class="my_content">content1</div> </td> <td> <div class="my_title">title2</div> <div class="my_content">this is the content2,this is the content2,this is the content2,this is the content2</div> </td> <td> <div class="my_title">title3</div> <div class="my_content">this is the content3</div> </td> <td> <div class="my_title">title1</div> <div class="my_content">this is the content1</div> </td> <td> <div class="my_title">title2</div> <div class="my_content">this is the content2</div> </td> <td> <div class="my_title">title3</div> <div class="my_content">this is the content3</div> </td> <td> <div class="my_title">title1</div> <div class="my_content">this is the content1</div> </td> <td> <div class="my_title">title2</div> <div class="my_content">this is the content2</div> </td> <td> <div class="my_title">title3</div> <div class="my_content">this is the content3</div> </td> <td> <div class="my_title">title1</div> <div class="my_content">this is the content1</div> </td> <td> <div class="my_title">title2</div> <div class="my_content">this is the content2</div> </td> <td> <div class="my_title">title3</div> <div class="my_content">this is the content3</div> </td> </tr> </table> </div> <script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script> <script> function tabFun(){ var tabW = $(".table").width(); // 表格的寬度 var tdW = $(".table_box td").width(); // 一個內容的寬度 var bor = parseInt(tabW/tdW); // 一行有多少個td var cha = tabW - tdW*bor; // 差值 if (cha <= bor) { // 去掉一個邊框的寬度 bor--; } // 清空邊框 $(".table_box td").css({"border-right":"none","border-top":"none"}); // 設置相應的邊框 $(".table_box td:nth-child("+bor+"n)").css({"border-right":"1px solid #000"}); $(".table_box td").slice(0,bor).css({"border-top":"1px solid #000"}); // 所有.my_title的高度集合 var titH = []; // 所有.my_content的高度集合 var conH = []; $(".my_content").each(function(index, el) { // 把高度數據存放到conH數組里面 conH.push($(this).height()); }); $(".my_title").each(function(index, el) { // 把高度數據存放到conH數組里面 titH.push($(this).height()); }); // 獲取最大高度並把其它my_content高度設為最大高度 $(".my_content").height(Math.max.apply(null, conH)); // 獲取最大高度並把其它my_title高度設為最大高度 $(".my_title").height(Math.max.apply(null, titH)); } tabFun(); $(window).resize(function(event) { tabFun(); }); </script> </body> </html>