こんにちは。KOUKIです。
本記事は、Udemyの「50 Projects In 50 Days – HTML, CSS & JavaScript」で学習したことを載せています。
<目次>
実装するもの
今回は、要素の拡大・縮小のスタイリング方法について学びます。
demoは、「こちら」で確認できます。
JavaScript編
HTML&JavaScrript
HTMLとJavaScriptのソースコードを貼り付けておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <title>Expanding Cards</title> </head> <body> <div class="container"> <div class="panel active" style="background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')"> <h3>Explore The World</h3> </div> <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')"> <h3>Wild Forest</h3> </div> <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80')"> <h3>Sunny Beach</h3> </div> <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80')"> <h3>City on Winter</h3> </div> <div class="panel" style="background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80')"> <h3>Mountains - Clouds</h3> </div> <script src="script.js"></script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// 要素の取得 const panels = document.querySelectorAll('.panel') panels.forEach(panel => { panel.addEventListener('click', () =>{ // activeクラスを削除する removeActiveClasses() panel.classList.add('active') }) }) function removeActiveClasses() { panels.forEach(panel => { // activeクラスを削除する(ある場合) panel.classList.remove('active') }) } |
スタイルの実装
ページのスタイルを実装していきましょう。
ボックスサイズの指定
1 2 3 4 5 |
@import url("https://fonts.googleapis.com/css?family=Muli&display=swap"); * { box-sizing: border-box; } |
box-sizingは、divなどのボックス要素のサイズを算出するために使用します。
一つのボックスには、以下の要素がそれぞれ存在します。

「border-box」は、パディング(padding)とボーダー(border)を幅(width)と高さ(height)に含める設定です。

要素を中央寄せにする
現在は、画像がページの上部から横幅一杯に表示されています。
これを中央寄せにしたいので、bodyに次の設定を追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
body { font-family: "Roboto", sans-serif; /* body内の要素をフレックスコンテナにする こうすることで、自由に要素を動かせる */ display: flex; /* ビューポート(画面サイズ)の高さに対する割合。 100vhは画面の高さと同じ(100%)を表す */ height: 100vh; /* フレックスコンテナを 積み重なるように配置する */ flex-direction: column; /* 左右でみてアイテムを中央付近にまとめる */ align-items: center; /* アイテムをページ中央に寄せる */ justify-content: center; /* ボックスからはみ出た要素を非表示にする */ overflow: hidden; /* マージンを0 */ margin: 0; } |

コンテナのスタイル
1 2 3 4 5 6 |
.container { /* コンテナの中でさらにフレックスに */ display: flex; /* ビューポートの幅に対する割合 */ width: 90vw; } |

パネルのスタイル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
.panel { /* 縦横比は保持して、背景領域を完全に覆う最小サイズになるように 背景画像を拡大縮小する */ background-size: cover; /* 背景画像を中央にする */ background-position: center; /* 背景画像を一回だけ表示して繰り返さない */ background-repeat: no-repeat; /* ビューポート(画面サイズ)の高さに対する割合。 80vhは画面の高さと同じ(80%)を表す */ height: 80vh; /* 枠を丸くする */ border-radius: 50px; /* テキスト文字を白 */ color: #fff; /* カーソルをポインターへ */ cursor: pointer; /* フレックスアイテムの表示割合 拡大したい要素はこの数値を変更する */ flex: 0.5; margin: 10px; /* 要素の配置方法 relativeを指定すると配置の起点になる */ position: relative; /* flexスタイルに変化が会った時、0.7sかけて 変化する。 ease-inは開始時は緩やかに変化、 終了に近づくと早く変化する */ transition: flex 0.7s ease-in; } |

flexが肝です。今は全ての要素の表示割合が0.5に設定されていますが、この数値を変更すると要素の拡大ができます。
テキストのスタイル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
.panel h3 { /* 文字の大きさ */ font-size: 24px; /* 要素の位置指定 起点はrelativeが設定されている要素 */ position: absolute; /* 下からの配置 */ bottom: 20px; /* 左からの配置 */ left: 20px; margin: 0; /* 文字の透過性100% */ opacity: 0; } |
opacityを0に指定したので、テキストは消えてますね^^;

要素の拡大
1 2 3 4 |
.panel.active { /* 表示割合を増やす */ flex: 5; } |
これで、panelクラス+activeクラスが付与された要素が拡大して表示されます。

テキストの表示
先ほど、opacityで非表示にしたテキストを表示させます。
1 2 3 4 5 6 |
.panel.active h3 { opacity: 1; /* opacityに対するアニメーション */ transition: opacity 0.3s ease-in 0.4s; } |

transitionを設定しているので、文字もフワッとした感じで表示されます。
おわりに
cssを学習し始めました^^
最初は知らないプロパティばかりで大変ですが、徐々に意味を覚えていくと思います。それに、楽しいですからね。
それでは、また!
CSSまとめ
CSSソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
@import url("https://fonts.googleapis.com/css?family=Muli&display=swap"); * { box-sizing: border-box; } body { font-family: "Roboto", sans-serif; /* body内の要素をフレックスコンテナにする こうすることで、自由に要素を動かせる */ display: flex; /* ビューポート(画面サイズ)の高さに対する割合。 100vhは画面の高さと同じ(100%)を表す */ height: 100vh; /* フレックスコンテナを 積み重なるように配置する */ flex-direction: column; /* 左右でみてアイテムを中央付近にまとめる */ align-items: center; /* アイテムをページ中央に寄せる */ justify-content: center; /* ボックスからはみ出た要素を非表示にする */ overflow: hidden; /* マージンを0 */ margin: 0; } .container { /* コンテナの中でさらにフレックスに */ display: flex; /* ビューポートの幅に対する割合 */ width: 90vw; } .panel { /* 縦横比は保持して、背景領域を 完全に覆う最小サイズになるように 背景画像を拡大縮小する */ background-size: cover; /* 背景画像を中央にする */ background-position: center; /* 背景画像を一回だけ表示して繰り返さない */ background-repeat: no-repeat; /* ビューポート(画面サイズ)の高さに対する割合。 80vhは画面の高さと同じ(80%)を表す */ height: 80vh; /* 枠を丸くする */ border-radius: 50px; /* テキスト文字を白 */ color: #fff; /* カーソルをポインターへ */ cursor: pointer; /* フレックスアイテムの表示割合 拡大したい要素はこの数値を変更する */ flex: 0.5; margin: 10px; /* 要素の配置方法 relativeを指定すると配置の起点になる */ position: relative; /* flexスタイルに変化が会った時、0.7sかけて 変化する。 ease-inは開始時は緩やかに変化、 終了に近づくと早く変化する */ transition: flex 0.7s ease-in; } .panel h3 { /* 文字の大きさ */ font-size: 24px; /* 要素の位置指定 起点はrelativeが設定されている要素 */ position: absolute; /* 下からの配置 */ bottom: 20px; /* 左からの配置 */ left: 20px; margin: 0; /* 文字の透過性100% */ opacity: 0; } .panel.active { /* 表示割合を増やす */ flex: 5; } .panel.active h3 { opacity: 1; /* opacityに対するアニメーション */ transition: opacity 0.3s ease-in 0.4s; } |
コメントを残す
コメントを投稿するにはログインしてください。