【问题标题】:CSS3 background opacity with background imageCSS3背景不透明度与背景图像
【发布时间】:2011-11-10 21:51:51
【问题描述】:

我正在尝试对应用了背景图像的 div 设置不透明效果,并且仍然保持文本“不透明”。 我的标记:

<div id="projects" class="feature">
    Projects
</div>

我的 CSS:

.feature {
    display: inline-block;
    margin: 30px;
    cursor: pointer;
    width: 300px;
    height: 300px;
    -webkit-border-radius: 150px;
    -moz-border-radius: 150px;
    border-radius: 150px;
    font-family: 'Droid Sans', Helvetica Neue, sans-serif;
    font-size: 2em;
    color: #fff;
    text-align: center;
    line-height: 1.2em;
}

.feature:after {
    /* 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.8);
    /* For IE 8*/
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";
}

.feature#projects {
    background-image: url('img/hero.png');
}

我想过使用伪元素,但没有成功!

有什么想法吗?

【问题讨论】:

    标签: css background-image opacity


    【解决方案1】:

    我可能会使用半透明的png 作为背景图片,然后相应地设置文本样式。

    您可能还想研究 RGBa:

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

    这适用于背景颜色,尽管它也可能适用于您的图像(但未经测试)

    【讨论】:

    • @benhowdle89...RGBa,对吗?半透明的png怎么样。不理想,但应该可以。
    【解决方案2】:

    有一种技巧,您可以将背景布置在另一个包含文本的 div 下方。

    查看以下链接:

    http://www.sumobaby.net/news/2011/03/04/change-background-opacity-without-affecting-text/

    还有一个简单的例子:

    http://jsfiddle.net/7pVGM/

    注意:标记和 css 不是很好,我只用了 5 分钟就写好了 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-11
      • 2016-09-06
      • 2011-10-16
      • 2013-11-30
      • 2013-07-05
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多