简体   繁体   English

如何使不透明度仅适用于div类中的背景

[英]How to make Opacity only apply to background in div class

When I set the opacity to my main-body div class, it makes the text and everything else opacity too. 当我将不透明度设置为主体div类时,它会使文本以及其他所有内容也变得不透明。 I only want the gray background to have opacity. 我只希望灰色背景具有不透明度。 Here is my code: 这是我的代码:

<div class="main1">
content
</div>

CSS: CSS:

.main1
{
background-color:#333333;
border:2px solid #7C7C7C;
text-align:center;
width:1250px;
top:auto;
bottom:auto;
left:300px;
right:300px;
margin-left:auto;
margin-right:auto;
}

Everything gets opacity when this happens. 发生这种情况时,所有内容都会变得不透明。 Any help? 有什么帮助吗?

只是让背景色不透明

background-color: rgba(51,51,51,0.5);

Make 2 divs inside that with the same size as each other and make them over lap then you make div 1 have opacity and div2 have the content. 在其中的2个div彼此之间具有相同的大小,并使其重叠一圈,然后使div 1具有不透明度,并且div2具有内容。 I would attach an example but in using my phone 我会举一个例子,但在使用手机时

background-color:rgba(51, 51, 51, 0.5)

如果您的BG是一种颜色,则可以使用上面代码中0.5的alpha滤镜。

Set the background color to rgba(r,g,b,opacity) 将背景颜色设置为rgba(r,g,b,opacity)

background-color: (51, 51, 51, 0.2). background-color:(51,51,51,0.2)。

This will put opacity on only the background, and not the entire div (which included your text ) 这会将不透明度仅显示在背景上,而不是整个div(包括您的文本)

You can use a converter to convert hex colors to rgb colors. 您可以使用转换器将十六进制颜色转换为rgb颜色。

You also need to color the text!! 您还需要为文本加上颜色! First like others said use CSS background color. 首先像其他人说的那样使用CSS背景色。 Here's the full code and the JS Fiddle 这是完整的代码和JS小提琴

.main1
 {
 background-color:rgba(255,0,0,0.5);
 border:2px solid #7C7C7C;
 text-align:center;
 width:150px;
 top:auto;
 bottom:auto;
 left:300px;
 right:300px;
 margin-left:auto;
 margin-right:auto;
 color: black;

} }

HTML HTML

<div class="main1">
content
</div>

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

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