专业汉语词典知识平台,分享汉字词语知识、历史文学知识解答!

励北网
励北网

backgroundimage用法,backgroundimage使用技巧

来源:小易整编  作者:小易  发布时间:2023-03-07 05:18
摘要:backgroundimage用法,backgroundimage使用技巧背景图像可能是我们所有前端开发人员在我们的职业生涯中至少使用过几次的CSS属性之一。大多数人认为背景图像不可能有任何不寻常的地方,但经过研究,答案并非如此。所以本文收...

 backgroundimage用法,backgroundimage使用技巧

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

backgroundimage用法,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; }

backgroundimage用法,backgroundimage使用技巧

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

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

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

backgroundimage用法,backgroundimage使用技巧

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

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

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

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

backgroundimage用法,backgroundimage使用技巧

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

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

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

「css」

HTML CSSResult EDIT 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%),

backgroundimage用法,backgroundimage使用技巧

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

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

backgroundimage用法,backgroundimage使用技巧

「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/MWaQNWb

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

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

backgroundimage用法,backgroundimage使用技巧

「HTML」

<body>  <h1>Hello world!</h1> </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://www.neebe.cn/wenda/918243.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!


百科问答
小编:小易整编
相关文章相关阅读
  • Win 7系统的EFS加密功能使用技巧

    Win 7系统的EFS加密功能使用技巧

    什么是EFS,EFS加密是基于公钥策略的。在使用EFS加密一个文件或文件夹时,系统首先会生成一个由伪随机数组成的FEK(FileEncryptionKey,文件文件加密钥匙),然后将利用FEK和数据扩展标准X算法创建加密后的文件,并把它...

  • sketchup使用技巧:[2]如何使用路径跟随

    sketchup使用技巧:[2]如何使用路径跟随

    su中,路径跟随是一个极其强大的功能,一些很复杂的面都能通过路径跟随来完成。那么如何在sketchup中推拉出立体物件呢?下面就来简单的说明一下。操作方法01画出一个作为路径的面。...

  • 手机蓝牙的作用和用法(手机蓝牙有什么用途)

    手机蓝牙的作用和用法(手机蓝牙有什么用途)

    每部手机上都有一个蓝牙功能,大家以前都会用它来传输文件,不过随着移动网络和WiFi的普及,这个功能也渐渐被大家忽略了。今天,我们就来科普一些手机蓝牙不为人知的功能,让大家可以更好的使用这一功能。1、传输文件传输文件,这一功能应该是大家最熟悉...

  • 磁盘阵列raid5的用法

    磁盘阵列raid5的用法

    操作方法01在日常应用中,大多是把服务器上所有的硬盘创建RAID5,并且只划分了一个“逻辑磁盘”,这样从理论上来讲没有任何问题,在实际中也可以可以使用的,但是这种方法并不可取。RAID5是一种存储性...

  • C语言while语句的用法

    C语言while语句的用法

    while语句的一般形式为:while(表达式)语句其中表达式是循环条件,语句为循环体。#includeintmain(void){inti,sum=0;i=1;while(i...

  • 电脑触摸板的使用方法(电脑触控板使用技巧)

    电脑触摸板的使用方法(电脑触控板使用技巧)

    你对你笔记本电脑的触控板熟悉吗?触控板的操作方法你掌控了吗?看到别人流畅的使用触控板你羡慕吗?今天小编教你怎么玩转触控板,让你轻松掌握触控板的各项功能。首先我们先介绍一下触控板的品牌,目前各大计算机品牌的触控板多为Synaptcs和Alps...

  • 详解Linux中hdparm命令查看硬盘信息的用法

    详解Linux中hdparm命令查看硬盘信息的用法

    功能说明:显示与设定硬盘的参数。语  法:hdparm[-CfghiIqtTvyYZ][-a][-A][-c][-d][-k][-K][-m][-n...

  • 机动都市阿尔法枪盾好用吗 枪盾用法大全

    机动都市阿尔法枪盾好用吗 枪盾用法大全

    机动都市阿尔法中各式各样的武器,每个都有自己的特点,枪盾也是深受玩家青睐,那么机动都市阿尔法枪盾好用吗,下面就和小编一起来了解一下吧。一、武器简介:枪盾从外观即可看出组成,一面盾牌,一把散弹枪,可以看出这是攻守兼备的一个武器,尤其是在近战中...

  • 周排行
  • 月排行
  • 年排行

精彩推荐