CSS3 Border Opacity?

2019-01-03 19:14发布

Is there a straight forward CSS way to make the border of an element semi-transparent with something like :

border-opacity:0.7;

?

If not, does anyone have an idea how I could do so without using images?

标签: css css3 opacity
8条回答
够拽才男人
2楼-- · 2019-01-03 20:10

As an alternate solution that may work in some cases: change the border-style to dotted.

Having alternating groups of pixels between the foreground color and the background color isn't the same as a continuous line of partially transparent pixels. On the other hand, this requires significantly less CSS and it is much more compatible across every browser without any browser-specific directives.

查看更多
爷、活的狠高调
3楼-- · 2019-01-03 20:11

Other answers deal with the technical aspect of the border-opacity issue, while I'd like to present a hack(pure CSS and HTML only). Basically create a container div, having a border div and then the content div.

<div class="container">
  <div class="border-box"></div>
  <div class="content-box"></div>
</div>

And then the CSS:(set content border to none, take care of positioning such that border thickness is accounted for)

.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;
}
查看更多
登录 后发表回答