为什么在我的React应用中,div的背景图片没有显示在子元素的后面?

huangapple go评论100阅读模式
英文:

Why isn't the div's background image showing up behind child elements in my React app?

问题

你想给一个 div 应用背景图像,但是该 div 的子元素不显示背景图像。图像只显示在空白区域。我正在使用 ReactJS。

以下是正在显示的内容:

为什么在我的React应用中,div的背景图片没有显示在子元素的后面?

以下是 div 的代码:

  1. <div className='start'>
  2. <Navbar/>
  3. <div className='firstContainer'>
  4. <h3 className="subheading-div1">
  5. 通过Charzit体验无限的充电可能性
  6. </h3>
  7. <h1 className="home-heading">
  8. <span>电动汽车充电</span>
  9. <span className="greenText">解决方案</span>
  10. <span>您喜欢的地方</span>
  11. </h1>
  12. <p className="desc-div1">
  13. 我们的电动汽车充电解决方案具有适应性和高效性,
  14. 让企业满足对清洁能源交通不断增长的需求,
  15. 同时降低其二氧化碳排放。
  16. </p>
  17. <h2 className="desc-div1">立即下载</h2>
  18. <a href="/" target="_blank">
  19. <div className="btn-div">
  20. <FaGooglePlay className="btn" />
  21. </div>
  22. </a>
  23. <a href="/" target="_blank">
  24. <div className="btn-div">
  25. <FaApple className="btn" />
  26. </div>
  27. </a>
  28. </div>
  29. </div>

CSS

  1. .start {
  2. height: 100vh;
  3. background-image: url("../../public/main2.jpg");
  4. background-size: cover;
  5. background-size: 100%;
  6. background-repeat: no-repeat;
  7. }
  8. .firstContainer {
  9. padding-left: 50px;
  10. width: 50vw;
  11. z-index: -1;
  12. }
  13. .subheading-div1 {
  14. font-size: 1.7rem;
  15. margin: 0px;
  16. }
  17. .home-heading {
  18. font-size: 3rem;
  19. margin: 20px;
  20. margin-bottom: 0px;
  21. }
  22. .greenText {
  23. color: #79ca5d;
  24. }
  25. .desc-div1 {
  26. font-size: 1.4rem;
  27. font-weight: 700;
  28. margin-top: 20px;
  29. }
  30. .btn-div {
  31. background-color: transparent;
  32. display: inline-block;
  33. width: 68.46px;
  34. height: 51.86px;
  35. border-radius: 5px;
  36. margin: 20px 20px 0 0;
  37. border: 1px solid #79ca5d;
  38. }
  39. .btn-div:hover {
  40. background-color: #79ca5d;
  41. }
  42. .btn {
  43. color: #fff;
  44. padding-right: 20px;
  45. width: 45px;
  46. height: 45px;
  47. padding-left: 10px;
  48. padding-top: 5px;
  49. }
英文:

I want to apply a background image to a div, but the divs' child element does not show the background image behind it. The image is only shown in the blank space. I'm using ReactJS.

Here is what's showing up:

为什么在我的React应用中,div的背景图片没有显示在子元素的后面?

Here is the code for the div

  1. &lt;div className=&#39;start&#39;&gt;
  2. &lt;Navbar/&gt;
  3. &lt;div className=&#39;firstContainer&#39;&gt;
  4. &lt;h3 className=&quot;subheading-div1&quot;&gt;
  5. Experience limitless charging possibilities with Charzit
  6. &lt;/h3&gt;
  7. &lt;h1 className=&quot;home-heading&quot;&gt;
  8. &lt;span&gt;EV Charging&lt;/span&gt;
  9. &lt;span className=&quot;greenText&quot;&gt; Solutions &lt;/span&gt;
  10. &lt;span&gt;Where you like it &lt;/span&gt;
  11. &lt;/h1&gt;
  12. &lt;p className=&quot;desc-div1&quot;&gt;
  13. Our electric vehicle charging solutions are adaptable and efficient
  14. , allowing businesses to fulfill the rising demand for clean energy
  15. transportation while lowering their CO2 footprint.
  16. &lt;/p&gt;
  17. &lt;h2 className=&quot;desc-div1&quot;&gt;Download Now&lt;/h2&gt;
  18. &lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
  19. &lt;div className=&quot;btn-div&quot;&gt;
  20. &lt;FaGooglePlay className=&quot;btn&quot; /&gt;
  21. &lt;/div&gt;
  22. &lt;/a&gt;
  23. &lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
  24. &lt;div className=&quot;btn-div&quot;&gt;
  25. &lt;FaApple className=&quot;btn&quot; /&gt;
  26. &lt;/div&gt;
  27. &lt;/a&gt;
  28. &lt;/div&gt;
  29. &lt;/div&gt;

