0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【CSS】背景画像の設定

Posted at

background-image

:sunny: 背景画像を指定する。
:sunny: background-image: 画像のURL;

background-color で背景色は設定できたけど、それだけではなんだか味気ない・・・:sweat:
というときは背景画像を入れてみよう:smile:

例.html
<div>
  <p>海はいいねぇ</p>
</div>
例.css
div {
  color: white;
  width: 100%;
  height: 500px;
  background-image: url(https://www.pakutaso.com/shared/img/thumb/RED19515A006_TP_V.jpg);
}

一番下に background-image: 画像のURL; を入れてみました:smile:

Web

スクリーンショット 2019-12-28 3.17.49.png

画像の左上に文字と写真が入りました:raised_hands:
文字で記入したとおり
海はいいねぇ〜:relaxed:
・・・とおもいきや!
海ではなく空しか写っていない!!:scream:

原因は私が高さを height: 500px; にしているため入り切らない画像は切り取られてしまったんだと思います:sob:

かといって高さは変えたくない:disappointed_relieved:

そんなときは・・・!:point_up::bulb:
background-size: cover; を加えてみよう:smile:

例.css
div {
  color: white;
  width: 100%;
  height: 500px;
  background-image: url(https://www.pakutaso.com/shared/img/thumb/RED19515A006_TP_V.jpg);
  background-size: cover;
}

Web

スクリーンショット 2019-12-28 3.27.48.png

表示されました!:smile:

:sunny: background-size: cover;・・・画像をぴったりサイズに表示してくれる

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?