docs.rodeo

MDN Web Docs mirror

: The Table Column element

{{HTMLSidebar}} 

The <col> HTML element defines one or more columns in a column group represented by its parent {{HTMLElement("colgroup")}}  element. The <col> element is only valid as a child of a {{HTMLElement("colgroup")}}  element that has no span attribute defined.

{{EmbedInteractiveExample("pages/tabbed/col.html","tabbed-taller")}} 

Attributes

This element includes the global attributes.

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.

Usage notes

Example

See {{HTMLElement("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 {{HTMLElement("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. The span attribute specifies the number of table columns each <col> should span (defaulting to 1 when omitted), enabling attributes to be shared across the columns in each <col>.

<table>
  <caption>
    Personal weekly activities
  </caption>
  <colgroup>
    <col />
    <col span="5" class="weekdays" />
    <col span="2" class="weekend" />
  </colgroup>
  <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>
  <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>
</table>

CSS

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

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

{{EmbedLiveSample('Example', 650, 170)}} 

Technical summary

Content categories None.
Permitted content None; it is a `{{Glossary("void element")}}` .
Tag omission Must have a start tag and must not have an end tag.
Permitted parents `{{HTMLElement("colgroup")}}`  only, though it can be implicitly defined as its start tag is not mandatory. The `{{HTMLElement("colgroup")}}`  must not have a span attribute.
Implicit ARIA role No corresponding role
Permitted ARIA roles No role permitted
DOM interface `{{domxref("HTMLTableColElement")}}` 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN