简体   繁体   English

CSS opacity 只给背景色,而不是文字就可以了? [复制]

[英]CSS opacity only to background color, not the text on it? [duplicate]

Can I assign the opacity property to the background property of a div only and not to the text on it?我可以将opacity属性仅分配给divbackground属性而不是其上的文本吗?

I've tried:我试过了:

background: #CCC;
opacity: 0.6;

but this doesn't change the opacity.但这不会改变不透明度。

It sounds like you want to use a transparent background, in which case you could try using the rgba() function:听起来您想使用透明背景,在这种情况下,您可以尝试使用rgba()函数:

rgba(R, G, B, A)

R (red), G (green), and B (blue) can be either <integer> s or <percentage> s, where the number 255 corresponds to 100%. R(红色)、G(绿色)和 B(蓝色)可以是<integer> s 或<percentage> s,其中数字 255 对应于 100%。 A (alpha) can be a <number> between 0 and 1, or a <percentage> , where the number 1 corresponds to 100% (full opacity). A (alpha) 可以是介于 0 和 1 之间的<number><percentage> ,其中数字 1 对应于 100%(完全不透明度)。

RGBa example RGBa 示例

background: rgba(51, 170, 51, .1) /* 10% opaque green */ background: rgba(51, 170, 51, .4) /* 40% opaque green */ background: rgba(51, 170, 51, .7) /* 70% opaque green */ background: rgba(51, 170, 51, 1) /* full opaque green */

A small example showing how rgba can be used.一个展示如何使用rgba例子

As of 2018, practically every browser supports the rgba syntax .截至 2018 年,几乎每个浏览器都支持rgba语法

The easiest way to do this is with 2 divs, 1 with the background and 1 with the text:最简单的方法是使用 2 个 div,1 个用于背景,1 个用于文本:

 #container { position: relative; width: 300px; height: 200px; } #block { background: #CCC; filter: alpha(opacity=60); /* IE */ -moz-opacity: 0.6; /* Mozilla */ opacity: 0.6; /* CSS3 */ position: absolute; top: 0; left: 0; height: 100%; width: 100%; } #text { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
 <div id="container"> <div id="block"></div> <div id="text">Test</div> </div>

For Less users only:仅适用于较少用户:

If you don't like to set your colors using RGBA, but rather using HEX, there are solutions.如果您不喜欢使用 RGBA 设置颜色,而是使用 HEX,有一些解决方案。

You could use a mixin like:你可以使用像这样的混合:

.transparentBackgroundColorMixin(@alpha,@color) {
  background-color: rgba(red(@color), green(@color), blue(@color), @alpha);
}

And use it like:并像这样使用它:

.myClass {
    .transparentBackgroundColorMixin(0.6,#FFFFFF);
}

Actually this is what a built-in Less function also provide:实际上,这是一个内置的 Less 函数也提供的:

.myClass {
    background-color: fade(#FFFFFF, 50%);
}

See How do I convert a hexadecimal color to rgba with the Less compiler?请参阅如何使用 Less 编译器将十六进制颜色转换为 rgba?

我的技巧是用颜色创建一个透明的 .png 并使用background:url()

This will work with every browser这适用于所有浏览器

div {
    -khtml-opacity: .50;
    -moz-opacity: .50;
    -ms-filter: ”alpha(opacity=50)”;
    filter: alpha(opacity=50);
    filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0.5);
    opacity: .50;
}

If you don't want transparency to affect the entire container and its children, check this workaround.如果您不希望透明度影响整个容器及其子项,请检查此解决方法。 You must have an absolutely positioned child with a relatively positioned parent to achieve this.你必须有一个绝对定位的孩子和一个相对定位的父母来实现这一点。 CSS Opacity That Doesn't Affect Child Elements 不影响子元素的 CSS 不透明度

Check a working demo at CSS Opacity That Doesn't Affect "Children"检查不影响“儿童”的 CSS Opacity 上的工作演示

I had the same problem.我有同样的问题。 I want a 100% transparent background color.我想要一个 100% 透明的背景颜色。 Just use this code;只需使用此代码; it's worked great for me:它对我很有用:

rgba(54, 25, 25, .00004);

You can see examples on the left side on this web page (the contact form area).您可以在此网页左侧(联系表单区域)查看示例。

A great way to do this would be to use CSS 3 indeed.做到这一点的一个好方法是确实使用 CSS 3。

Create a div width a class - eg supersizer on top of your page:创建一个 div 宽度类 - 例如在页面顶部的 supersizer:

Then set following CSS properties:然后设置以下 CSS 属性:

 .supersizer { background: url("http://fc06.deviantart.net/fs70/f/2012/099/d/f/stackoverflow_16x16_icon_by_muntoo_stock-d4vl2v4.png") no-repeat center center fixed; width: 100%; height: 100%; position: fixed; z-index: -1; opacity: 0.5; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; top: 0; }
 <div class="supersizer"></div>

For anyone coming across this thread, here's a script called thatsNotYoChild.js that I just wrote that solves this problem automatically:对于遇到此线程的任何人,这是我刚刚编写的一个名为 thatsNotYoChild.js 的脚本,它可以自动解决此问题:

http://www.impressivewebs.com/fixing-parent-child-opacity/ http://www.impressivewebs.com/fixing-parent-child-opacity/

Basically, it separates children from the parent element, but keeps the element in the same physical location on the page.基本上,它将子元素与父元素分开,但将元素保持在页面上的相同物理位置。

The easiest solution is to create 3 divs .最简单的解决方案是创建 3 个divs One that will contain the other 2, the one with transparent background and the one with content.一个将包含另外两个,一个具有透明背景,另一个包含内容。 Make the first div's position relative and set the one with transparent background to negative z-index , then adjust the position of the content to fit over the transparent background.使第一个 div 的位置相对并将具有透明背景的那个设置为负z-index ,然后调整内容的位置以适应透明背景。 This way you won't have issues with absolute positioning.这样您就不会遇到绝对定位问题。

Use:用:

background:url("location of image"); // Use an image with opacity

This method will work in all browsers.此方法适用于所有浏览器。

You can't.你不能。 You have to have a separate div that is just that background, so that you can only apply the opacity to that.你必须有一个单独的 div 就是那个背景,这样你就可以只应用不透明度。

I tried doing this recently, and since I was already using jQuery, I found the following to be the least hassle:我最近尝试这样做,因为我已经在使用 jQuery,我发现以下是最不麻烦的:

  1. Create the two different divs.创建两个不同的 div。 They'll be siblings, not contained in each other or anything.他们将是兄弟姐妹,不包含在彼此或任何东西中。
  2. Give the text div a solid background color, because that will be the JavaScript-less default.text div 一个纯背景色,因为这将是无 JavaScript 的默认值。
  3. Use jQuery to get the text div's height, and apply it to the background div.使用 jQuery 获取text div 的高度,并将其应用于background div。

I'm sure there's some kind of CSS ninja way to do all this with only floats or something, but I didn't have the patience to figure it out.我确信有某种 CSS ninja 方法可以只用浮点数或其他东西来完成所有这些,但我没有耐心弄清楚。

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

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