如何为div实现实线+渐变边框及多渐变边框?
二、实现两组及以上相对的渐变边框
比如上下边框用一种渐变,左右边框用另一种渐变,推荐两种方案:
伪元素+辅助元素方案(灵活可控)
用伪元素处理上下渐变,辅助div处理左右渐变:
.your-div { position: relative; border: 2px solid #000; padding: 15px; background: #fff; box-sizing: border-box; } /* 上下渐变边框 */ .your-div::before { content: ''; position: absolute; top: 2px; left: 2px; right: 2px; height: 2px; background: linear-gradient(to right, #ff4444, #ffee44); } .your-div::after { content: ''; position: absolute; bottom: 2px; left: 2px; right: 2px; height: 2px; background: linear-gradient(to right, #ff4444, #ffee44); } /* 左右渐变边框 */ .your-div .side-border { position: absolute; top: 4px; /* 避开上下渐变和实线边框 */ bottom: 4px; width: 2px; background: linear-gradient(to bottom, #4444ff, #44ffee); } .your-div .side-border.left { left: 2px; } .your-div .side-border.right { right: 2px; }
HTML结构只需加两个辅助元素:
<div class="your-div"> <div class="side-border left"></div> <div class="side-border right"></div> 你的内容 </div>
多层背景方案(零额外HTML,更简洁)
通过精确设置渐变的位置和尺寸,实现不同方向的渐变边框:
.your-div { border: 2px solid #000; padding: 15px; background: /* 上下渐变 */ linear-gradient(to right, #ff4444, #ffee44) top / 100% 2px, linear-gradient(to right, #ff4444, #ffee44) bottom / 100% 2px, /* 左右渐变 */ linear-gradient(to bottom, #4444ff, #44ffee) left / 2px 100%, linear-gradient(to bottom, #4444ff, #44ffee) right / 2px 100%, /* 内容背景 */ #fff; background-repeat: no-repeat; background-origin: border-box; box-sizing: border-box; }
三、你的现有代码优化(针对移动应用)
原始代码存在几个问题:border-image不支持多渐变源,冗余的IE滤镜(移动应用完全不需要兼容IE6-9),过度的浏览器前缀。优化后的代码如下:
优化思路:
- 移除IE滤镜,移动端无需考虑旧版IE
- 仅保留
-webkit-前缀兼容旧版iOS Safari,主流移动端浏览器已支持标准渐变语法 - 放弃
border-image方案,改用前面推荐的伪元素或多层背景方案(因为border-image无法保留实线边框)
优化后的多层背景方案代码:
.your-div { border: 2px solid #000; padding: 10px; background: -webkit-linear-gradient(to bottom, rgba(255,224,122,1) 0%,rgba(153,119,0,1) 15%,rgba(35,35,35,1) 39%,rgba(51,51,51,1) 45%,rgba(51,51,51,1) 58%,rgba(132,117,66,1) 59%,rgba(255,249,204,1) 100%), linear-gradient(to bottom, rgba(255,224,122,1) 0%,rgba(153,119,0,1) 15%,rgba(35,35,35,1) 39%,rgba(51,51,51,1) 45%,rgba(51,51,51,1) 58%,rgba(132,117,66,1) 59%,rgba(255,249,204,1) 100%), #fff; background-clip: padding-box, border-box; background-origin: padding-box, border-box; box-sizing: border-box; -webkit-box-sizing: border-box; }
优化后的伪元素方案代码:
.your-div { position: relative; border: 2px solid #000; padding: 10px; background: #fff; box-sizing: border-box; -webkit-box-sizing: border-box; } .your-div::before { content: ''; position: absolute; top: 2px; left: 2px; right: 2px; bottom: 2px; border: 2px solid transparent; background: -webkit-linear-gradient(to bottom, rgba(255,224,122,1) 0%,rgba(153,119,0,1) 15%,rgba(35,35,35,1) 39%,rgba(51,51,51,1) 45%,rgba(51,51,51,1) 58%,rgba(132,117,66,1) 59%,rgba(255,249,204,1) 100%), linear-gradient(to bottom, rgba(255,224,122,1) 0%,rgba(153,119,0,1) 15%,rgba(35,35,35,1) 39%,rgba(51,51,51,1) 45%,rgba(51,51,51,1) 58%,rgba(132,117,66,1) 59%,rgba(255,249,204,1) 100%) border-box; pointer-events: none; }
内容的提问来源于stack exchange,提问作者Hideto
相关产品推荐
相关产品推荐

