div具有渐变背景和圆角

时间:2015-04-25 07:32:46

标签: css css3 radial-gradients

我已使用渐变背景和圆角编码此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的最终缩放结果是:

div with gradient background and rounded corners

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

有没有一种方法,没有jQuery,让一切变得动态?我测试了CSS3 border-image-slice,看起来很有前景,但似乎拒绝radial-gradient作为边框图像。

1 个答案:

答案 0 :(得分:2)

或多或少的请求结果,但使用阴影

创建

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

#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>