Quantcast
Channel: CSSタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8586

display: table; の使い方

$
0
0

使い方

横に並べたい要素にdisplay: table-cell;を指定し、その要素を囲む親要素にdisplay: table;を指定する。
display: table;はHTMLのtable要素を指定したテーブルレイアウトをやっていることは同じ。それをCSSだけで実現できる。
高さを揃えた横並びのレイアウトや、縦方向への中央揃えが簡単にできるようになる。

<divclass="parent"><divclass="child">aaa</div><divclass="child">bbb</div><divclass="child">ccc</div></div>
.parent{display:table;width:100%;border:2pxsolidred;margin-top:10px;}.child{display:table-cell;border:1pxsolidblue;}

スクリーンショット 2019-11-17 9.53.27.png

display: table;を指定したした要素はそのままだとこ要素分しか広がらないので、画面横幅いっぱいに広げるためにwidth: 100%;を指定している。

  • width: 100%;を指定しなかった場合
    スクリーンショット 2019-11-17 9.56.49.png

display: table-cell;で指定した子要素どうしに間隔をあけたい場合

子要素にmarginを指定するのでなく、親要素にborder-collapse: separate;,border-spacing:プロパティを指定する。

.parent{display:table;width:100%;border:2pxsolidred;margin-top:10px;border-collapse:separate;border-spacing:10px5px;}

スクリーンショット 2019-11-17 10.06.46.png


Viewing all articles
Browse latest Browse all 8586

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>