CSS

  1. .start {
  2. height: 100vh;
  3. background-image: url(&quot;../../public/main2.jpg&quot;);
  4. background-size: cover;
  5. background-size: 100%;
  6. background-repeat: no-repeat;
  7. }
  8. .firstContainer {
  9. padding-left: 50px;
  10. width: 50vw;
  11. z-index: -1;
  12. }
  13. .subheading-div1 {
  14. font-size: 1.7rem;
  15. margin: 0px;
  16. }
  17. .home-heading {
  18. font-size: 3rem;
  19. margin: 20px;
  20. margin-bottom: 0px;
  21. }
  22. .greenText {
  23. color: #79ca5d;
  24. }
  25. .desc-div1 {
  26. font-size: 1.4rem;
  27. font-weight: 700;
  28. margin-top: 20px;
  29. }
  30. .btn-div {
  31. background-color: transparent;
  32. display: inline-block;
  33. width: 68.46px;
  34. height: 51.86px;
  35. border-radius: 5px;
  36. margin: 20px 20px 0 0;
  37. border: 1px solid #79ca5d;
  38. }
  39. .btn-div:hover {
  40. background-color: #79ca5d;
  41. }
  42. .btn {
  43. color: #fff;
  44. padding-right: 20px;
  45. width: 45px;
  46. height: 45px;
  47. padding-left: 10px;
  48. padding-top: 5px;
  49. }

答案1

得分: 0

似乎您提出的问题无法重现。我已将它放在下面的片段中,它似乎以您想要的方式呈现。

我猜您需要将.firstContainer元素的背景设置为透明。您可能在某个地方设置了它的颜色。

如果您需要更多帮助,我建议您使用重现您问题的片段更新您的问题。如果StackOverflow片段无法工作,因为您需要JSX支持使其工作,您可以使用类似CodeSandbox的工具。

  1. .start {
  2. height: 100vh;
  3. background-image: url("https://kajabi-storefronts-production.kajabi-cdn.com/kajabi-storefronts-production/blogs/18725/images/PuRv7TFRzGdNsmp3iRmj_Tetons.jpg");
  4. background-size: cover;
  5. background-size: 100%;
  6. background-repeat: no-repeat;
  7. }
  8. .firstContainer {
  9. padding-left: 50px;
  10. width: 50vw;
  11. z-index: -1;
  12. }
  13. .subheading-div1 {
  14. font-size: 1.7rem;
  15. margin: 0px;
  16. }
  17. .home-heading {
  18. font-size: 3rem;
  19. margin: 20px;
  20. margin-bottom: 0px;
  21. }
  22. .greenText {
  23. color: #79ca5d;
  24. }
  25. .desc-div1 {
  26. font-size: 1.4rem;
  27. font-weight: 700;
  28. margin-top: 20px;
  29. }
  30. .btn-div {
  31. background-color: transparent;
  32. display: inline-block;
  33. width: 68.46px;
  34. height: 51.86px;
  35. border-radius: 5px;
  36. margin: 20px 20px 0 0;
  37. border: 1px solid #79ca5d;
  38. }
  39. .btn-div:hover {
  40. background-color: #79ca5d;
  41. }
  42. .btn {
  43. color: #fff;
  44. padding-right: 20px;
  45. width: 45px;
  46. height: 45px;
  47. padding-left: 10px;
  48. padding-top: 5px;
  49. }
  1. <div class='start'>
  2. <div class='firstContainer'>
  3. <h3 class="subheading-div1">
  4. 使用Charzit体验无限充电可能性
  5. </h3>
  6. <h1 class="home-heading">
  7. <span>电动汽车充电</span>
  8. <span class="greenText">解决方案</span>
  9. <span>您喜欢的地方</span>
  10. </h1>
  11. <p class="desc-div1">
  12. 我们的电动车充电解决方案是可适应和高效的,允许企业满足对清洁能源交通不断增长的需求,同时降低其二氧化碳排放。
  13. </p>
  14. <h2 class="desc-div1">立即下载</h2>
  15. <a href="/" target="_blank">
  16. <div class="btn-div">
  17. <FaGooglePlay class="btn" />
  18. </div>
  19. </a>
  20. <a href="/" target="_blank">
  21. <div class="btn-div">
  22. <FaApple class="btn" />
  23. </div>
  24. </a>
  25. </div>
  26. </div>
