2015-10-14 78 views
1

简而言之,我需要在具有透明背景的动态大小的圆上放置渐变。有没有办法在CSS/HTML中重新创建以下图像?在HTML/CSS中创建渐变圆环

据我所知,无论是径向渐变还是边界图像都无法构造这种形状。关键是圆的中心需要是透明的,因为我不能通过用白色填充中心来“伪造”圆环。

enter image description here

更新:此效果是SVG很容易实现的,但我想知道的HTML/CSS只有这样,才能实现这一目标。看到这个例子:http://codepen.io/MaxXiong/pen/rOGzgR

+0

只是为了澄清,你是否愿意引用CSS的SVG路径? –

+0

我很好奇是否有纯粹的CSS解决方案。如果我们将SVG带入方程中,我可以直接使用示例 – mxiong

回答

1

只使用CSS,我相信你是有限的(如果你不想在CSS中使用任何SVG)。

在分辨率方面不可扩展的可行解决方案是通过像这样的PNG创建一个简单的遮罩。

Inner Circle Mask

透明部分是将来自元件的边界框被去除的区域。

外圈可以通过border-radius: 50%平凡地创建。

body { 
 
    background-color: #d5d5d5; 
 
} 
 

 
.torus { 
 
    width: 312px; 
 
    height: 312px; 
 
    
 
    /* creates outer circle */ 
 
    border-radius: 50%; 
 
    
 
    /* masks the inner circle */ 
 
    -webkit-mask: url(http://i.imgur.com/pFLUTns.png) no-repeat; 
 
    mask: url(http://i.imgur.com/pFLUTns.png) no-repeat; 
 
    
 
    /* gradient background */ 
 
    background: #00601b; 
 
    background: -moz-linear-gradient(top, #00601b 0%, #e10019 100%); 
 
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#00601b), color-stop(100%,#e10019)); 
 
    background: -webkit-linear-gradient(top, #00601b 0%,#e10019 100%); 
 
    background: -o-linear-gradient(top, #00601b 0%,#e10019 100%); 
 
    background: -ms-linear-gradient(top, #00601b 0%,#e10019 100%); 
 
    background: linear-gradient(to bottom, #00601b 0%,#e10019 100%); 
 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00601b', endColorstr='#e10019',GradientType=0); 
 
}
<div class="torus"></div>

更新

您可以使用radial-gradient()动态创建,我原本有PNG手动完成剪辑路径。目前仅支持webkit。

除了浏览器支持,唯一稍微不满意的结果是内圈上没有锯齿。您可以将这些值混淆在一起,最后创建一个±1%的内插值,但我个人认为硬切断看起来更好。但是,嘿,这是非常直接的,尊重集装箱的规模!

body { 
 
    background-color: #d5d5d5; 
 
} 
 

 
.torus { 
 
    width: 312px; 
 
    height: 312px; 
 
    
 
    /* create the outer circle */ 
 
    border-radius: 50%; 
 
    
 
    /* use a radial gradient to create the inner circle mask */ 
 
    /* tweak 60% for the desired radius of the gradient */ 
 
    -webkit-mask: radial-gradient(ellipse at center, 
 
     rgba(0,0,0,0) 0%, rgba(0,0,0,0) 60%, 
 
     rgba(0,0,0,1) 60%, rgba(0,0,0,1) 100% 
 
    ); 
 
    mask: radial-gradient(ellipse at center, 
 
     rgba(0,0,0,0) 0%, rgba(0,0,0,0) 60%, 
 
     rgba(0,0,0,1) 60%, rgba(0,0,0,1) 100% 
 
    ) 
 
    
 
    /* gradient background */ 
 
    background: #00601b; 
 
    background: -moz-linear-gradient(top, #00601b 0%, #e10019 100%); 
 
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#00601b), color-stop(100%,#e10019)); 
 
    background: -webkit-linear-gradient(top, #00601b 0%,#e10019 100%); 
 
    background: -o-linear-gradient(top, #00601b 0%,#e10019 100%); 
 
    background: -ms-linear-gradient(top, #00601b 0%,#e10019 100%); 
 
    background: linear-gradient(to bottom, #00601b 0%,#e10019 100%); 
 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00601b', endColorstr='#e10019',GradientType=0); 
 
}
<div class="torus"></div>

+1

中公布的SVG。不幸的是,它需要可扩展。对不起,我忘了在原来的问题中提及!感谢您的提示,但! – mxiong

+1

我会做更多的研究并更新我的答案! –

+1

太棒了!我从来没有想过使用具有定义渐变的遮罩。感谢这个好主意! – mxiong

1

的更新问题没有完美的解决方案(CSS嵌入禁止SVG),目前工作在基于Firefox浏览器只(据我所知),但希望把CSS clip-path进入讨论:

div { 
 
    height:100px; 
 
    width:100px; 
 
    background-image: linear-gradient(to bottom, #393, #933); 
 
    clip-path: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"><defs><clipPath id="a"><path d="M 50 10 A 25 25 0 0 0 50 90 A 25 25 0 0 0 50 10 M 50 0 A 50 50 0 0 1 50 100 A 50 50 0 0 1 50 0" /></clipPath></defs></svg>#a'); 
 
} 
 

 
body { 
 
    background-image: linear-gradient(to right, #333, #666, #333); 
 
    background-size: 33px 10px; 
 
}
<div>

0

使用unicode圈并设置渐变字体颜色。

http://jsfiddle.net/agvbqvmn/1/

.bg { 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: #eee; 
 
} 
 
.torus:after { 
 
    content: '\020dd'; 
 
    display: block; 
 
    font-size: 72px; 
 
    line-height: 102px; 
 
    background: linear-gradient(to bottom, #00601b 0%,#e10019 100%); 
 
    -webkit-background-clip: text; 
 
    -webkit-text-fill-color: transparent; 
 
}
<div class="bg"> 
 
    <div class="torus"></div> 
 
</div>

+0

非常有创意!不幸的是我需要能够调整边框的厚度。 – mxiong