Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. HTML
  3. Reference
  4. Elements
  5. <col>

<col>: The Table Column element

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

* Some parts of this feature may have varying levels of support.

The<col>HTML element defines one or more columns in a column group represented by its parent<colgroup> element. The<col> element is only valid as a child of a<colgroup> element that has nospan attribute defined.

Try it

<table>  <caption>    Superheros and sidekicks  </caption>  <colgroup>    <col />    <col span="2" />    <col span="2" />  </colgroup>  <thead>    <tr>      <td></td>      <th scope="col">Batman</th>      <th scope="col">Robin</th>      <th scope="col">The Flash</th>      <th scope="col">Kid Flash</th>    </tr>  </thead>  <tbody>    <tr>      <th scope="row">Skill</th>      <td>Smarts, strong</td>      <td>Dex, acrobat</td>      <td>Super speed</td>      <td>Super speed</td>    </tr>  </tbody></table>
.batman {  background-color: #d7d9f2;}.flash {  background-color: #ffe8d4;}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;}th,td {  border: 1px solid rgb(160 160 160);  padding: 8px 6px;}td {  text-align: center;}

Attributes

This element includes theglobal attributes.

span

Specifies the number of consecutive columns the<col> element spans. The value must be a positive integer greater than zero. If not present, its default value is1.

Deprecated attributes

The following attributes are deprecated and should not be used. They are documented below for reference when updating existing code and for historical interest only.

alignDeprecated

Specifies the horizontal alignment of each column cell. The possibleenumerated values areleft,center,right,justify, andchar. When supported, thechar value aligns the textual content on the character defined in thechar attribute and on offset defined by thecharoff attribute. Note that this attribute overrides the specifiedalign of its<colgroup> parent element. Use thetext-align CSS property on the<td> and<th> elements instead, as this attribute is deprecated.

Note:Settingtext-align on the<col> element has no effect as<col> has no descendants, and therefore no elements inherit from it.

If the table does not use acolspan attribute, use thetd:nth-of-type(an+b) CSS selector. Seta to zero andb to the position of the column in the table, e.g.,td:nth-of-type(2) { text-align: right; } to right-align the second column cells.

If the table does use acolspan attribute, the effect can be achieved by combining adequate CSS attribute selectors like[colspan=n], though this is not trivial.

bgcolorDeprecated

Defines the background color of each column cell. The value is an HTML color; either a6-digit hexadecimal RGB code, prefixed by a#, or acolor keyword. Other CSS<color> values are not supported. Use thebackground-color CSS property instead, as this attribute is deprecated.

charDeprecated

Does nothing. It was originally intended to specify the alignment of the content to a character of each column cell. Typical values for this include a period (.) when attempting to align numbers or monetary values. Ifalign is not set tochar, this attribute is ignored, though it will still override the specifiedchar of its<colgroup> parent element.

charoffDeprecated

Does nothing. It was originally intended to specify the number of characters to offset the column cell content from the alignment character specified by thechar attribute.

valignDeprecated

Specifies the vertical alignment of each column cell. The possibleenumerated values arebaseline,bottom,middle, andtop. Note that this attribute overrides the specifiedvalign of its<colgroup> parent element. Use thevertical-align CSS property on the<td> and<th> elements instead, as this attribute is deprecated.

Note:Settingvertical-align on the<col> element has no effect as<col> has no descendants, and therefore no elements inherit from it.

If the table does not use acolspan attribute, use thetd:nth-of-type(an+b) CSS selector. Seta to zero andb to the position of the column in the table, e.g.,td:nth-of-type(2) { vertical-align: middle; } to center the second column cells vertically.

If the table does use acolspan attribute, the effect can be achieved by combining adequate CSS attribute selectors like[colspan=n], though this is not trivial.

widthDeprecated

Specifies a default width for each column. In addition to the standard pixel and percentage values, this attribute can take the special form0*, which means that the width of each column spanned should be the minimum width necessary to hold the column's contents. Relative widths such as5* can also be used. Note that this attribute overrides the specifiedwidth of its<colgroup> parent element. Use thewidth CSS property instead, as this attribute is deprecated.

Usage notes

  • The<col> element is used within a<colgroup> element that doesn't have aspan attribute.
  • The<col> elements do not structurally group columns together. This is the role of the<colgroup> element.
  • Only a limited number of CSS properties affect<col>:
    • background : The variousbackground properties will set the background for cells within the column. As the column background color is painted on top of the table and column groups (<colgroup>), but behind background colors applied to the row groups (<thead>,<tbody>, and<tfoot>), the rows (<tr>), and the individual cells (<th> and<td>), backgrounds applied to table columns are only visible if every layer painted on top of them has a transparent background.
    • border: The variousborder properties apply, but only if the<table> hasborder-collapse: collapse set.
    • visibility: The valuecollapse for a column results in all cells of that column not being rendered, and cells spanning into other columns being clipped. The space these columns would have occupied is removed. However, the size of other columns is still calculated as though the cells in the collapsed column(s) are present. Other values forvisibility have no effect.
    • width: Thewidth property defines a minimum width for the column, as ifmin-width were set.

Example

See<table> for a complete table example introducing common standards and best practices.

This example demonstrates an eight-column table divided into three<col> elements.

HTML

A<colgroup> element provides structures to a basic table, creating a single implicit column group. Three<col> elements are included within the<colgroup>, creating three stylable columns. Thespan attribute specifies the number of table columns each<col> should span (defaulting to1 when omitted), enabling attributes to be shared across the columns in each<col>.

html
<table>  <caption>    Personal weekly activities  </caption>  <colgroup>    <col />    <col span="5" />    <col span="2" />  </colgroup>  <thead>    <tr>      <th>Period</th>      <th>Mon</th>      <th>Tue</th>      <th>Wed</th>      <th>Thu</th>      <th>Fri</th>      <th>Sat</th>      <th>Sun</th>    </tr>  </thead>  <tbody>    <tr>      <th>a.m.</th>      <td>Clean room</td>      <td>Football training</td>      <td>Dance Course</td>      <td>History Class</td>      <td>Buy drinks</td>      <td>Study hour</td>      <td>Free time</td>    </tr>    <tr>      <th>p.m.</th>      <td>Yoga</td>      <td>Chess Club</td>      <td>Meet friends</td>      <td>Gymnastics</td>      <td>Birthday party</td>      <td>Fishing trip</td>      <td>Free time</td>    </tr>  </tbody></table>

CSS

We use CSS, instead of deprecated HTML attributes, to provide a background color to the columns and align the cell content:

css
table {  border-collapse: collapse;  border: 2px solid rgb(140 140 140);}caption {  caption-side: bottom;  padding: 10px;}th,td {  border: 1px solid rgb(160 160 160);  padding: 8px 6px;  text-align: center;}.weekdays {  background-color: #d7d9f2;}.weekend {  background-color: #ffe8d4;}
table {  font-family: sans-serif;  font-size: 0.8rem;  letter-spacing: 1px;}

Result

Technical summary

Content categoriesNone.
Permitted contentNone; it is avoid element.
Tag omissionMust have a start tag and must not have an end tag.
Permitted parents<colgroup> only, though it can be implicitly defined as its start tag is not mandatory. The<colgroup> must not have aspan attribute.
Implicit ARIA roleNo corresponding role
Permitted ARIA rolesNorole permitted
DOM interfaceHTMLTableColElement

Specifications

Specification
HTML
# the-col-element

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp