HTML <tr> 表ã®è¡è¦ç´
Baseline
åºãå©ç¨å¯è½
*
ãã®æ©è½ã¯åºãå®è£ ããã¦ãããå¤ãã®ãã¼ã¸ã§ã³ã®ç«¯æ«ããã©ã¦ã¶ã¼ã§åä½ãã¾ãã2015å¹´7æä»¥éããã¹ã¦ã®ãã©ã¦ã¶ã¼ã§å©ç¨å¯è½ã§ãã
* ãã®æ©è½ã®ä¸é¨ã¯ã対å¿ã¬ãã«ãç°ãªãå ´åãããã¾ãã
<tr> 㯠HTML ã®è¦ç´ ã§ã表ã®ä¸ã®ã»ã«ã®è¡ãå®ç¾©ãã¾ããè¡ã®ã»ã«ã«ã¯ <td> ï¼è¡ã®ã»ã«ï¼ããã³ <th> ï¼è¦åºãã»ã«ï¼è¦ç´ ãæ··å¨ããããã¨ãã§ãã¾ãã
試ãã¦ã¿ã¾ããã
<table>
<caption>
Alien football stars
</caption>
<thead>
<tr>
<th scope="col">Player</th>
<th scope="col">Gloobles</th>
<th scope="col">Za'taak</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">TR-7</th>
<td>7</td>
<td>4,569</td>
</tr>
<tr>
<th scope="row">Khiresh Odo</th>
<td>7</td>
<td>7,223</td>
</tr>
<tr>
<th scope="row">Mia Oolong</th>
<td>9</td>
<td>6,219</td>
</tr>
</tbody>
</table>
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
th[scope="col"] {
background-color: #505050;
color: white;
}
th[scope="row"] {
background-color: #d6ecd4;
}
td {
text-align: center;
}
tr:nth-of-type(even) {
background-color: #eeeeee;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
caption {
caption-side: bottom;
padding: 10px;
}
屿§
ãã®è¦ç´ ã«ã¯ã°ãã¼ãã«å±æ§ãããã¾ãã
鿍奍ã®å±æ§
以ä¸ã®å±æ§ã¯éæ¨å¥¨ã¨ãªã£ã¦ããããã使ç¨ããªãã§ãã ããããããã¯ãæ¢åã®ã³ã¼ããæ´æ°ããéã®åèã¨ãã¦ãã¾ãæ´å²çãªé¢å¿ã®ããã ãã«ãä¸è¨ã«è¨è¼ããã¦ãã¾ãã
align-
ããããã®è¡ã®ã»ã«ã®æ°´å¹³æ¹åã®é ç½®ãæå®ãã¾ãã使ç¨å¯è½ãªåæå¤ã¯ã
leftãcenterãrightãjustifyãcharã§ãã対å¿ãã¦ãã¦ããcharå¤ã¯ãchar屿§ã§å®ç¾©ãããæåã¨ãcharoff屿§ã§å®ç¾©ããããªãã»ããã«åºã¥ãã¦ãããã¹ãã³ã³ãã³ããé ç½®ãã¾ãããã®å±æ§ã¯éæ¨å¥¨ã¨ãªã£ã¦ããããã代ããã« CSS ã®text-alignããããã£ã使ç¨ãã¦ãã ããã bgcolor-
æååã§ãè¡ã®åã»ã«ã®èæ¯è²ãå®ç¾©ãã¾ãããã®å¤ã¯ HTML è²ã§ããã6 æ¡ã® 16 鲿°ã® RGB ã³ã¼ãã®åã« '
#' ãä»ããå½¢ãã¾ãã¯è²ãã¼ã¯ã¼ãã®ã©ã¡ããã§ãããã以å¤ã® CSS ã®<color>å¤ã«ã¯å¯¾å¿ãã¦ãã¾ããããã®å±æ§ã¯éæ¨å¥¨ã§ãã®ã§ã代ããã« CSS ã®background-colorããããã£ã使ç¨ãã¦ãã ããã char-
ããããã®è¡ã®ã»ã«å ã®ã³ã³ãã³ããããã®ã»ã«å ã®æåã«å¯¾ãã¦é ç½®ããä½ç½®ãæå®ãã¾ããããããå¤ã¨ãã¦ã¯ãæ°å¤ãéé¡ãé ç½®ããå ´åã®ããªãªã (
.) ãããã¾ããalignãcharã«è¨å®ããã¦ããªãå ´åããã®å±æ§ã¯ç¡è¦ããã¾ãã charoff-
char屿§ã§æå®ãããé ç½®æåãããè¡ã®ã»ã«ã®ã³ã³ãã³ãããªãã»ããããæåæ°ãæå®ãã¾ãã valign-
åå ã®åã»ã«ã®åç´æ¹åã®é ç½®ãæå®ãã¾ããæå®å¯è½ãªåæå¤ã¯ã
baselineãbottomãmiddleãtopã§ãããã®å±æ§ã¯éæ¨å¥¨ã¨ãªã£ã¦ããããã代ããã« CSS ã®vertical-alignããããã£ã使ç¨ãã¦ãã ããã
使ç¨ä¸ã®æ³¨æ
<tr>è¦ç´ ã¯ã<thead>ã<tbody>ã<tfoot>è¦ç´ ã®åè¦ç´ ã¨ãã¦ã®ã¿æå¹ã§ãã<tr>ã親è¦ç´ ã§ãã<table>ã®ç´æ¥ã®åè¦ç´ ã¨ãã¦é ç½®ããã¦ããå ´åã<tbody>è¦ç´ ãæé»çã«è¦ªã¨ãã¦æ±ããããã©ã¦ã¶ã¼ããã¼ã¯ã¢ããã«<tbody>ã追å ãã¾ãã- æé»ã®è¦ªè¦ç´
<tbody>ã¯ã<table>ãåè¦ç´ ã¨ãã¦<tbody>ãæã£ã¦ããªãå ´åã§ããããã¤<tr>ã<caption>ã<colgroup>ã<thead>è¦ç´ ã®ã©ããããå¾ã«è¨è¼ããã¦ããå ´åã«ã®ã¿å¯¾å¿ãã¾ãã - CSS ã®æ¬ä¼¼ã¯ã©ã¹
:nth-of-typeã:first-of-typeã:last-of-typeã¯ãå¤ãã®å ´åãç®çã®è¡ã®ã»ããã¨ãã®è¡ã®ã»ã«ããã³è¦åºãã»ã«ï¼<td>ããã³<th>è¦ç´ ï¼ã鏿ããéã«å½¹ç«ã¡ã¾ãã <tr>ã<table>ã®ç´æ¥ã®åè¦ç´ ã¨ãã¦å«ã¾ãã¦ããå ´åããã©ã¦ã¶ã¼ããã¼ã¯ã¢ããã«<tbody>ã追å ãããããtable > trãªã©ã® CSS ã»ã¬ã¯ã¿ã¼ãæå¾ éãã«åä½ããªãããããã¯ã¾ã£ããåä½ããªããã¨ãããã¾ãã
ä¾
ä¸è¬çãªæ¨æºãæåã®ææ³ãç´¹ä»ããå®å
¨ãªè¡¨ã®ä¾ã«ã¤ãã¦ã¯ã<table> ãã覧ãã ããã
åºæ¬çãªè¡ã®ä½æ
ãã®ä¾ã§ã¯ã4 è¡ 3 åã®è¡¨ã示ãã¦ãã¾ããæåã®åã«ã¯ãè¡ã®ãã¼ã¿ã»ã«ã«å¯¾ããè¦åºããå«ã¾ãã¦ãã¾ãã
HTML
4 ã¤ã® <tr> è¦ç´ ã使ç¨ãã¦ã表㫠4 ã¤ã®è¡ã使ãã¾ããããããã®è¡ã«ã¯ 3 ã¤ã®ã»ã«ï¼1 ã¤ã®è¦åºãã»ã«ï¼<th>ï¼ã¨ 2 ã¤ã®ãã¼ã¿ã»ã«ï¼<td>ï¼ï¼ãå«ã¾ãã¦ããã3 ã¤ã®åãå½¢æããã¾ããããããã®è¦åºãã»ã«ã«è¨å®ããã scope 屿§ã¯ããã®ã»ã«ãã©ã®ã»ã«ã«é¢é£ããããæå®ãã¾ãããã®ä¾ã§ã¯ãrow å
ã®ãã¹ã¦ã®ãã¼ã¿ã»ã«ã対象ã¨ãªãã¾ãã
<table>
<tbody>
<tr>
<th scope="row">A</th>
<td>Alfa</td>
<td>AL fah</td>
</tr>
<tr>
<th scope="row">B</th>
<td>Bravo</td>
<td>BRAH voh</td>
</tr>
<tr>
<th scope="row">C</th>
<td>Charlie</td>
<td>CHAR lee</td>
</tr>
<tr>
<th scope="row">D</th>
<td>Delta</td>
<td>DELL tah</td>
</tr>
</tbody>
</table>
CSS
CSS ã® :nth-of-type æ¬ä¼¼ã¯ã©ã¹ã使ç¨ãã¦ããã¹ã¦ã®å¥æ° (odd) è¡ã鏿ãããããã®è¡ã® background-color ãå°ãæ¿ãè²ã«è¨å®ãããã¨ã§ããããããã¼ãã©ã¹ãã©ã¤ãã广ã使ãã¦ãã¾ããèæ¯è²ã交äºã«ãããã¨ã«ããã表å
ã®ãã¼ã¿è¡ãè§£éãèªã¿åãããããã¾ããè¡ã¨åã®æ°ãå¤ãä¸ã§ãç¹å®ã®è¡ã«ãããã¼ã¿ãæ¢ãå ´é¢ãæ³åãã¦ã¿ã¦ãã ãããããã«ãè¡ãããã¼ã®è¦åºãã»ã«ï¼<th> è¦ç´ ï¼ã¯ãbackground-colorãã¤ãã¦å¼·èª¿è¡¨ç¤ºãããã¼ã¿ã»ã«ï¼<td>è¦ç´ ï¼ã¨å¤å¥ã§ããããã«ãã¦ãã¾ãã
tr:nth-of-type(odd) {
background-color: #eeeeee;
}
tr th[scope="row"] {
background-color: #d6ecd4;
}
çµæ
ãããã¼è¡
ãã®ä¾ã§ã¯ãååã®ä¾ã®åºæ¬çãªè¡¨ãæ¡å¼µãã表㮠1 è¡ç®ã¨ãã¦ãããã¼è¡ã追å ãã¦ãã¾ãã
HTML
表㮠1 è¡ç®ã¨ãã¦è¿½å ã®è¡ (<tr>) ã追å ãããããããã®åã®ãããã¼ã¨ãªãè¦åºãã»ã« (<th>) ãæå®ããã¾ãããã®è¡ã <thead> ã°ã«ã¼ãåè¦ç´ å
ã«é
ç½®ãããã¨ã§ãããã表ã®ãããã¼ã§ããæ¨ã示ãã¾ããscope 屿§ãããã®ãããã¼è¡å
ã®åè¦åºãã»ã« (<th>) ã«è¿½å ãããã¨ããããã®ã»ã«ã <tbody> å
ã«ãã£ãã¨ãã¦ããåè¦åºãã»ã«ãåãã®åå
ã®ãã¹ã¦ã®ã»ã«ã«é¢é£ãã¦ãããã¨ãæç¤ºçã«æå®ãã¾ãã
<table>
<thead>
<tr>
<th scope="col">è¨å·</th>
<th scope="col">Code word</th>
<th scope="col">çºé³</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">A</th>
<td>Alfa</td>
<td>AL fah</td>
</tr>
<tr>
<th scope="row">B</th>
<td>Bravo</td>
<td>BRAH voh</td>
</tr>
<tr>
<th scope="row">C</th>
<td>Charlie</td>
<td>CHAR lee</td>
</tr>
<tr>
<th scope="row">D</th>
<td>Delta</td>
<td>DELL tah</td>
</tr>
</tbody>
</table>
CSS
CSS ã¯åã®ä¾ã¨ã»ã¼åãã§ãããããããã¼è¡ãã強調ããããã®ã¹ã¿ã¤ã«è¨å®ãããã¤ã追å ãã¦ãããããã«ããåã®è¦åºããä»ã®ã»ã«ã¨åºå¥ãããããªã£ã¦ãã¾ãã
tr:nth-of-type(odd) {
background-color: #eeeeee;
}
tr th[scope="col"] {
background-color: #505050;
color: white;
}
tr th[scope="row"] {
background-color: #d6ecd4;
}
çµæ
è¡ã®ä¸¦ã¹æ¿ã
<table> ã®è¡ï¼<tr> è¦ç´ ï¼ãã½ã¼ãããããã®çµã¿è¾¼ã¿æ©è½ã¯ããã¾ãããããããArray.prototype.sort()ãNode.removeChildãNode.appendChild ã使ç¨ãããã¨ã§ã<tr> è¦ç´ ã® HTMLCollection ãã½ã¼ãããããã®ç¬èªã® sort() 颿°ã JavaScript ã§å®è£
ãããã¨ãã§ãã¾ãã
HTML
ãã®åºæ¬çãªè¡¨ã§ã¯ã<tbody> è¦ç´ ã使ç¨ãã¦è¡¨ã®æ¬ä½é¨åãæå®ãããã¼ã¿ï¼<td> è¦ç´ ï¼ãå«ã 3 ã¤ã®è¡ï¼<tr> è¦ç´ ï¼ãé
ç½®ãããã¨ã§ãéé ã§ä¸¦ãã æ°å¤ãããªã 1 ã¤ã®åã使ãã¦ãã¾ãã
<table>
<tbody>
<tr>
<td>3</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>1</td>
</tr>
</tbody>
</table>
JavaScript
ä¸è¨ã® JavaScript ã³ã¼ãã§ã¯ã使ããã sort() 颿°ã <tbody> è¦ç´ ã«åãä»ãã表ã®ã»ã«ãå¤ã®æé ã§ä¸¦ã¹æ¿ããããã«å¿ãã¦è¡¨ç¤ºãæ´æ°ãã¾ãã
HTMLTableSectionElement.prototype.sort = function (cb) {
Array.from(this.rows)
.sort(cb)
.forEach((e) => this.appendChild(this.removeChild(e)));
};
document
.querySelector("table")
.tBodies[0].sort((a, b) => a.textContent.localeCompare(b.textContent));
çµæ
è¦åºãã»ã«ãã¯ãªãã¯ãããã¨ã§è¡ãä¸¦ã¹æ¿ã
ãã®ä¾ã§ã¯ãåã®ä¾ã®åºæ¬çãªè¡¨ãæ¡å¼µããè¤æ°ã®åã«å¯¾ãã¦ã対話çãã¤åå¥ã«ä¸¦ã¹æ¿ããè¡ããããã«ãã¦ãã¾ãã
HTML
追å ã®ãã¼ã¿ã»ã«ï¼<td> è¦ç´ ï¼ãããã¼ãã«æ¬ä½ï¼<tbody> è¦ç´ ï¼å
ã®ããããã®è¡ï¼<tr> è¦ç´ ï¼ã«è¿½å ããã¢ã«ãã¡ãããã®æé ã«ä¸¦ãã 2 ã¤ç®ã¨ãªãåã使ãã¾ãã<thead> è¦ç´ ã使ç¨ããæ¬ä½ã»ã¯ã·ã§ã³ã®åã«ãããã¼ã»ã¯ã·ã§ã³ã追å ãã¦ã表è¦åºãã»ã«ï¼<th> è¦ç´ ï¼ãå«ããããã¼è¡ãå°å
¥ãã¾ãããããã®è¦åºãã»ã«ã¯ãå¾è¿°ã® JavaScript ã³ã¼ãã§ä½¿ç¨ãã¦ãã¯ãªãã¯å¯è½ã«è¨å®ããã¯ãªãã¯ã«ãã£ã¦ã¢ã¯ãã£ãåãããéã«å¯¾å¿ããã½ã¼ããå®è¡ãã¾ãã
<table>
<thead>
<tr>
<th>çªå·</th>
<th>æ°å¤</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>A</td>
</tr>
<tr>
<td>2</td>
<td>B</td>
</tr>
<tr>
<td>1</td>
<td>C</td>
</tr>
</tbody>
</table>
JavaScript
ã¯ãªãã¯ã¤ãã³ããã³ãã©ã¼ããdocument å
ã®å <table> ã®ããããã®è¦åºãè¡ï¼<th> è¦ç´ ï¼ã«è¿½å ãã¾ããããã«ãããè¡ã«å«ã¾ãã¦ãããã¼ã¿ã»ã«ï¼<td> è¦ç´ ï¼ã®å
容ã«åºã¥ãã¦ã<tbody> ã®ãã¹ã¦ã®è¡ï¼<tr> è¦ç´ ï¼ãã½ã¼ãããã¾ãã
ã¡ã¢:
ãã®è§£æ±ºçã§ã¯ã<td> è¦ç´ ã«ã¯åè¦ç´ ãå«ã¾ãªããã¬ã¼ã³ããã¹ããä»®å®ããã¦ãããã¨ãæ³å®ãã¦ãã¾ãã
const allTables = document.querySelectorAll("table");
for (const table of allTables) {
const tBody = table.tBodies[0];
const rows = Array.from(tBody.rows);
const headerCells = table.tHead.rows[0].cells;
for (const th of headerCells) {
const cellIndex = th.cellIndex;
th.addEventListener("click", () => {
rows.sort((tr1, tr2) => {
const tr1Text = tr1.cells[cellIndex].textContent;
const tr2Text = tr2.cells[cellIndex].textContent;
return tr1Text.localeCompare(tr2Text);
});
tBody.append(...rows);
});
}
}
çµæ
ã¡ã¢:
使ãããããã¢ã¯ã»ã·ããªãã£ã確ä¿ããããã«ã¯ãä¸¦ã¹æ¿ãå¯è½ãªããããã®åã®è¦åºãã»ã«ãä¸¦ã¹æ¿ããã¿ã³ã¨ãã¦èå¥å¯è½ã§ããå¿
è¦ããããã¾ãããã®åãç¾å¨æé ã§ä¸¦ã¹æ¿ãããã¦ãããéé ã§ä¸¦ã¹æ¿ãããã¦ãããããè¦è¦çã«ãããã³ aria-sort 屿§ã«ãã£ã¦æç¢ºã«å®ç¾©ããå¿
è¦ãããã¾ããæ
å ±ã«ã¤ãã¦ã¯ãARIA Authoring Practices Guide ã® sortable table example ãã覧ãã ããã
æè¡çæ¦è¦
| ã³ã³ãã³ãã«ãã´ãªã¼ | ãªã |
|---|---|
| 許å¯ããã¦ããå 容 |
0 å以ä¸ã® <td> è¦ç´ ã¾ã㯠<th> è¦ç´ ã ã¹ã¯ãªãã対å¿è¦ç´ (<script> ããã³ <template>) ã許å¯ããã¦ãã¾ãã
|
| ã¿ã°ã®çç¥ |
éå§ã¿ã°ã¯å¿
é ã <tr> è¦ç´ ã®ç´å¾ã« <tr> è¦ç´ ãããå ´åãã¾ãã¯è¦ªã®è¡¨ã°ã«ã¼ãè¦ç´ (<thead>, <tbody>, <tfoot>) å
ã§æå¾ã®è¦ç´ ã§ããå ´åã¯çµäºã¿ã°ãçç¥å¯è½ã
|
| 許å¯ããã¦ãã親è¦ç´ |
<table> è¦ç´ (åã® <tbody> ãæããªãå ´åã§ã <caption>, <colgroup>, <thead> ã®åè¦ç´ ããå¾ã«éã)ã <thead>, <tbody>, <tfoot> ã®åè¦ç´
|
| æé»ã® ARIA ãã¼ã« |
row
|
| 許å¯ããã¦ãã ARIA ãã¼ã« | ãã¹ã¦ |
| DOM ã¤ã³ã¿ã¼ãã§ã¤ã¹ | HTMLTableRowElement |
仿§æ¸
| 仿§æ¸ |
|---|
| HTML > # the-tr-element > |
ãã©ã¦ã¶ã¼ã®äºææ§
é¢é£æ å ±
- å¦ç¿: HTML 表ã®åºæ¬
<tr>ãå«ãã表ã®ä½¿ç¨ã«ã¤ãã¦èª¬æãã¾ãã <caption>,<col>,<colgroup>,<table>,<tbody>,<td>,<tfoot>,<th>,<thead>: ãã®ä»ã®è¡¨é¢é£è¦ç´background-color: è¡ã®åã»ã«ã®èæ¯è²ãè¨å®ãã CSS ããããã£border: è¡ã®ã»ã«ã®å¢çãå¶å¾¡ãã CSS ããããã£text-align: è¡ã®åã»ã«ã®å å®¹ãæ°´å¹³æ¹åã«ä½ç½®ãããããããã® CSS ããããã£vertical-align: è¡ã®åã»ã«ã®å 容ãåç´æ¹åã«ä½ç½®ãããããããã® CSS ããããã£:nth-of-type,:first-of-type,:last-of-type: ç®çã®è¡ã®ã»ã«ã鏿ããããã® CSS æ¬ä¼¼ã¯ã©ã¹