複数の背景プロパティを組み合わせる
このステップでは、より複雑で興味深い背景デザインを作成するために、複数の背景プロパティをどのように組み合わせるかを学びます。高度な背景スタイリング技術を示すために、~/project
ディレクトリ内のstyles.html
ファイルを変更します。
styles.html
ファイルを以下の内容で更新します。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple Background Properties</title>
<style>
.image-container {
width: 600px;
height: 400px;
border: 2px solid black;
margin: 20px;
color: white;
font-weight: bold;
padding: 20px;
}
.multiple-backgrounds {
background-image:
linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
url("background-sample.jpg");
background-size: cover, cover;
background-position: center, center;
background-repeat: no-repeat, no-repeat;
}
.layered-backgrounds {
background-image:
url("small-pattern.png"), linear-gradient(to right, #ff6b6b, #4ecdc4);
background-size:
100px 100px,
cover;
background-position:
top left,
center;
background-repeat: repeat, no-repeat;
}
</style>
</head>
<body>
<div class="image-container multiple-backgrounds">
Overlay Background with Image
</div>
<div class="image-container layered-backgrounds">
Layered Background with Pattern and Gradient
</div>
</body>
</html>
まず、小さなパターン画像をダウンロードします。
wget https://labex.io/courses/images/small-pattern.png -O ~/project/small-pattern.png
この例では、2つの高度な背景技術を示しています。
-
オーバーレイ付きの複数の背景:
- 半透明なオーバーレイを作成するために線形グラデーションを使用
- グラデーションと背景画像を組み合わせる
- 一貫したサイズ指定、位置指定、繰り返しを適用
-
重ね合わせた背景:
- 繰り返しパターン画像と線形グラデーションを組み合わせる
- 各背景層に異なるサイズ指定と位置指定を使用する
このHTMLファイルをブラウザで開くと、複数の背景プロパティがどのように複雑で視覚的に興味深いデザインを作成できるかがわかります。