Stylish CSS Tables Tutorial

Stylish CSS Tables Tutorial


原文


image

CSS Tables. Veteran developers may cringe upon hearing those words, remembering the times when the typical site layout was created using them. Perhaps it invokes the pains of theming your old Myspace page with nested tablet after table. Maybe you're a dedicated HTML e-mail maker, and you're so desensitized to tables that there's no more hope for you. Whatever it may be, there is still one thing tables excel at above all HTML elements: presenting tabular data.

To make it clear, tabular data is information you'd normally represent in a column & row style fashion. If you were presenting it in any other medium, you'd probably end up using Excel for that data. However, the thought may already be creeping into your mind, "Oh yea, but tables are ugly!" That may be so, however, with some CSS magic we are capable of making some stylish tables. Let's see how.

Demo

One Two Three
Apples Carrots Steak
Oranges Potato Pork
Pears Peas Chicken

Basic CSS Table Structure

Let's make a simple 3x4 table containing columns for different food groups. Our HTML would look something like:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: "Roboto Mono", "Source Sans Pro", Monaco, courier, monospace !important; font-size: 0.85rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248) !important; position: relative !important; width: inherit; border: 1px solid rgb(244, 244, 244); -webkit-font-smoothing: initial; line-height: 1.43rem; border-radius: 2px; overflow-wrap: normal; margin: 0.8rem 0px !important; padding: 0.3rem 0px !important; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html"><table>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
</tr>
<tr>
<td>Apples</td>
<td>Carrots</td>
<td>Steak</td>
</tr>
<tr>
<td>Oranges</td>
<td>Potato</td>
<td>Pork</td>
</tr>
<tr>
<td>Pears</td>
<td>Peas</td>
<td>Chicken</td>
</tr>
</table> </pre>

Within a brand new HTML document, before any resets or CSS, you'd probably have something like this on your hands:

image

We want stylish CSS tables though, so it's time to call our buddy CSS and work some magic on this table!

Borders

When you're styling tables, you can obviously take some different approaches. You may want borders on a per-cell basis, or maybe just on the table itself. Let's do a quick test here. First, I'm going to make the table slightly bigger for the example's sale. Now, if I were to add a light grey border to all my cells (both and ) with this CSS:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n34" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: "Roboto Mono", "Source Sans Pro", Monaco, courier, monospace !important; font-size: 0.85rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248) !important; position: relative !important; width: inherit; border: 1px solid rgb(244, 244, 244); -webkit-font-smoothing: initial; line-height: 1.43rem; border-radius: 2px; overflow-wrap: normal; margin: 0.8rem 0px !important; padding: 0.3rem 0px !important; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="css">table { width: 640px; } /* Make table wider /
td, th { border: 1px solid #CCC; } /
Add borders to cells */ </pre>

Your CSS table will appear as so:

image

You'll notice the cells have some weird spacing between themselves. This may not concern you if you decided to not use borders, but we want full control of these things (and to avoid browser differences). A normal reset will add some extra properties to our table:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: "Roboto Mono", "Source Sans Pro", Monaco, courier, monospace !important; font-size: 0.85rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248) !important; position: relative !important; width: inherit; border: 1px solid rgb(244, 244, 244); -webkit-font-smoothing: initial; line-height: 1.43rem; border-radius: 2px; overflow-wrap: normal; margin: 0.8rem 0px !important; padding: 0.3rem 0px !important; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="css">table {
width: 640px;
border-collapse: collapse;
border-spacing: 0;
}
td, th { border: 1px solid #CCC; } </pre>

Which in turn will give us a better looking, but still totally basic table.

image

A Better CSS Table

Let's improve on this and add some visual improvements to our table:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: "Roboto Mono", "Source Sans Pro", Monaco, courier, monospace !important; font-size: 0.85rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248) !important; position: relative !important; width: inherit; border: 1px solid rgb(244, 244, 244); -webkit-font-smoothing: initial; line-height: 1.43rem; border-radius: 2px; overflow-wrap: normal; margin: 0.8rem 0px !important; padding: 0.3rem 0px !important; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="css">table {
color: #333; /* Lighten up font color /
font-family: Helvetica, Arial, sans-serif; /
Nicer font /
width: 640px;
border-collapse:
collapse; border-spacing: 0;
}

td, th { border: 1px solid #CCC; height: 30px; } /
Make cells a bit taller /

th {
background: #F3F3F3; /
Light grey background /
font-weight: bold; /
Make sure they're bold /
}

td {
background: #FAFAFA; /
Lighter grey background /
text-align: center; /
Center our text */
}</pre>

Now have a much nicer table!

image

This table is styled, but it's not quite a "stylish" table just yet. You'll notice it's very static, and still bland. With the power of the nth-child pseudo selector, watch what we can do!

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n47" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: "Roboto Mono", "Source Sans Pro", Monaco, courier, monospace !important; font-size: 0.85rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248) !important; position: relative !important; width: inherit; border: 1px solid rgb(244, 244, 244); -webkit-font-smoothing: initial; line-height: 1.43rem; border-radius: 2px; overflow-wrap: normal; margin: 0.8rem 0px !important; padding: 0.3rem 0px !important; color: rgb(52, 73, 94); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="css">table {
color: #333;
font-family: Helvetica, Arial, sans-serif;
width: 640px;
border-collapse:
collapse; border-spacing: 0;
}

td, th {
border: 1px solid transparent; /* No more visible border /
height: 30px;
transition: all 0.3s; /
Simple transition for hover effect /
}

th {
background: #DFDFDF; /
Darken header a bit /
font-weight: bold;
}

td {
background: #FAFAFA;
text-align: center;
}

/
Cells in even rows (2,4,6...) are one color /
tr:nth-child(even) td { background: #F1F1F1; }

/
Cells in odd rows (1,3,5...) are another (excludes header cells) /
tr:nth-child(odd) td { background: #FEFEFE; }

tr td:hover { background: #666; color: #FFF; }
/
Hover cell effect! */</pre>

image

Much fancier! In particular notice the last 3 rules we added. "nth-child()" allows us to choose the odd and even rows and style those cells however we want. You can also use :first-child or :last-child to, unsurprisingly, only style the first or last of a certain element. These are incredibly powerful selectors, however do note, in versions of Internet Explorer before 9 you'll need to use something like Selectivizr for your CSS to work properly. The darker cell (Peas) is what it would look like thanks to that hover property, and since we applied transition to our cell (which still needs prefixes for older webkit browser) it does a nice fade on our table. As a fun exercise, why don't you try to figure out how to make a table like this? (Answer can be found here, but no cheating!)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,802评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,109评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,683评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,458评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,452评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,505评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,901评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,550评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,763评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,556评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,629评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,330评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,898评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,897评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,140评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,807评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,339评论 2 342