有一个直接的CSS方法,使一个元素的边界半透明的东西像这样?
border-opacity: 0.7;
如果没有,有没有人知道我如何在不使用图像的情况下做到这一点?
有一个直接的CSS方法,使一个元素的边界半透明的东西像这样?
border-opacity: 0.7;
如果没有,有没有人知道我如何在不使用图像的情况下做到这一点?
当前回答
试试这个:
<h2>Snippet for making borders transparent</h2>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.
Mauris massa. Vestibulum lacinia arcu eget nulla. <b>Lorem ipsum dolor sit amet, consectetur adipiscing elit</b>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim
lacinia nunc. <i>Lorem ipsum dolor sit amet, consectetur adipiscing elit</i>. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor.
<i>Lorem ipsum dolor sit amet, consectetur adipiscing elit</i>. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod
in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis
turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. <b>Nam nec ante</b>. Suspendisse in justo eu magna luctus suscipit. Sed lectus. <i>Sed convallis tristique sem</i>.
Integer euismod lacus luctus magna. <b>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos</b>. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum
ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc,
viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. <b>Suspendisse in justo eu magna luctus suscipit</b>. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. </p>
</div>
<div id="transparentBorder">
This <div> has transparent borders.
</div>
这里是我们神奇的CSS..
* {
padding: 10pt;
font: 13px/1.5 Helvetica Neue, Arial, Helvetica, 'Liberation Sans', FreeSans, sans-serif;
}
b {
font-weight: bold;
}
i {
font-style: oblique;
}
H2 {
font-size: 2em;
}
div[id='transparentBorder'] {
height: 100px;
width: 200px;
padding: 10px;
position: absolute;
top: 40%;
left: 30%;
text-align: center;
background: Black;
border-radius: 4px;
border: 10pt solid rgba(0, 0, 0, 0.5);
-moz-background-clip: border;
/* Firefox 3.6 */
-webkit-background-clip: border;
/* Safari 4? Chrome 6? */
background-clip: border-box;
/* Firefox 4, Safari 5, Opera 10, IE 9 */
-moz-background-clip: padding;
/* Firefox 3.6 */
-webkit-background-clip: padding;
/* Safari 4? Chrome 6? */
background-clip: padding-box;
/* Firefox 4, Safari 5, Opera 10, IE 9 */
text-align: center;
margin: 0;
color: #fff;
cursor: pointer;
}
点击这里查看演示。
其他回答
试试这个:
<h2>Snippet for making borders transparent</h2>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.
Mauris massa. Vestibulum lacinia arcu eget nulla. <b>Lorem ipsum dolor sit amet, consectetur adipiscing elit</b>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim
lacinia nunc. <i>Lorem ipsum dolor sit amet, consectetur adipiscing elit</i>. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor.
<i>Lorem ipsum dolor sit amet, consectetur adipiscing elit</i>. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod
in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis
turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. <b>Nam nec ante</b>. Suspendisse in justo eu magna luctus suscipit. Sed lectus. <i>Sed convallis tristique sem</i>.
Integer euismod lacus luctus magna. <b>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos</b>. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum
ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc,
viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. <b>Suspendisse in justo eu magna luctus suscipit</b>. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. </p>
</div>
<div id="transparentBorder">
This <div> has transparent borders.
</div>
这里是我们神奇的CSS..
* {
padding: 10pt;
font: 13px/1.5 Helvetica Neue, Arial, Helvetica, 'Liberation Sans', FreeSans, sans-serif;
}
b {
font-weight: bold;
}
i {
font-style: oblique;
}
H2 {
font-size: 2em;
}
div[id='transparentBorder'] {
height: 100px;
width: 200px;
padding: 10px;
position: absolute;
top: 40%;
left: 30%;
text-align: center;
background: Black;
border-radius: 4px;
border: 10pt solid rgba(0, 0, 0, 0.5);
-moz-background-clip: border;
/* Firefox 3.6 */
-webkit-background-clip: border;
/* Safari 4? Chrome 6? */
background-clip: border-box;
/* Firefox 4, Safari 5, Opera 10, IE 9 */
-moz-background-clip: padding;
/* Firefox 3.6 */
-webkit-background-clip: padding;
/* Safari 4? Chrome 6? */
background-clip: padding-box;
/* Firefox 4, Safari 5, Opera 10, IE 9 */
text-align: center;
margin: 0;
color: #fff;
cursor: pointer;
}
点击这里查看演示。
其他答案涉及边界不透明问题的技术方面,而我想提出一个hack(纯CSS和HTML)。基本上创建一个容器div,有一个边框div,然后是内容div。
<div class="container">
<div class="border-box"></div>
<div class="content-box"></div>
</div>
然后CSS:(将内容边框设置为none,注意定位,以便考虑边框厚度)
.container {
width: 20vw;
height: 20vw;
position: relative;
}
.border-box {
width: 100%;
height: 100%;
border: 5px solid black;
position: absolute;
opacity: 0.5;
}
.content-box {
width: 100%;
height: 100%;
border: none;
background: green;
top: 5px;
left: 5px;
position: absolute;
}
在这里稍微跳出框框思考一下:如果您正在使用SVG基本形状,您可以使用笔画、笔画宽度(浏览器支持> 97%)和笔画不透明度(浏览器支持> 99%)的组合来完成基本上与OP要求的相同的事情。
例如,这样的声明:
circle {
stroke: blue;
stroke-width: 5px;
stroke-opacity: 0.4
}
将在<circle>的周长添加一个半透明的蓝色晕,填充#000。
这个JSFiddle提供了几个SVG基本形状的演示。小提琴使用红色填充和蓝色描边来突出描边和边框之间的主要区别-一半的描边宽度在基本形状的周长内。
这就给出了一个“双边框”外观(从外部到每个基本形状的中心:蓝色->(蓝色+红色=紫色)->红色),这是不可能仅用CSS边框实现的(但边框+轮廓可以-参见上面的JSFiddle,使用<div>的例子),并且很难实现(在径向基本形状的情况下)用径向梯度()。
在某些情况下,另一种可行的解决方案是:将边框样式改为虚线样式。
在前景色和背景色之间有交替的像素组与连续的部分透明像素线是不同的。另一方面,这大大减少了对CSS的要求,而且在不需要任何特定于浏览器的指令的情况下,它在每个浏览器之间都更加兼容。
这很简单,使用一个0偏移量的实体阴影:
#foo {
border-radius: 1px;
box-shadow: 0px 0px 0px 8px rgba(0,0,0,0.3);
}
此外,如果您为元素设置一个边界半径,它会为您提供非常圆润的边界
jsFiddle演示