マージンとパディングの基本
メニューを表示するにはスワイプしてください
すべての要素にはコンテンツ、パディング、ボーダー、マージンがあることはご存知でしょう。これらのプロパティを使ってスペースをどのように制御するかを見ていきます。
パディング
パディングは、要素の内側、コンテンツとボーダーの間にスペースを作ります。
.box {
padding: 20px;
}
これにより、四辺すべてに20pxのパディングが適用されます。
パディングのショートハンド
パディングはショートハンド構文で記述できます。
値の順序は常に「上 → 右 → 下 → 左」です。
padding: 20px; /* all sides */
padding: 10px 20px; /* top/bottom | left/right */
padding: 10px 20px 5px; /* top | left/right | bottom */
padding: 10px 15px 20px 25px; /* top | right | bottom | left */
個別のパディングプロパティ
各辺を個別に制御することも可能:
padding-top: 10px;
padding-right: 15px;
padding-bottom: 25px;
padding-left: 5px;
マージン
マージンは要素の外側にスペースを作り、他の要素との間隔を確保。
.box {
margin: 20px;
}
マージンのショートハンド
マージンはパディングと同じショートハンドルールに従う:
margin: 20px; /* all sides */
margin: 10px 20px; /* top/bottom | left/right */
margin: 10px 20px 5px; /* top | left/right | bottom */
margin: 10px 15px 20px 25px; /* top | right | bottom | left */
個別のマージンプロパティ
個別に設定することも可能:
margin-top: 10px;
margin-right: 15px;
margin-bottom: 25px;
margin-left: 5px;
ボーダー
ボーダーはパディングとマージンの間に位置。
基本的なショートハンド:
border: 4px solid brown;
この指定で幅、スタイル、色を定義。
一般的なボーダースタイルには、solid、dotted、dashed、double があります。
必要に応じて各辺を個別に指定することもできます:
border-top: 2px solid black;
次の例を見て、考えられるボーダースタイルを確認しましょう:
index.html
styles.css
出力
主な違い
パディング:
- 要素内側の余白を追加;
- 背景領域が拡張される。
マージン:
- 要素外側の余白を追加;
- 要素の背景には影響しない。
1. 次の宣言で 40px が適用されるのはどの辺ですか?
2. 要素の内部、コンテンツと境界線の間にスペースを追加するプロパティはどれですか?
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 2
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 3. 章 2