作者:ryanmcdermott
译者:前端小智
起源:github

明天送 5 本前端的书,先天开奖,抽奖地址点击这里 https://mp.weixin.qq.com/s/nb...,祝大家好运。

点赞再看,微信搜寻 【大迁世界】 关注这个没有大厂背景,但有着一股向上踊跃心态人。本文 GitHub https://github.com/qq44924588... 上曾经收录,文章的已分类,也整顿了很多我的文档,和教程材料。

大家都说简历没我的项目写,我就帮大家找了一个我的项目,还附赠【搭建教程】。

背景图像可能是咱们所有前端开发人员在咱们的职业生涯中至多应用过几次的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;}

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

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;}

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

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

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

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

html

<body>  <div class="day"></div>  <div class="night"></div></body>

css

body {  margin: 0;  padding: 0;}div {  position: absolute;  height: 100vh;  width: 100vw;}.day {  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");  background-size: cover;  background-repeat: no-repeat;}.night {  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");  background-size: cover;  background-repeat: no-repeat;  clip-path: polygon(100vw 0, 0% 0vh, 100vw 100vh);}

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

4.如何在背景图像上增加叠加突变?

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

例如,能够通过增加粉红橙色突变或红色至通明突变来加强日落图像,这些状况下应用叠加的突变就很容易做到。

css

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}

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

5.如何创立一个色彩动态变化的背景

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

css

HTML CSSResultEDIT ON@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("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%),  

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

大家都说简历没我的项目写,我就帮大家找了一个我的项目,还附赠【搭建教程】。

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

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

HTML

<body><div class="container">  <div class="item_img"></div>  <div class="item"></div>  <div class="item_img"></div>  <div class="item"></div>  <div class="item"></div>  <div class="item_img"></div>  <div class="item"></div>  <div class="item_img"></div>  <div class="item"></div>  <div class="item"></div>  <div class="item_img"></div>  <div class="item"></div>  <div class="item_img"></div>  <div class="item"></div>  <div class="item_img"></div>  <div class="item"></div></div></body>

scss

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('https://images.unsplash.com/photo-1499856871958-5b9627545d1a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2207&q=80');  background-repeat: no-repeat;  background-position: center;  background-attachment: fixed;  background-size: cover;}}

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

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

应用background-image background-clip ,能够实现背景图像对文字的柔美成果。 在某些状况下,它可能十分有用,尤其是当咱们想创立一个较大的文本题目而又不如一般色彩那么干燥的状况。

HTML

<body>  <h1>Hello world!</h1></body>

SCSS

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("https://images.unsplash.com/photo-1462275646964-a0e3386b89fa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2600&q=80");  background-clip: text;  -webkit-background-clip: text;  color: transparent;}

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


代码部署后可能存在的BUG没法实时晓得,预先为了解决这些BUG,花了大量的工夫进行log 调试,这边顺便给大家举荐一个好用的BUG监控工具 Fundebug。

原文:https://dev.to/duomly/discove...


交换

文章每周继续更新,能够微信搜寻「 大迁世界 」第一工夫浏览和催更(比博客早一到两篇哟),本文 GitHub https://github.com/qq449245884/xiaozhi 曾经收录,整顿了很多我的文档,欢送Star和欠缺,大家面试能够参照考点温习,另外关注公众号,后盾回复福利,即可看到福利,你懂的。