CSS同时使用背景图和渐变色

CSS同时使用背景图和渐变色

需求

一个盒子,在拥有渐变色的前提下还需要同时拥有背景图层

类似如下的效果

代码实现

首先我们按照常规的写css的方式来写

复制代码
<div class="box"></div>

 .box{
            width: 300px;
            height: 120px;
            border-radius: 15px;
            background-color:#ff5e62;
            background-image:url('./btc1.png');   
            background-position: right 20px top 50%;
            background-repeat: no-repeat;
        }

这种图片加背景的写法没什么问题。可以正常显示背景和图片

但是我们把背景换成渐变色以后就出问题了

不显示渐变色,只显示一个图片

复制代码
background:linear-gradient(to right, #ff9966, #ff5e62);

完整写法

把背景图和渐变色写在一起

这里url一定要写在前面,展示在上层,如果写在linear-gradient后面就只会显示渐变色

复制代码
.box{
      width: 300px;
      height: 120px;
      border-radius: 15px;
      background:
        url('./btc1.png') right 20px top 50% / 50px 50px no-repeat,
        linear-gradient(to right, #ff9966, #ff5e62);
    }

上面的代码最终呈现的效果

相关推荐
C澒4 小时前
前端整洁架构(Clean Architecture)实战解析:从理论到 Todo 项目落地
前端·架构·系统架构·前端框架
C澒4 小时前
Remesh 框架详解:基于 CQRS 的前端领域驱动设计方案
前端·架构·前端框架·状态模式
Charlie_lll4 小时前
学习Three.js–雪花
前端·three.js
onebyte8bits5 小时前
前端国际化(i18n)体系设计与工程化落地
前端·国际化·i18n·工程化
C澒5 小时前
前端分层架构实战:DDD 与 Clean Architecture 在大型业务系统中的落地路径与项目实践
前端·架构·系统架构·前端框架
BestSongC5 小时前
行人摔倒检测系统 - 前端文档(1)
前端·人工智能·目标检测
0思必得05 小时前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice6 小时前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js
青茶3606 小时前
php怎么实现订单接口状态轮询(二)
前端·php·接口
大橙子额6 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js