CSS Web制作

【CSS】hover(マウスオーバー)で画像を拡大する方法

※ 当サイトではアフィリエイト広告を利用しています

hoverしたら画像を拡大するにはどうすればいい?

hoverしたら画像を拡大させるのは実務でも非常に良くある実装です。

画像を表示させるには『imgタグ』と『background-image』の2種類の方法があります。

今回は両方のケースで画像を拡大する方法を解説していきます。

 

完全無料のプログラミングスクール『ZeroPlus Gate』
30日間でWeb制作を学べる無料のプログラミングスクールがこちら
  • 30日間でWeb制作を学べる
  • 完全無料
  • 現役エンジニアへの質問無制限
  • オンラインの動画学習なので時間場所を問わず勉強可能
  • 最大4回の学習サポート面談
  • 受付は1日25名までの先着制
無料なのに専属のメンターが付き、現役エンジニアへの質問も無制限という破格のサービスです。
いきなり数十万するプログラミングスクールは厳しい・・・という人のお試しに最適。
現在は無料ですがいつ有料になるか分からないので、気になる方はお早めに👇

\ 完全無料 /

ZeroPlus Gate公式サイト

先着1日25名まで!

hover(マウスオーバー)で画像を拡大する方法:imgタグの場合

まずは『imgタグ』を使った場合です。

コードを見ていきます。

<div class="sample-img">
  <img src="画像パス" alt="">
</div>
.sample-img {
  cursor: pointer;
  max-width: 500px;
  overflow: hidden;
  width: 100%;
}
.sample-img img {
  height: auto;
  transition: transform .6s ease; /* ゆっくり変化させる */
}
.sample-img:hover img {
  transform: scale(1.1); /* 拡大 */
}

transform: scale(1.1);で画像が拡大しています。

これは数字が大きいほど拡大し、1.0未満だと縮小します。

ポイントはoverflowで、これがないと画像が親要素からはみ出てしまいます。

こちらがサンプルになります。

See the Pen
hoverで画像拡大
by junpei (@junpei-sugiyama)
on CodePen.

 

overflowが無いと画像がはみ出る

ちなみにoverflowが無いとこうなります。

See the Pen
hoverで画像拡大(overflowなし)
by junpei (@junpei-sugiyama)
on CodePen.

拡大はしますが・・・枠ごとはみ出てしまいます。

 

画像の上に文字がある場合

画像の上に文字がある場合はこのようになります。

<div class="sample-img">
  <img src="画像パス" alt="">
  <p>テキスト</p>