英文:

It seems your issue is not reproducible as you have presented it. I've placed it in a snippet below, and it appears to be rendering the way you want.

I would guess that you need to set the background of your .firstContainer element to be transparent. You might have a style somewhere giving it a color.

If you need more help, I recommend updating your question with a snippet that reproduces your issue. If the StackOverflow snippet doesn't work because you require JSX support to make it work, you can use something like CodeSandbox.

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-css -->

  1. .start {
  2. height: 100vh;
  3. background-image: url(&quot;https://kajabi-storefronts-production.kajabi-cdn.com/kajabi-storefronts-production/blogs/18725/images/PuRv7TFRzGdNsmp3iRmj_Tetons.jpg&quot;);
  4. background-size: cover;
  5. background-size: 100%;
  6. background-repeat: no-repeat;
  7. }
  8. .firstContainer {
  9. padding-left: 50px;
  10. width: 50vw;
  11. z-index: -1;
  12. }
  13. .subheading-div1 {
  14. font-size: 1.7rem;
  15. margin: 0px;
  16. }
  17. .home-heading {
  18. font-size: 3rem;
  19. margin: 20px;
  20. margin-bottom: 0px;
  21. }
  22. .greenText {
  23. color: #79ca5d;
  24. }
  25. .desc-div1 {
  26. font-size: 1.4rem;
  27. font-weight: 700;
  28. margin-top: 20px;
  29. }
  30. .btn-div {
  31. background-color: transparent;
  32. display: inline-block;
  33. width: 68.46px;
  34. height: 51.86px;
  35. border-radius: 5px;
  36. margin: 20px 20px 0 0;
  37. border: 1px solid #79ca5d;
  38. }
  39. .btn-div:hover {
  40. background-color: #79ca5d;
  41. }
  42. .btn {
  43. color: #fff;
  44. padding-right: 20px;
  45. width: 45px;
  46. height: 45px;
  47. padding-left: 10px;
  48. padding-top: 5px;
  49. }

<!-- language: lang-html -->

  1. &lt;div class=&#39;start&#39;&gt;
  2. &lt;div class=&#39;firstContainer&#39;&gt;
  3. &lt;h3 class=&quot;subheading-div1&quot;&gt;
  4. Experience limitless charging possibilities with Charzit
  5. &lt;/h3&gt;
  6. &lt;h1 class=&quot;home-heading&quot;&gt;
  7. &lt;span&gt;EV Charging&lt;/span&gt;
  8. &lt;span class=&quot;greenText&quot;&gt; Solutions &lt;/span&gt;
  9. &lt;span&gt;Where you like it &lt;/span&gt;
  10. &lt;/h1&gt;
  11. &lt;p class=&quot;desc-div1&quot;&gt;
  12. Our electric vehicle charging solutions are adaptable and efficient
  13. , allowing businesses to fulfill the rising demand for clean energy
  14. transportation while lowering their CO2 footprint.
  15. &lt;/p&gt;
  16. &lt;h2 class=&quot;desc-div1&quot;&gt;Download Now&lt;/h2&gt;
  17. &lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
  18. &lt;div class=&quot;btn-div&quot;&gt;
  19. &lt;FaGooglePlay class=&quot;btn&quot; /&gt;
  20. &lt;/div&gt;
  21. &lt;/a&gt;
  22. &lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
  23. &lt;div class=&quot;btn-div&quot;&gt;
  24. &lt;FaApple class=&quot;btn&quot; /&gt;
  25. &lt;/div&gt;
  26. &lt;/a&gt;
  27. &lt;/div&gt;
  28. &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 03:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定