0

我正在使用gatsby-image并尝试在图像上添加暗渐变,但它没有按预期工作。

这是我的代码:

 <Img
    sx={{
      height: "70vh",
      background: "linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5))",
    }}
    fluid={data.tos.childImageSharp.fluid}
  />
4

2 回答 2

2

尝试使用 Gatsby 背景图像并将不透明度添加到 1 !important 就像这样

import BackgroundImage from 'gatsby-background-image';
const HeroBackgroundImage = styled(BackgroundImage)`
      width: 100%;
      height: 100vh;
      opacity: 1 !important;
      background-size: cover;
      background: linear-gradient(
        90.26deg,
        rgba(3, 113, 227, 0.9) 17.75%,
        rgba(238, 169, 64, 0.61) 99.89%
      );
      background-size: cover;
    `;
于 2020-04-11T18:46:32.970 回答
1

解决方案 1 - 使用 gatsby-image

您可以使用以下方法解决此问题position: relative/absolute

import Img from "gatsby-image"

const Wrapper = styled.div`
  position: relative;
  /* width, height */
`

const Gradient = styled.div`
  position: absolute;
  inset: 0 0 0 0;
  background: linear-gradient(/* your linear-gradient */);
`

const Text = styled.p`
  position: absolute;
  inset: /* position */
`

const Component = () => {
  return(
    <Wrapper>
      <Img fluid={fluid} />
      <Gradient />
      <Text>Gradient over image, text over gradient</Text>
    </Wrapper>
  )
}

或者,如上所述:

解决方案 2 - 使用 gatsby-background-image

对我有用的是将 a<div>放在顶部<BackgroundImage>,然后设置background: linear-gradient()在上面<div>

import BackgroundImage from "gatsby-background-image"

const Wrapper = styled.div`
  width: /* your width */;
`
const Gradient = styled.div`
  height: /* your height */;
  background: linear-gradient(/* your linear-gradient */);
`

const Component = () => {
  return(
    <Wrapper>
      <BackgroundImage ...>
        <Gradient>
          <p>Gradient over image, text over gradient</p>
        </Gradient>
      <BackgroundImage>
    </Wrapper>
  )
}
于 2021-01-29T18:20:15.037 回答