【问题标题】:div with gradient background and rounded corners具有渐变背景和圆角的 div
【发布时间】:2015-04-25 07:32:45
【问题描述】:

我用渐变背景和圆角编码了这个div

#pill {
    font-size: 12px;
    height: 40px;
    width: 100px;
    margin: 0 20px 0 20px;
    background: transparent;
    background-image: linear-gradient(#080, #cf0 45%, #cf0 55%, #080);
    z-index: 1;
    text-align: center;
}

#pill::before {
    display: block;
    content: '';
    width: 40px;
    height: 40px;
    position: absolute;
    margin-left: -20px;
    z-index: -1;
    border-radius: 40px;
    background-image: radial-gradient(21px #cf0 5%, #080);
}

#pill::after {
    display: inline-block;
    content: '';
    width: 40px;
    height: 40px;
    position: relative;
    top: -14.5px;
    right: -50px;
    z-index: -1;
    border-radius: 40px;
    background-image: radial-gradient(21px #cf0 5%, #080);
}

Firefox 的结果,在顶部缩放中,是这个:

我对必须使用硬连线值的方式不满意,特别是对于 ::before 元素。

有没有办法在没有 jQuery 的情况下让一切变得动态?我测试了 CSS3 border-image-slice,看起来很有希望,但它似乎拒绝将 radial-gradient 作为边框图像。

【问题讨论】:

  • 为什么不将渐变和border-radius 应用到div 本身而不是使用伪元素?我错过了什么吗?
  • 效果不一样:linear-gradient背景被半径切割,而radial-gradient形成扁平椭圆。

标签: css radial-gradients


【解决方案1】:

或多或少是您要求的结果,但使用阴影创建

您可以使用阴影参数进行微调。

#test {
    height: 40px;
    width: 140px;
  border-radius: 20px;
  background-color: #cf0;
  box-shadow: inset 0px 0px 14px 10px #080;
}


#pill {
    font-size: 12px;
    height: 40px;
    width: 100px;
    margin: 0 20px 0 20px;
    background: transparent;
    background-image: linear-gradient(#080, #cf0 45%, #cf0 55%, #080);
    z-index: 1;
    text-align: center;
}

#pill::before {
    display: block;
    content: '';
    width: 40px;
    height: 40px;
    position: absolute;
    margin-left: -20px;
    z-index: -1;
    border-radius: 40px;
    background-image: radial-gradient(circle 21px, #cf0 5%, #080);
}

#pill::after {
    display: inline-block;
    content: '';
    width: 40px;
    height: 40px;
    position: relative;
    right: -50px;
    z-index: -1;
    border-radius: 40px;
    background-image: radial-gradient(circle 21px, #cf0 5%, #080);
}
<div id=pill></div>
<div id=test></div>

【讨论】:

  • 哇!我想我必须阅读整个 W3C 页面,而不是停留在我认为“有希望”的部分:box-shadow 段落是 CSS3 背景模块页面的最后一个w3.org/TR/css3-background :-)
  • 我你读了一半的 w3c 页面,你做的比大多数人都多!很高兴它有帮助:-)
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 2014-12-16
  • 2010-12-03
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
相关资源
最近更新 更多