Transparent border with background color

戏子无情 提交于 2020-01-21 04:19:34

问题


This is strange.

This works:

border-right: 1px solid rgba(0,0,0,0.12);
/* renders a gray border */

But when I use it together with background color, then border is now a solid black line.

background-color: #333;
border-right: 1px solid rgba(0,0,0,0.12);
/* renders a black border */

Am I missing something ?

http://codepen.io/anon/pen/myxpXN


回答1:


The behaviour you are experiencing is that the background of the element appears through the transparent border. If you want to prevent this and clip the background inside the border, you can use:

background-clip: padding-box;

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  background:green;
}
#nav {
  position:relative;
  height: 100%;
  width: 240px;
  background-clip: padding-box;  /** <-- this **/
  background-color: pink;
  border-right: 10px solid rgba(0,0,0,0.12);
}
header {
  height: 4em;
  background-color: #ffffff;
}
<div id="nav">
        <header></header>
        <nav></nav>
    </div>

More info about background-clip on MDN.



来源:https://stackoverflow.com/questions/28603713/transparent-border-with-background-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!