0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CSSのあれこれ

Posted at

ブロック要素、インライン要素、インラインブロック要素について

ブロック要素

幅:親要素いっぱいに広がる(width: 100%が基本)

改行:前後に必ず改行される(縦並びになる)

サイズ指定:width・height 可能

余白:margin・padding は上下左右すべて有効

用途:段落・見出し・区切りなど、大枠の構造

(例)
<div>, <p>, <h1><h6>, <ul>, <ol>, <section>, <article>, <header>, <footer>

インライン要素

幅:中身のコンテンツ分だけ

改行:前後に改行しない(横に並ぶ)

サイズ指定:width・height は基本無視される

余白:margin は左右のみ有効(上下は反映されにくい)

用途:文章の一部(強調、リンク、アイコンなど)

(例)
<span>, <a>, <strong>, <em>, <b>, <i>, <u>, <abbr>, <label>

インラインブロック要素

幅:中身のコンテンツ分だけ(width 指定可能)

改行:前後に改行しない(横に並ぶ)

サイズ指定:width・height 可能

余白:margin・padding 上下左右すべて有効

用途:横並びにしつつサイズを調整したい場合(ボタン、カード、アイコンなど)

(例)
<img>, <button>, <input>, CSSで display: inline-block; にした要素

要素のセンター寄せについて

テキストやインライン要素を中央に寄せる

.parent {
  text-align: center;
}

適用対象:テキスト、inline / inline-block / inline-flex 要素
親要素に設定する必要あり。

ブロック要素を中央に寄せる

.child {
  margin-left: auto;
  margin-right: auto;
  width: 200px; /* 幅必須 */
}

適用対象:block 要素
widthを指定しないと左右 auto が効かない。

Flexboxで中央寄せ(横方向)

.parent 
  display: flex;
  justify-content: center;
}

幅やテキスト種別を問わず使える。

position + transform

.parent {
  position: relative;
  height: 300px;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

固定サイズでも可変サイズでも対応できる。

Gridでの中央寄せ(縦横両方)

.parent {
  display: grid;
  place-items: center;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?