html - 将一个 DIV 层叠在另一个之上,以提供具有清晰文本的透明背景

标签 html css

我想要做的是有一个不影响文本的透明背景的div。考虑以下 HTML:

<section class="content">
    <header>
        <h1>Description</h1>
    </header>
    Code
</section>

如果我给它以下 CSS:

background-color: #7ac0da;
opacity: 0.5;
filter: alpha(opacity=50);

文本会受到 部分 透明度的影响。所以,我开始尝试像这样对内容进行分层:

<div class="code-sample">
    <div class="background"></div>
    <section class="content">
        <header>
            <h1>Description</h1>
        </header>
        Code
    </section>
</div>

但是,通过无数次迭代,我无法让 section 覆盖 div。老实说,我试过定位内部 divsection 绝对和相对。我试过使用 z-index。但实际上,我只是在黑暗中拍摄。我希望 .background 看起来透明:

background-color: #7ac0da;
opacity: 0.5;
filter: alpha(opacity=50);

但是 .content 然后会覆盖 div。这将允许我随后 float .code-sample div 并使用它们进行三列布局。

我怎样才能实现我正在寻找的东西?

最佳答案

使用 RGB 颜色只设置背景的透明度:

.class {    
   /* 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)";
}

Source

关于html - 将一个 DIV 层叠在另一个之上,以提供具有清晰文本的透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17657234/

相关文章:

javascript - Selenium 和 PhantomJS 不解析 Javascript

jquery - 使用 jQuery 计算元素的宽度

html - 如何在相关类 css 中设置最小高度?

JQuery 自动完成 : changing look and feel

javascript - CSS 不会出现在 HTML 和 Node 中

HTML 注释标记

javascript - 在 Firefox 中悬停时下拉值会发生变化

javascript - 如何在 meteor 中获取表单值?

css - Safari 和 Chrome 中的 Stick Footer 问题

html - Bootstrap navbar-fixed-top 类,不工作