html - div两侧的颜色逐渐变透明

标签 html css gradient

我需要使用 CSS 在 div 的每一侧创建颜色渐变的 strip ,如下图所示。 怎么做?

div with fading colour on both sides

最佳答案

在纯色上:

要使两侧的颜色变淡,您可以使用 2 inset box-shadows具有正模糊和负扩散半径:

DEMO

body {
  background: #D4D4CC;
}
div {
  height: 100px;
  background: #fff;
  box-shadow: inset 100px 0px 100px -50px #D4D4CC, 
              inset -100px 0px 100px -50px #D4D4CC;
}
<div></div>


在图像上:

如果您需要在图像或非纯色上淡化双面,您可以将此方法与伪元素和框阴影一起使用:

输出:

div fading on both sides over image

div{
    height:100px;
    overflow:hidden;
    padding:0 100px;
    margin:20px;
}
div:before{
    content:'';
    display:block;
    height:100%;
    background:#D4D4CC;
    box-shadow: 0px 0px 80px 50px #D4D4CC;
}

body{
    background: url('http://i.imgur.com/hPLqUtN.jpg');
    background-size:cover;
}
<div></div>

请注意 IE9 and over 支持框阴影

关于html - div两侧的颜色逐渐变透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28982198/

相关文章:

html - 在 css 中创建像表格一样工作的水平流体布局

html - 为我的评论系统交替背景颜色

javascript - DOM 更改后刷新浏览器悬停效果

jquery - 改变 droppable 掉落时的 css 值

javascript - 无法让 jQuery div 覆盖 div

javascript - 当离线网络应用程序缓存失败时,有效的后备措施是什么?

html - 基于Web技术堆栈构建JavaFX应用程序的UI

svg - 可以在 SVG 中创建(或伪造)二维渐变吗?

css - 带有背景图像的白色到透明渐变

css - 背景渐变 - 填充页面,没有滚动条或重复