本文翻译自:Opacity of background-color, but not the text [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

  • How do I give text or an image a transparent background using CSS? 如何使用CSS为文本或图像提供透明背景? 28 answers 28个答案

How do I make the cross-browser (including Internet Explorer 6) transparency for the background of a div while the text remains opaque? 如何在文本保持不透明的情况下为div的背景制作跨浏览器(包括Internet Explorer 6)透明度?

I need to do it without using any library such as jQuery, etc. (But if you know of a library that does it I'd love to know so I can look at their code). 我需要在不使用任何库(如jQuery等)的情况下完成它。(但是如果你知道一个库那样做我很想知道,所以我可以查看他们的代码)。


#1楼

参考:https://stackoom.com/question/2fx3/背景颜色的不透明度-但不是文本-重复


#2楼

Relaxing your requirement to work on IE6 and legacy browsers you can use ::before and display: inline-block 放宽您对IE6和旧版浏览器的工作要求,您可以使用:: before和display:inline-block

div
{display: inline-block;position: relative;
}
div::before
{content: "";display: block;position: absolute;z-index: -1;width: 100%;height: 100%;background:red;opacity: .2;
}

Demo at http://jsfiddle.net/KVyFH/172/​ 演示http://jsfiddle.net/KVyFH/172/

It will work on any modern browser 它适用于任何现代浏览器


#3楼

Thanks @davy-landmann for https://stackoverflow.com/a/638064/417153 . 感谢@ davy-landmann for https://stackoverflow.com/a/638064/417153 。 That's what I was looking for! 这就是我在寻找的东西! Same effect with LESS code: 与LESS代码相同的效果:

  @searchResultMinHeight = 200px;.searchResult {min-height: @searchResultMinHeight;position: relative;.innerTrans {background: white;.opacity(0.5);min-height: @searchResultMinHeight;}.innerBody {padding: 0.5em;position: absolute;top: 0;}}

#4楼

Use rgba! 使用rgba!

.alpha60 {/* Fallback for web browsers that don't support RGBa */background-color: rgb(0, 0, 0);/* RGBa with 0.6 opacity */background-color: rgba(0, 0, 0, 0.6);/* For IE 5.5 - 7*/filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);/* For IE 8*/-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";
}

In addition to this, you have to declare background: transparent for IE web browsers, preferably served via conditional comments or similar! 除此之外,您必须为IE Web浏览器声明background: transparent ,最好通过条件注释或类似方式提供!

via http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/ 通过http://robertnyman.com/2010/01/11/css-background-transparency-without-affecting-child-elements-through-rgba-and-filters/


#5楼

I use an alpha-transparent PNG for that: 我使用alpha透明的PNG:

div.semi-transparent {background: url('semi-transparent.png');
}

For IE6, you'd need to use a PNG fix ( 1 , 2 ), though. 对于IE6,你需要用一个PNG修复( 1 , 2 ),虽然。


#6楼

I've created that effect on my blog Landman Code . 我在我的博客Landman Code上创建了这个效果。

What I did was 我做的是

 #Header { position: relative; } #Header H1 { font-size: 3em; color: #00FF00; margin:0; padding:0; } #Header H2 { font-size: 1.5em; color: #FFFF00; margin:0; padding:0; } #Header .Background { background: #557700; filter: alpha(opacity=30); filter: progid: DXImageTransform.Microsoft.Alpha(opacity=30); -moz-opacity: 0.30; opacity: 0.3; zoom: 1; } #Header .Background * { visibility: hidden; // hide the faded text } #Header .Foreground { position: absolute; // position on top of the background div left: 0; top: 0; } 
 <div id="Header"> <div class="Background"> <h1>Title</h1> <h2>Subtitle</h2> </div> <div class="Foreground"> <h1>Title</h1> <h2>Subtitle</h2> </div> </div> 

The important thing that every padding/margin and content must be the same in both the .Background as .Foreground. 重要的是每个填充/边距和内容在.Background和.Foreground中都必须相同。

背景颜色的不透明度,但不是文本[重复]相关推荐

  1. html超链接点击状态改变背景颜色,div css鼠标悬停锚文本超链接文字背景颜色或图片变化...

    css鼠标悬停超链接文字上时背景改变,鼠标经过文字链接时(悬停),文字的背景图片发生改变(出现背景图片或背景颜色). 鼠标经过悬停在超链接文字上时背景颜色或背景图片出现或改变教程篇. 此DIV CSS ...

  2. python不带颜色的图形_如何使用Matplotlib设置图形背景颜色的不透明度 - python

    我一直在玩Matplotlib,无法弄清楚如何更改图形的背景颜色,或者如何使背景完全透明. 参考方案 如果只希望图形和轴的整个背景都是透明的,则在使用transparent=True保存图形时,只需指 ...

  3. Java——文本框设置背景颜色、字体样式和颜色

    ┏(ω)=☞ 本专栏的目录(为您提供更好的查询方式)(点这里说不定有你想要的) 字体风格一共有四种,分别是 Font.PLAIN(普通) Font.BOLD(加粗) Font.ITALIC(斜体) F ...

  4. 如何利用python的opencv,在图片上添加一个有背景颜色的文本框,并在文本框内添加文字?

    可以使用OpenCV的cv2.putText()函数来实现. 示例代码: import cv2 读取图片 img = cv2.imread('image.jpg') 定义文本框的位置 x, y = 1 ...

  5. Bootstrap入门——情景文本颜色及背景颜色

    Bootstrap入门打卡1天:学习网址https://v3.bootcss.com/ [全局CSS样式]-[辅助类]关于情景文本颜色和背景颜色的设置 其中,情景文本颜色可以分为六种 -- 1.文本颜 ...

  6. ExtJS4.1.1 设置表格背景颜色 修改文本颜色 在表格中插入图片

    由于ExtJS版本不断更新,各种渲染方式也随之有所改变,目前大部分书籍还是停留在3版本,对于ExtJS4.1.1版本的表格渲染,设置表格行背景颜色的方式如下: 首先,定义行的样式: 1 .yellow ...

  7. BootStrap笔记-文本颜色链接颜色背景颜色

    代码如下: <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF- ...

  8. Windows API 关于控件的自绘——文本颜色、背景颜色、字体

    首先要将控件设置为可以自画,例如:要自画一个按钮,在窗口的WM_CREATE中创建类名为"BUTTON"的子窗口,风格必须包含BS_OWNERDRAW.然后在父窗口的WM_DRAW ...

  9. BootStrap4 文本颜色和背景颜色

    专门腾一篇颜色效果图出来了 文本颜色 背景颜色 文本颜色 <!-- BootStrap4 文字颜色 --><p class="text-muted">柔和的 ...

最新文章

  1. 树上启发式合并问题 ---- D. Tree and Queries[树上启发式合并+树状数组]
  2. 南方科技大学唐圆圆组招聘环境相关领域科研人才(年薪33~50万)
  3. 今天想到了关于 Django 中 view 的组织方式问题
  4. 运用Edraw为WPF应用程序嵌入Office文档的方法总结
  5. mac android studio 打不开adb,Android-Macbook ADB无法打开
  6. python求两数之和的命令_python计算两个数的百分比方法
  7. Spring Boot笔记-JPA中自定义@Query分页查询
  8. dell服务器读不到无线网卡,求助:Dell venue 11 pro 7130 ms 无线网卡无法驱动。找不到无线网卡。...
  9. mine 规范_模型规范要求
  10. cups支持的打印机列表_使用CUPS打印管理器管理打印机
  11. Android8怎么格式化内存卡,安卓手机怎么格式化存储内存 Android手机SD内存卡格式化方法...
  12. 学生奖学金评定系统c语言,学生奖学金评定系统.doc
  13. c语言中,偷机取巧,输出超市购物小票的票头
  14. 语雀实现收藏网页的功能(借助印象笔记)
  15. 3dmax基本物体放置操作1
  16. 月份, 星期的英语简写
  17. 卡苹果6plus在线_坐公交“神器”来了!在线充值、在线年检、信息查询...太方便了!...
  18. matlab 示波器模块,matlab示波器模块
  19. 主板电源开关接口图解_电脑主板上的电源开关插头怎么接啊
  20. ctrl c和 ctrl v 突然失灵不能使用的解决方法大全

热门文章

  1. Android开发之Buidler模式初探结合AlertDialog.Builder讲解
  2. Android开发六年收获阿里offer定级P7,多亏在阿里P8师兄给的攻略、面试技巧以及学习笔记!!!
  3. quadTree 论文Real-Time Generation of Continuous吃透了
  4. 算法----左叶子之和
  5. Android 对象池案例
  6. android adb 命令源码解析
  7. android Binary XML file line #1: Binary XML file line #1: Error inflating class x 问题详解
  8. Android的进程优先级
  9. android英文文献翻译,有关android技术英文文献翻译
  10. codeforces364D