当叠加在图像上时,使文本显示为白色并具有半透明黑色背景

发布于 2024-11-27 23:03:56 字数 1073 浏览 1 评论 0原文

我有一个简单的 CSS/HTMl 问题。我在 div 中有一张图像和一些文本。我使用 z 索引将文本放置在图像顶部。

文本为白色,黑色背景。我调整了文本 div 的不透明度,以便其下方的图像可见。看起来不错。

问题是文本显示为灰色而不是白色,因为不透明度降低了。如何使文本显示为白色,并且周围仍然有半透明的黑色背景?

 <style type="text/css">

        .wrap {
          position:relative;
          float:left;
          clear:none;
          overflow:hidden;
        }

        .wrap img {
          position:relative;
          z-index:1;
        }

        .wrap .desc {
          display:block;
      position:absolute;
      width:166px;
      top:20px;
      left:20px;
      z-index:2;
      color: #FFFFFF;
      padding: 10px;
      background-color: #000000;
      border-radius: 5px 5px 5px 5px;
      -moz-border-radius: 5px 5px 5px 5px;
      -webkit-border-radius: 5px 5px 5px 5px;

     /*For IE*/
      filter: alpha(opacity=60);
      opacity: 0.60;

        }
    </style>

    <div class="wrap">
        <img src="path/to/pic.png" />
        <h6 class="desc">This is my text about my image</h3>

    </div>

有什么建议吗?

I've got a simple CSS/HTMl question. I've got an image and some text in a div. I've got the text positioned on top of the image using the z-index.

The text is white with a black background. I adjusted the text's div's opacity, so that the image beneath it is visible. It looks good.

The problem is that the text appears gray instead of white, because the opacity is lowered. How can I make the text appear white, and still have a semi-transparent black background around it?

 <style type="text/css">

        .wrap {
          position:relative;
          float:left;
          clear:none;
          overflow:hidden;
        }

        .wrap img {
          position:relative;
          z-index:1;
        }

        .wrap .desc {
          display:block;
      position:absolute;
      width:166px;
      top:20px;
      left:20px;
      z-index:2;
      color: #FFFFFF;
      padding: 10px;
      background-color: #000000;
      border-radius: 5px 5px 5px 5px;
      -moz-border-radius: 5px 5px 5px 5px;
      -webkit-border-radius: 5px 5px 5px 5px;

     /*For IE*/
      filter: alpha(opacity=60);
      opacity: 0.60;

        }
    </style>

    <div class="wrap">
        <img src="path/to/pic.png" />
        <h6 class="desc">This is my text about my image</h3>

    </div>

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那一片橙海, 2024-12-04 23:03:56

像这样怎么样:

CSS

.mod {
  position: relative;
  width: 80px;
  height: 100px;
  padding: 5px;
}
.mod-text,
.mod-background {
  position: absolute;
  left: 0;
  width: 100%;
}
.mod-text {
  color: #FFF;
  font-size: 1em;
  text-align: center;
  bottom: 0;
}
.mod-background {
  background-color: #f58322;
  border-radius: 8px;
  filter: alpha(opacity=60);
  opacity: 0.60;
  top: 0;
  height: 100%;
}

HTML

<div class="mod">
  <img src="http://www.gravatar.com/avatar/d543f6789b58df56f6fed95291e78261.png" />
  <div class="mod-background">
     
  </div>
  <div class="mod-text">
    Hawt!
  </div>
</div>

Plnkr

http://plnkr.co/edit/aSd9rO?p=preview

How about like this:

CSS

.mod {
  position: relative;
  width: 80px;
  height: 100px;
  padding: 5px;
}
.mod-text,
.mod-background {
  position: absolute;
  left: 0;
  width: 100%;
}
.mod-text {
  color: #FFF;
  font-size: 1em;
  text-align: center;
  bottom: 0;
}
.mod-background {
  background-color: #f58322;
  border-radius: 8px;
  filter: alpha(opacity=60);
  opacity: 0.60;
  top: 0;
  height: 100%;
}

HTML

<div class="mod">
  <img src="http://www.gravatar.com/avatar/d543f6789b58df56f6fed95291e78261.png" />
  <div class="mod-background">
     
  </div>
  <div class="mod-text">
    Hawt!
  </div>
</div>

Plnkr

http://plnkr.co/edit/aSd9rO?p=preview

眼泪也成诗 2024-12-04 23:03:56

根据您的浏览器支持要求,您可能可以将不透明度保留为 100%,并使用 rgba 颜色:

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

颜色为红色、绿色、蓝色(每种颜色为 0-255)通过阿尔法(0-1.0)。

如果您需要在旧版浏览器中进行回退,通常可以使用:

background-color: #000;
background-color: rgba(0,0,0,0.5);

对于旧版浏览器,这将默认为黑色,对于新版浏览器,默认为半透明。它避免了额外的下载(平铺图像),并在文本文件中保留更多样式(更易于版本控制、维护和查找)。

Depending on your browser support requirements, you might be able to get away with leaving opacity at 100%, and using an rgba color:

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

The colors are Red, Green, Blue, (0-255 each) followed by Alpha (0-1.0).

If you need a fallback in older browsers, you can usually use:

background-color: #000;
background-color: rgba(0,0,0,0.5);

This will default to Black for older browsers, and semi-transparent for newer ones. It avoids an extra download (of a tiling image), as well as keeping more of your styling in the text file (easier to version, maintain, and find).

人海汹涌 2024-12-04 23:03:56

我会在描述之前创建另一个具有相同高度和宽度的 div,将该 div 的不透明度设置为透明,添加背景,然后将描述放在另一个没有背景的 div 中。如果两者都具有绝对地位,那么后者应该排在前者之上。

请参阅此处的演示

I would create another div before the description with the same height and width, set that div's opacity to transparent, add a background, then put the description in another div, without a background. If they both have absolute position, then the latter should go on top of the former.

See the demo here.

路弥 2024-12-04 23:03:56

您可以在元素的背景中放置半透明图像。实际图像可能非常小,您可以重复它以覆盖整个背景。

.wrap .desc {
      display: block;
      position: absolute;
      width: 166px;
      top: 20px;
      left: 20px;
      z-index: 2;
      color: #FFFFFF;
      padding: 10px;
      background: url('my-small-bg.png');
      border-radius: 5px;
      -moz-border-radius: 5px;
      -webkit-border-radius: 5px;
}

下面是一个示例: http://jsfiddle.net/f6XS6/1/

You can put a semi-transparent image in the background of the element instead. The actual image can be very small and you can repeat it to cover the whole background.

.wrap .desc {
      display: block;
      position: absolute;
      width: 166px;
      top: 20px;
      left: 20px;
      z-index: 2;
      color: #FFFFFF;
      padding: 10px;
      background: url('my-small-bg.png');
      border-radius: 5px;
      -moz-border-radius: 5px;
      -webkit-border-radius: 5px;
}

Here's an example of what this could look like: http://jsfiddle.net/f6XS6/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文