我发现了7个关于 CSS backgroundImage 好用的技巧

开发 前端
大多数人认为背景图像不可能有任何不寻常的地方,但经过研究,答案并非如此。所以本文收集了七个我认为最有用的技巧,并创建了一些代码示例。

本文转载自微信公众号「大迁世界」,转载本文请联系大迁世界公众号。

背景图像可能是我们所有前端开发人员在我们的职业生涯中至少使用过几次的CSS属性之一。大多数人认为背景图像不可能有任何不寻常的地方,但经过研究,答案并非如此。所以本文收集了七个我认为最有用的技巧,并创建了一些代码示例。

1. 背景图如何才能完美适配视口

让背景图适配视口很容易,需要使用下面 CSS 即可:

  1. body { 
  2.   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'); 
  3.   background-repeat: no-repeat; 
  4.   background-position: center; 
  5.   background-attachment: fixed; 
  6.   background-size: cover; 
  7.   -webkit-background-size: cover; 
  8.   -moz-background-size: cover; 
  9.   -o-background-size: cover; 

[[326397]]

事例源码:https://codepen.io/duomly/pen/xxwYBOE

2. 如何在CSS中使用多个背景图片?

如果我想在背景中添加一张以上的图片怎么办?CSS3 中可以直接 指定多个背景路径,如下所示:

  1. body { 
  2.   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); 
  3.   background-position: center, top; 
  4.   background-repeat: repeat, no-repeat; 
  5.   background-size: contain, cover; 

事例源码:https://codepen.io/duomly/pen/eYpVoJR

3. 如何创建一个三角形的背景图像

另一个很酷的背景特效就是三角形背景,当我们想展示某些完全不同的选择(例如白天和黑夜或冬天和夏天)时,这种特效就更加棒。

思路是这样的,首先创建两个div,然后将两个背景都添加到其中,然后,第二个div使用clip-path属性画出三角形。

「html」

  1. <body> 
  2.   <div class="day"></div> 
  3.   <div class="night"></div> 
  4. </body> 
「css」
  1. body { 
  2.   margin: 0; 
  3.   padding: 0; 
  4.  
  5. div { 
  6.   position: absolute; 
  7.   height: 100vh; 
  8.   width: 100vw; 
  9.  
  10. .day { 
  11.   background-image: url("https://images.unsplash.com/photo-1477959858617-67f85cf4f1df?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2613&q=80"); 
  12.   background-size: cover; 
  13.   background-repeat: no-repeat; 
  14.  
  15. .night { 
  16.   background-image: url("https://images.unsplash.com/photo-1493540447904-49763eecf55f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80"); 
  17.   background-size: cover; 
  18.   background-repeat: no-repeat; 
  19.   clip-path: polygon(100vw 0, 0% 0vh, 100vw 100vh); 

源码:https://codepen.io/duomly/pen/RwWQmwW

4. 如何在背景图像上添加叠加渐变?

有时我们想在背景上添加一些文字,但有的图片太亮,导致字看不清楚,所以这里我们就需要让背景图叠加一些暗乐来突出文字效果。

例如,可以通过添加粉红橙色渐变或红色至透明渐变来增强日落图像,这些情况下使用叠加的渐变就很容易做到。

「css」

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

源码:https://codepen.io/duomly/pen/rNOJgQE

5. 如何创建一个颜色动态变化的背景

如果你很多颜色,你想确认哪种颜色更适合背景图片的颜色,刚动态更改背景颜色的技巧就很有用。

「css」

  1. HTML CSSResult 
  2. EDIT ON 
  3. @keyframes background-overlay-animation { 
  4.   0%   { 
  5.       background-image: 
  6.         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"); 
  7.   } 
  8.   25%  { 
  9.       background-image: 
  10.          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"); 
  11.   } 
  12.   50%  { 
  13.     background-image: 
  14.        linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), 
  15.      url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80"); 
  16.   } 
  17.   100% { 
  18.     background-image: 
  19.         linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), 
  20.         url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80"); 
  21.   } 
  22.  
  23. @-webkit-keyframes background-overlay-animation { 
  24.   0%   { 
  25.       background-image: 
  26.         linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%) 
  27.         url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80"); 
  28.   } 
  29.   25%  { 
  30.       background-image: 
  31.          linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), 
  32.         url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80"); 
  33.   } 
  34.   50%  { 
  35.     background-image: 
  36.        linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), 
  37.      url("https://images.unsplash.com/photo-1559310589-2673bfe16970?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80"); 
  38.   } 
  39.   100% { 
  40.     background-image: 
  41.         linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), 
  42.     

源码:https://codepen.io/duomly/pen/gOavNOv

6. 如何制作网格背景图像?

有时候会遇到一些需要有艺术或者摄影类的项目,他们一般要求网站要有艺术信息,要有创意。网络的背景就挺有创意的,效果如下:

「HTML」

  1. <body> 
  2. <div class="container"> 
  3.   <div class="item_img"></div> 
  4.   <div class="item"></div> 
  5.   <div class="item_img"></div> 
  6.   <div class="item"></div> 
  7.   <div class="item"></div> 
  8.   <div class="item_img"></div> 
  9.   <div class="item"></div> 
  10.   <div class="item_img"></div> 
  11.   <div class="item"></div> 
  12.   <div class="item"></div> 
  13.   <div class="item_img"></div> 
  14.   <div class="item"></div> 
  15.   <div class="item_img"></div> 
  16.   <div class="item"></div> 
  17.   <div class="item_img"></div> 
  18.   <div class="item"></div> 
  19. </div> 
  20. </body> 
  21.  

「scss」

  1. body { 
  2.  margin: 0; 
  3.   padding: 0; 
  4.  
  5. .container { 
  6.   position: absolute; 
  7.   width: 100%; 
  8.   height: 100%; 
  9.   background: black; 
  10.   display: grid; 
  11.   grid-template-columns: 25fr 30fr 40fr 15fr; 
  12.   grid-template-rows: 20fr 45fr 5fr 30fr; 
  13.   grid-gap: 20px; 
  14.   .item_img { 
  15.     background-image: url('https://images.unsplash.com/photo-1499856871958-5b9627545d1a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2207&q=80'); 
  16.   background-repeat: no-repeat; 
  17.   background-position: center; 
  18.   background-attachment: fixed; 
  19.   background-size: cover; 

源码:https://codepen.io/duomly/pen/MWaQNWb

7. 如何将背景图像设置为文本颜色?

使用background-image与background-clip,可以实现背景图像对文字的优美效果。在某些情况下,它可能非常有用,尤其是当我们想创建一个较大的文本标题而又不如普通颜色那么枯燥的情况。

「HTML」

  1. <body> 
  2.   <h1>Hello world!</h1> 
  3. </body> 

「SCSS」

  1. body { 
  2.  margin: 0; 
  3.   padding: 0; 
  4.  
  5. .container { 
  6.   position: absolute; 
  7.   width: 100%; 
  8.   height: 100%; 
  9.   background: black; 
  10.   display: grid; 
  11.   grid-template-columns: 25fr 30fr 40fr 15fr; 
  12.   grid-template-rows: 20fr 45fr 5fr 30fr; 
  13.   grid-gap: 20px; 
  14.   .item_img { 
  15.     background-image: url('https://images.unsplash.com/photo-1499856871958-5b9627545d1a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2207&q=80'); 
  16.   background-repeat: no-repeat; 
  17.   background-position: center; 
  18.   background-attachment: fixed; 
  19.   background-size: cover; 

源码:https://codepen.io/duomly/pen/wvKyVjG

 

 

责任编辑:赵宁宁 来源: 大迁世界
相关推荐

2020-06-16 08:39:35

JavaScript图像处理库

2021-01-26 11:16:12

漏洞网络安全网络攻击

2021-10-29 11:45:26

Python代码Python 3.

2021-04-22 07:47:47

JavaJDKMYSQL

2022-11-30 09:18:51

JavaMyBatisMQ

2020-04-01 08:40:44

Vue.jsweb开发

2020-06-30 10:38:36

Python 开发编程语言

2021-07-10 07:40:27

Excel数据分析大数据

2021-04-28 14:31:35

Dubbo接口日志

2023-05-17 00:22:15

2022-04-26 06:43:12

文档TCPLinux

2021-05-13 16:34:20

TCP客户端

2023-10-16 15:41:14

WebCSS

2021-06-02 08:00:57

WebAsyncTas项目异步

2021-04-26 05:41:32

百度网盘秒传

2023-02-26 01:02:22

2020-08-04 08:48:34

数据弹屏技术

2022-04-28 09:41:29

Linux 操作系统漏洞Microsoft

2021-12-29 19:20:41

数据GitHub服务器

2023-06-24 23:11:07

点赞
收藏

51CTO技术栈公众号