前端背景圖片添加方法「html怎麼用css插入背景圖片」

背景圖像可能是我們所有前端開發人員在我們的職業生涯中至少使用過幾次的CSS屬性之一。大多數人認為背景圖像不可能有任何不尋常的地方,但經過研究,答案並非如此。所以本文收集了七個我認為最有用的技巧,並創建了一些代碼示例。

1.背景圖如何才能完美適配視口

讓背景圖適配視口很容易,需要使用下面 CSS 即可:

body {
  background-image: url('https://images.unsplash.com/photo-1573480813647-552e9b7b5394?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2253&q=80');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
}
7個關於 CSS backgroundImage 好用的技巧

2.如何在CSS中使用多個背景圖片?

如果我想在背景中添加一張以上的圖片怎麼辦?CSS3 中可以直接 指定多個背景路徑,如下所示:

body {
  background-image: url(https://image.flaticon.com/icons/svg/748/748122.svg), url(https://images.unsplash.com/photo-1478719059408-592965723cbc?ixlib=rb-1.2.1&auto=format&fit=crop&w=2212&q=80);
  background-position: center, top;
  background-repeat: repeat, no-repeat;
  background-size: contain, cover;
}
7個關於 CSS backgroundImage 好用的技巧

3.如何創建一個三角形的背景圖像

另一個很酷的背景特效就是三角形背景,當我們想展示某些完全不同的選擇(例如白天和黑夜或冬天和夏天)時,這種特效就更加棒。

思路是這樣的,首先創建兩個div,然後將兩個背景都添加到其中,然後,第二個div使用clip-path屬性畫出三角形。

7個關於 CSS backgroundImage 好用的技巧
<body>
  <div class="day"></div>
  <div class="night"></div>
</body>
body {
  margin: 0;
  padding: 0;
}
div {
position: absolute;
height: 100vh;
width: 100vw;
}
.day {
background-image: url("images.unsplash.com/photo-14779…");
background-size: cover;
background-repeat: no-repeat;
}
.night {
background-image: url("images.unsplash.com/photo-14935…");
background-size: cover;
background-repeat: no-repeat;
clip-path: polygon(100vw 0, 0% 0vh, 100vw 100vh);
}

4.如何在背景圖像上添加疊加漸變?

有時我們想在背景上添加一些文字,但有的圖片太亮,導致字看不清楚,所以這裡我們就需要讓背景圖疊加一些暗樂來突出文字效果。

例如,可以通過添加粉紅橙色漸變或紅色至透明漸變來增強日落圖像,這些情況下使用疊加的漸變就很容易做到。

7個關於 CSS backgroundImage 好用的技巧
body {
  background-image: 
    linear-gradient(4deg, rgba(38,8,31,0.75) 30%, rgba(213,49,127,0.3) 45%, rgba(232,120,12,0.3) 100%),
    url("https://images.unsplash.com/photo-1503803548695-c2a7b4a5b875?ixlib=rb-1.2.1&auto=format&fit=crop&w=2250&q=80");
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center
}

5.如何創建一個顏色動態變化的背景

如果你很多顏色,你想確認哪種顏色更適合背景圖片的顏色,剛動態更改背景顏色的技巧就很有用。

@keyframes background-overlay-animation {
  0%   {
      background-image: 
        linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%), url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
  }
  25%  {
      background-image: 
         linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
  }
  50%  {
    background-image: 
       linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%),
     url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
  }
  100% {
    background-image: 
        linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%),
        url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
  }
}
@-webkit-keyframes background-overlay-animation {
0%   {
background-image:
linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%)
url("images.unsplash.com/photo-15593…");
}
25%  {
background-image:
linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%),
url("images.unsplash.com/photo-15593…");
}
50%  {
background-image:
linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%),
url("images.unsplash.com/photo-15593…");
}
100% {
background-image:
linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%),
7個關於 CSS backgroundImage 好用的技巧

6. 如何製作網格背景圖像?

有時候會遇到一些需要有藝術或者攝影類的項目,他們一般要求網站要有藝術信息,要有創意。網絡的背景就挺有創意的,效果如下:

7個關於 CSS backgroundImage 好用的技巧
body {
 margin: 0;
  padding: 0;
}
.container {
position: absolute;
width: 100%;
height: 100%;
background: black;
display: grid;
grid-template-columns: 25fr 30fr 40fr 15fr;
grid-template-rows: 20fr 45fr 5fr 30fr;
grid-gap: 20px;
.item_img {
background-image: url('images.unsplash.com/photo-14998…');
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: cover;
}
}
body {
 margin: 0;
  padding: 0;
}
.container {
position: absolute;
width: 100%;
height: 100%;
background: black;
display: grid;
grid-template-columns: 25fr 30fr 40fr 15fr;
grid-template-rows: 20fr 45fr 5fr 30fr;
grid-gap: 20px;
.item_img {
background-image: url('images.unsplash.com/photo-14998…');
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: cover;
}
}

7.如何將背景圖像設置為文本顏色?

使用background-image與background-clip,可以實現背景圖像對文字的優美效果。 在某些情況下,它可能非常有用,尤其是當我們想創建一個較大的文本標題而又不如普通顏色那麼枯燥的情況。

7個關於 CSS backgroundImage 好用的技巧
<body>
  <h1>Hello world!</h1>
</body>
body {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  text-align: center;
  min-height: 100vh;
  font-size: 120px;
  font-family:Arial, Helvetica, sans-serif;
}
h1 {
background-image: url("images.unsplash.com/photo-14622…");
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/274594.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-17 14:14
下一篇 2024-12-17 14:14

相關推薦

發表回復

登錄後才能評論