简体   繁体   English

如何制作具有不透明边框的透明div?

[英]How can I make a transparent div that has a non-transparent border?

i created a white div and gave it an opacity of 0.4 and then i gave it a black border. 我创建了一个白色的div,使其不透明度为0.4,然后给了它黑色的边框。 however because i made the div transparent, the border was also transparent. 但是因为我使div透明,所以边框也是透明的。 How can I make the border non transparent whilst keeping the div transparent? 如何在保持div透明的同时使边框不透明?

CSS: CSS:

#box{
   background-color:white;
   opacity:0.4;
   width:600px;
   height:200px;
   border-radius:15px;
   border: 5px solid black;

}

You cannot make part of an element one opacity and another part of that same element another opacity. 您不能使一个元素的一部分不透明,而使同一元素的另一部分不透明。

Here is a silly example: https://jsfiddle.net/sheriffderek/85utzq4p/ 这是一个愚蠢的例子: https : //jsfiddle.net/sheriffderek/85utzq4p/

Try using rgba() for background color instead - or wrap the element in something. 尝试使用rgba()代替背景色-或将元素包装在某种东西中。

.box {
  background: rgba(255, 0, 0, .5);
}

Add another div that contains the current div. 添加另一个包含当前div的div。 Remove the border property and the width and height properties on the #box and add it the other containing div. 删除#box上的border属性和width和height属性,并将其添加到另一个包含div的属性中。 Make sure the containing div has a class instead of an id. 确保包含div的类不是ID。 An example: 一个例子:

 .entirebox { width: 600px; height: 200px; border-radius: 15px; border: 5px solid black; } #box { background-color: white; opacity: 0.4; } 
 <div class="entirebox"> <div id="box"> <p>The stuff that you originally had here</p> </div> </div> 

Here, I added the containing div and named it entirebox . 在这里,我添加了包含的div并将其命名为entirebox Notice how the containing div has a class, while the div you started off with still has an id. 请注意,包含的div如何具有一个类,而您开始使用的div仍然具有ID。

Hope this helped. 希望这会有所帮助。

if you are looking for something that can work with solid color backgrounds and image backgrounds both you can create another parent and set it in this way: 如果您正在寻找适用于纯色背景和图像背景的东西,则可以创建另一个父对象并通过以下方式进行设置:

 body{ margin: 0px; } div.child { display: block; position: relative; width: 200px; height: 150px; background: red; opacity:0.3; } div.parent{ display: inline-block; position:relative; border: 4px solid black; top: 0px; left: 0px; } 
 <div class="parent"> <div class="child"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM