反应背景图像覆盖不透明度

aas*_*han 4 css stylesheet reactjs

没有找到如何在reactjs中设置背景图像叠加不透明度的方法,我正在尝试做类似的事情。

风格

const styles={ 
 header:{ 
 background: 'rgba(0, 0, 0, 0.5)', 
 backgroundImage:url(${background}),
 height: '100vh', 
 backgroundPosition:'center', 
 backgroundRepeat: 'no-repeat',
 backgroundSize: 'cover' 
 }
}
Run Code Online (Sandbox Code Playgroud)

JSX

<div style={styles.header}>
      <div>
        Portfolio
      </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Den*_*sur 5

您应该将样式添加到内部块并在那里设置背景颜色。另外,我建议您使用字符串模板来为样式设置有效值backgroundImage。

我们到了:

const styles = {
  header: {
    backgroundImage: `url(${background})`,
    height: '100vh',
    backgroundPosition: 'center',
    backgroundRepeat: 'no-repeat',
    backgroundSize: 'cover'
  },

  content: {
    height: '100%',
    width: '100%',
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
  }
}

<div style={styles.header}>
  <div style={styles.content}>
     Portfolio
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

示例代码在这里。