</div>
.sample-img {
  cursor: pointer;
  max-width: 500px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.sample-img img {
  height: auto;
  transition: transform .6s ease; /* ゆっくり変化させる */
}
.sample-img:hover img {
  transform: scale(1.1); /* 拡大 */
}
.sample-img p {
  align-items: center; /* テキストの中央揃え */
  bottom: 0;
  color: #fff; /* テキストの色 */
  display: flex; /* テキストの中央揃え */
  justify-content: center; /* テキストの中央揃え */
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 80%; /* テキストを横幅いっぱいにならないようにする */
}

See the Pen
Untitled
by junpei (@junpei-sugiyama)
on CodePen.

 

画像だけに黒いマスクをかける

さらに画像だけ黒いマスクをかけたい場合はこちらになります。

<div class="sample-img">
  <img src="画像パス" alt="">
  <p>テキスト</p>
</div>
.sample-img {
  cursor: pointer;
  max-width: 500px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.sample-img img {
  height: auto;
  transition: transform .6s ease; /* ゆっくり変化させる */
}
.sample-img:hover img {
  transform: scale(1.1); /* 拡大 */
}
.sample-img p {
  align-items: center; /* テキストの中央揃え */
  bottom: 0;
  color: #fff; /* テキストの色 */
  display: flex; /* テキストの中央揃え */
  justify-content: center; /* テキストの中央揃え */
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 80%; /* テキストを横幅いっぱいにならないようにする */
  z-index: 2;
}
/* マスク */
.sample-img::before {
  background: rgba(0, 0, 0, .5); /* マスクの色(黒の50%) */
  bottom: 0;
  content: '';
  height: auto;
  left: 0;
  opacity: 0; /* 最初は透明(非表示) */
  position: absolute;
  right: 0;
  top: 0;
  transition: opacity .6s ease; /* ゆっくりopacityのみへ変化させる */
  width: 100%;
  z-index: 1;
}
.sample-img:hover::before {
  opacity: 1;
}

See the Pen
hoverで画像拡大(テキスト、マスクあり)
by junpei (@junpei-sugiyama)
on CodePen.

これはCSS】画像をhoverしたら透過させた黒いマスクをかけて文字を表示させる方法の応用になりますが、

テキストとマスクにz-indexを書かないとマスクが反映されなかったり、数値を間違えるとテキストの上にマスクがきてテキストも暗くなるのでご注意下さい。

hover(マウスオーバー)で画像を拡大する方法:background-imageの場合

次は『background-image』を使った場合です。

<div class="box">
  <div class="box-bg"></div>
</div>
.box {
  cursor: pointer;
  max-width: 500px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.box-bg {
  background-image: url(画像パス);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 350px;
  transition: transform .6s ease;
  width: 100%;
}
.box:hover .box-bg {
  transform: scale(1.1);
}

See the Pen
hoverでbackground-imageを拡大
by junpei (@junpei-sugiyama)
on CodePen.

今度は『imgタグ』ではなく背景画像の『background-image』で画像を表示しています。

背景画像は.box-bgに表示させます。

あとは基本的な所は同じかと思います。

 

画像の上に文字がある場合

画像の上に文字がある場合はこのようになります。

<div class="box">
  <p>テキスト</p>
  <div class="box-bg"></div>
</div>
.box {
  cursor: pointer;
  max-width: 500px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.box-bg {
  background-image: url(画像パス);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 350px;
  transition: transform .6s ease;
  width: 100%;
}
.box:hover .box-bg {
  transform: scale(1.1);
}
.box p {
  align-items: center; /* テキストの中央揃え */
  bottom: 0;
  color: #fff; /* テキストの色 */
  display: flex; /* テキストの中央揃え */
  justify-content: center; /* テキストの中央揃え */
  left: 0;
  margin: auto;
  pointer-events: none;
  position: absolute;
  right: 0;
  top: 0;
  width: 80%; /* テキストを横幅いっぱいにならないようにする */
  z-index: 1;
}

See the Pen
hoverでbackground-imageを拡大(テキストあり)
by junpei (@junpei-sugiyama)
on CodePen.

 

画像だけに黒いマスクをかける

さらに画像だけ黒いマスクをかけたい場合はこちらになります。

<div class="box">
  <p>テキスト</p>
  <div class="box-bg"></div>
</div>
.box {
  cursor: pointer;
  max-width: 500px;
  overflow: hidden;
  position: relative;
  width: 100%;
}
.box-bg {
  background-image: url(画像パス);
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 350px;
  transition: transform .6s ease;
  width: 100%;
}
.box:hover .box-bg {
  transform: scale(1.1);
}
.box p {
  align-items: center; /* テキストの中央揃え */
  bottom: 0;
  color: #fff; /* テキストの色 */
  display: flex; /* テキストの中央揃え */
  justify-content: center; /* テキストの中央揃え */
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 80%; /* テキストを横幅いっぱいにならないようにする */
  z-index: 2;
}
/* マスク */
.box::before {
  background: rgba(0, 0, 0, .5); /* マスクの色(黒の50%) */
  bottom: 0;
  content: '';
  height: auto;
  left: 0;
  opacity: 0; /* 最初は透明(非表示) */
  position: absolute;
  right: 0;
  top: 0;
  transition: opacity .6s ease; /* ゆっくりopacityのみへ変化させる */
  width: 100%;
  z-index: 1;
}
.box:hover::before {
  opacity: 1;
}

See the Pen
hoverでbackground-imageを拡大(テキスト&マスクあり)
by junpei (@junpei-sugiyama)
on CodePen.

ポイントは以下になります。

  • overflowで親要素からはみ出させないようにする
  • z-indexでマスクとテキストの順番を指定する



まとめ

今回はhover(マウスオーバー)で画像を拡大する方法を解説しました。

実務でもよくある実装だと思うので、ブクマしていつでもコピペ出来るようにしておくと便利かと思います。

以上になります。

この記事が役に立ったと思ったら、シェアボタンからX(旧Twitter)などにシェアすると、いいねされてフォロワーが増えたりすることがあるよ!

 

Web制作おすすめ教材と案件獲得サービス
当ブログではWeb制作学習におすすめの教材を厳選してご紹介しています。
こちらの記事を参考に教材を購入して成果を出している人もたくさんいるので、自分に必要な教材を探してみて下さい。
また、学習面だけでなく営業面である案件獲得サービスもご紹介しています。
スキルが身についても仕事がなければ意味がないので、営業に不安がある人はこちらの記事をぜひ参考にしてみて下さい。
この記事を参考にスキルと営業力を身につけて稼げるようになりましょう!

-CSS, Web制作