简体   繁体   English

在变暗的图像上有明亮的文字

[英]Have bright text over a darkened image

I have the following CSS component:我有以下 CSS 组件:

<div className="dimmed-image" style={itemStyle}>
   <div className="bright-text">
      <br/>
      <h2 className="white-center">Some text</h2>
   </div>
</div>

The itemStyle variable references a background image. itemStyle变量引用背景图像。

var itemStyle = {
  backgroundImage: 'url(' + imageLocation + ')'
};

The css classes are these: css类是这些:

.dimmed-image {
    -webkit-filter: brightness(60%);
}

.bright-text {
    -webkit-filter: brightness(100%) !important;
}

I want to have white text over an image which is darkened.我想在变暗的图像上添加白色文本。

However, if I use the code above, the text will also be darkened.但是,如果我使用上面的代码,文本也会变暗。

If I make two separate div classes like this:如果我像这样制作两个单独的div类:

<div className="dimmed-image" style={itemStyle}>
</div>
<div className="bright-text">
  <br/>
  <h2 className="white-center">Some text</h2>
</div>

Then the text will be placed beneath the image.然后文本将被放置在图像下方。

How can I have bright text on top of a darkened image?如何在变暗的图像上添加明亮的文字?

To make the text jump back to the top of the image just apply position: absolute to the .dimmed-image :要使文本跳回到图像的顶部,只需将position: absolute.dimmed-image

.dimmed-image {
  ...
  position: absolute;
}

This will take the image outside the normal rendering and as long as you don't specify any other location will make it stay at the same place.这会将图像置于正常渲染之外,只要您不指定任何其他位置,它就会保持在同一位置。 Meanwhile the elements below (eg the text) will ignore the image and be rendered starting from the same point where the image starts.同时,下面的元素(例如文本)将忽略图像并从图像开始的同一点开始渲染。

Place the dimmed image and the text in separate divs and wrap them together in a container div.将变暗的图像和文本放在单独的 div 中,并将它们包装在一个容器 div 中。

Give the container div the same width and height as the one containing the image.为容器 div 提供与包含图像的 div 相同的宽度和高度。 Do the same to the bright-text div as well.bright-text div 也做同样的事情。 Use position to place your text above the image.使用position将文本放置在图像上方。

That is the best approach.这是最好的方法。

Also className is invalid.另外className无效。 Use class instead.改用class

HTML: HTML:

<div class="dimmed-image-wrapper">
  <div class="dimmed-image" style={itemStyle}>
  </div>
  <div class="bright-text">
    <br/>
    <h2 class="white-center">Some text</h2>
  </div>
</div>

CSS CSS

.dimmed-image {
    -webkit-filter: brightness(60%);
}

.dimmed-image-wrapper {
  position: relative;
  width: [same-as-dimmed-image]px;
  height: [same-as-dimmed-image]px; 
}

.bright-text {
  position: absolute;
  left: 0;
  top: 0;
  width: [same-as-dimmed-image]px;
  height: [same-as-dimmed-image]px; 
}

Inside the .bright-text div you can also use position , left and top css rules to move around your text, if you wish..bright-text div 中,如果您愿意,您还可以使用positionlefttop css 规则来移动您的文本。

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

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