このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

HTMLTableCellElement: colSpan プロパティ

Baseline 広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

colSpanHTMLTableCellElement インターフェイスのプロパティで、このセルがまたがる列の数を表します。これにより、セルは表の複数の列にまたがって空間を占めることができるようになります。これは colspan 属性に対応しています。

列の数を表す正の数値です。

メモ: 新しい値を設定する際、その値は最も近い正の数値に丸められます。

この例では、本文の最初のセルがまたがる列の数を変更するための 2 つのボタンが提供されています。

HTML

html
<table>
  <thead>
    <tr>
      <th>列 1</th>
      <th>列 2</th>
      <th>列 3</th>
      <th>列 4</th>
      <th>列 5</th>
      <th>列 6</th>
      <th>列 7</th>
      <th>列 8</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="2">1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
    </tr>
  </tbody>
</table>
<button id="increase">colspan を増やす</button>
<button id="decrease">colspan を減らす</button>
<div>最初のセルは <output>2</output> 列にまたがります。</div>

JavaScript

js
// 要素に関連するインターフェイスを取得する
const cell = document.querySelectorAll("tbody tr td")[0];
const output = document.querySelectorAll("output")[0];

const increaseButton = document.getElementById("increase");
const decreaseButton = document.getElementById("decrease");

increaseButton.addEventListener("click", () => {
  cell.colSpan += 1;

  // 表示を更新
  output.textContent = cell.colSpan;
});

decreaseButton.addEventListener("click", () => {
  cell.colSpan -= 1;

  // 表示を更新
  output.textContent = cell.colSpan;
});

結果

仕様書

仕様書
HTML
# dom-tdth-colspan

ブラウザーの互換性

関連情報