段落にパディングを適用する
このステップでは、要素のコンテンツとそのボーダーの間にスペースを作成するCSSプロパティであるパディングについて学びます。パディングは、要素の読みやすさと視覚的な間隔を改善するのに役立ちます。
styles.html
ファイルの<style>
セクションを次のCSSで更新します。
<style>
p {
border: 4px dashed green;
border-radius: 10px;
padding: 20px;
}
</style>
パディングを適用するさまざまな方法を見てみましょう。
- 均等なパディング(すべての辺):
padding: 20px; /* すべての辺に20pxのパディング */
- 個々の辺のパディング:
padding-top: 10px;
padding-right: 20px;
padding-bottom: 15px;
padding-left: 25px;
- 異なる値を持つショートハンド:
padding: 10px 20px 15px 25px;
/* 上: 10px, 右: 20px, 下: 15px, 左: 25px */
- 水平と垂直のパディング:
padding: 15px 30px;
/* 上/下: 15px, 左/右: 30px */
例の出力:
HTMLファイルを開くと、次のような段落が表示されます。
- 緑の点線のボーダー
- 丸い角
- すべての辺に20ピクセルのパディングで、テキストとボーダーの間にスペースが作成されている
パディングの可視化:
+---------------------------+
| |
| [20px padding] |
| Welcome to CSS |
| Styling! |
| [20px padding] |
| |
+---------------------------+