【CSS】display:tableの使い方

「display: table」は、HTML要素を表形式に配置するためのCSSプロパティです。表形式は、行と列に分割し、それらを組み合わせて配置する方式で、データの表示やレイアウトの調整に適しています。

使用方法は以下のようになります。

1.HTML要素に「display: table」を適用する

例:

<div style="display: table;">
  <div style="display: table-row;">
    <div style="display: table-cell;">セル1</div>
    <div style="display: table-cell;">セル2</div>
  </div>
  <div style="display: table-row;">
    <div style="display: table-cell;">セル3</div>
    <div style="display: table-cell;">セル4</div>
  </div>
</div>

2.CSSファイルに「display: table」を適用する

例:

.table {
  display: table;
}

.table-row {
  display: table-row;
}

.table-cell {
  display: table-cell;
}
<div class="table">
  <div class="table-row">
    <div class="table-cell">セル1</div>
    <div class="table-cell">セル2</div>
  </div>
  <div class="table-row">
    <div class="table-cell">セル3</div>
    <div class="table-cell">セル4</div>
  </div>
</div>

これにより、「div」要素がテーブルになり、セルが行と列に分割されるように配置されます。

「display: table」は、テーブルに表示される要素に対して、「border-spacing」や「border-collapse」などのプロパティを使用して、セルや行の間隔や枠線のスタイルを調整することができます。

また、「display: table-row」や「display: table-cell」を使用することで、行やセルに対してスタイルを適用することもできます。

「display: table」は、一般的にテーブルやグリッドを表示するために使用されますが、特定のレイアウトを作成するためにも使用することができます。ただし、レスポンシブデザインなどでは、「display: flex」や「display: grid」を使用することが推奨されるため、使用する場面によっては代替手段を検討する必要があるでしょう。