专业IT网络知识平台,分享IT百科知识、生活百科知识解答!

易企推科技
易企推科技

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


本文地址:IT百科频道 https://www.hkm168.com/tags/918243.html,易企推百科一个免费的知识分享平台,本站部分文章来网络分享,本着互联网分享的精神,如有涉及到您的权益,请联系我们删除,谢谢!


IT百科
小编:小易整编
相关文章相关阅读
  • 什么是Linux系统中nc命令?nc命令的用法详解

    什么是Linux系统中nc命令?nc命令的用法详解

    这篇文章主要介绍了linux系统中nc命令的基本用法,nc命令非常之强大,这里先简单介绍它用来作端口扫描以及文件传输等的基础使用。功能说明:功能强大的网络工具,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本。因为它短小...

  • 数据库的substr函数用法是什么

    数据库的substr函数用法是什么

    数据库的substr函数用法:1、【substr(str,pos,len)】从pos开始的位置,截取len个字符;2、【substr(str,pos)】pos开始的位置,一直截取到最后。数据库的substr函数用法:1、SUBSTR(st...

  • c语言中pow函数的用法是什么?

    c语言中pow函数的用法是什么?

    在c语言中pow()函数是用来求x的y次幂。x、y及函数值都是double型,其语法为“doublepow(doublex,doubley)”;其中参数“doublex”表示底数;参数“doubley”表示指数。pow()函数...

  • c语言中fun用法详解

    c语言中fun用法详解

    c语言中fun用法详解fun函数是自定义的C/C++语言函数,函数功能多样。该函数名为“函数”英文function的简写,一般在示例和试题中使用,通常在主函数中被调用。C/C++语言中,fun函数通常被主函数所调用。它是指用fun来定义一个...

  • matlab中zeros函数用法

    matlab中zeros函数用法

    matlab中zeros函数是用于返回一个double类零矩阵,其用法是:1、在命令行窗口中输入“b=zeros(5)”,按回车键可生成一个“5*5”的零矩阵;2、在命令行窗口中输入“b=zeros(3,4)”,并按回车键即可。zeros函...

  • jquery中select2的用法是什么

    jquery中select2的用法是什么

    在jquery中,select2用于优化select使其支持单选和多选,同时也支持分组显示、列表检索,是一款基于jquery的下拉列表插件,调用语法为“$(select).select2()”。本教程操作环境:windows10系统、jq...

  • linux中grep的用法是什么

    linux中grep的用法是什么

    在linux中,grep命令用于查找文件里符合条件的字符串,语法为“grep选项模式文件”;该命令可以查找包含指定范本样式的文件,如果文件内容符合指定的范本样式,就会把含有范本样式的那一列显示出来。本教程操作环境:linux7.3系统...

  • oracle中nvl函数的用法是什么

    oracle中nvl函数的用法是什么

    在oracle中,nvl()函数用于从两个表达式返回一个非null值,语法为“nvl(表达式1,表达式2)”;若表达式1的计算结果为null值,则该函数返回表达式2的结果,若表达式1的计算结果不是null值,则返回表达式1的结果。本教程操作...

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

精彩推荐