You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让CSS盒子保持100%宽度仅30%区域显示渐变背景?

实现方法

你可以通过以下两种方式实现盒子宽度保持100%,仅30%区域显示渐变背景的需求:

方案1:直接修改渐变色标

在渐变参数里明确指定渐变终止于30%位置,剩余区域统一设为透明即可:

<div class="box">30%</div>
.box{
    width: 100%;
    background : linear-gradient(to right, rgba(250,0,0,0), rgba(250, 0, 0, 1) 30%, transparent 30%);
}

该方案逻辑:0%30%宽度区间执行从透明到红色的渐变,30%100%宽度区间完全透明。

方案2:控制背景层尺寸

将背景层的宽度设置为30%,同时禁止背景重复:

.box{
    width: 100%;
    background : linear-gradient(to right, rgba(250,0,0,0),rgba(250, 0, 0, 1));
    background-size: 30% 100%;
    background-repeat: no-repeat;
}

如果需要将渐变背景调整到右侧显示,额外添加background-position: right top;属性即可。

内容的提问来源于stack exchange,提问作者김규민

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 07:24:00