forked from ssw15/table_practice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tables.html
199 lines (179 loc) · 5.88 KB
/
tables.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html>
<head>
<title>Tables</title>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style>
td, th {
border: thin gray solid;
}
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>HTML Tables</h1>
</div>
<p>The way to make a table of data in HTML is to use the <code><table></table></code> element. And, similar to how we used an <code>li</code> for each list item within a <code>ul</code> (unordered list), we use a <code>tr</code> (table row) for each row within a table:</p>
<pre>
<table>
<tr></tr>
<tr></tr>
</table></pre>
<p>But that would just produce empty rows. For each cell in the row, you need to add a <code><td></code> (table data) element:</p>
<pre>
<table>
<tr><td>Andrew Luck</td><td>Quarterback</td></tr>
<tr><td>Wes Welker</td><td>Wide Receiver</td></tr>
<tr><td>Matt Forte</td><td>Running Back</td></tr>
</table></pre>
<p>which produces</p>
<p>
<table>
<tr><td>Andrew Luck</td><td>Quarterback</td></tr>
<tr><td>Wes Welker</td><td>Wide Receiver</td></tr>
<tr><td>Matt Forte</td><td>Running Back</td></tr>
</table>
</p>
<p>You need to have the same number of <code>td</code>s, or cells, in each row or your table is going to look wonky.</p>
<p>Usually in our code we prefer to keep each element on its own line, for our own sanity, unless it is a simple inline element like <code><em></code> or <code><small></code> or <code><span></code>; so tables usually look like this:</p>
<pre>
<code><table>
<tr>
<td>Andrew Luck</td>
<td>Quarterback</td>
</tr>
<tr>
<td>Wes Welker</td>
<td>Wide Receiver</td>
</tr>
<tr>
<td>Matt Forte</td>
<td>Running Back</td>
</tr>
</table></code></pre>
<p>You can also use <code><th></code> (table heading) elements instead of <code><td></code> for cells which represent column headings:</p>
<pre>
<code><table>
<tr>
<th>Player</th>
<th>Position</th>
</tr>
<tr>
<td>Andrew Luck</td>
<td>Quarterback</td>
</tr>
<tr>
<td>Wes Welker</td>
<td>Wide Receiver</td>
</tr>
<tr>
<td>Matt Forte</td>
<td>Running Back</td>
</tr>
</table></code></pre>
<p>which produces</p>
<p>
<table>
<tr>
<th>Player</th>
<th>Position</th>
</tr>
<tr>
<td>Andrew Luck</td>
<td>Quarterback</td>
</tr>
<tr>
<td>Wes Welker</td>
<td>Wide Receiver</td>
</tr>
<tr>
<td>Matt Forte</td>
<td>Running Back</td>
</tr>
</table>
</p>
<p>Finally, and optionally, you can use <code><thead></code> to group rows containing headings and <code><tbody></code> to group rows containing data:</p>
<pre>
<code><table>
<thead>
<tr>
<th>Player</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td>Andrew Luck</td>
<td>Quarterback</td>
</tr>
<tr>
<td>Wes Welker</td>
<td>Wide Receiver</td>
</tr>
<tr>
<td>Matt Forte</td>
<td>Running Back</td>
</tr>
</tbody>
</table></code></pre>
<p>The last thing to note is, if you need to "merge" two cells together, you can make one cell and give it the attribute <code>colspan="2"</code>:</p>
<pre>
<code><table>
<thead>
<tr>
<th colspan="2">My Fantasy Team</th>
</tr>
<tr>
<th>Player</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td>Andrew Luck</td>
<td>Quarterback</td>
</tr>
<tr>
<td>Wes Welker</td>
<td>Wide Receiver</td>
</tr>
<tr>
<td>Matt Forte</td>
<td>Running Back</td>
</tr>
</tbody>
</table></code></pre>
<p>which produces</p>
<p>
<table>
<thead>
<tr>
<th colspan="2">My Fantasy Team</th>
</tr>
<tr>
<th>Player</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td>Andrew Luck</td>
<td>Quarterback</td>
</tr>
<tr>
<td>Wes Welker</td>
<td>Wide Receiver</td>
</tr>
<tr>
<td>Matt Forte</td>
<td>Running Back</td>
</tr>
</tbody>
</table>
</p>
<p>You can similarly "merge" a cell with cells below in the same column, but you use the <code>rowspan=""</code> attribute instead. Then make sure you have one less <code><td></code> in the rows below (however many you chose to span) so that everything still adds up right.</p>
<hr>
</div>
</body>
</html>