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

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

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

问题

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

以下是正在显示的内容:

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

以下是 div 的代码:

 
<div className='start'>
	<Navbar/>
	<div className='firstContainer'>
		<h3 className="subheading-div1">
            通过Charzit体验无限的充电可能性
          </h3>
		<h1 className="home-heading">
			<span>电动汽车充电</span>
			<span className="greenText">解决方案</span>
			<span>您喜欢的地方</span>
		</h1>
		<p className="desc-div1">
            我们的电动汽车充电解决方案具有适应性和高效性,
            让企业满足对清洁能源交通不断增长的需求,
            同时降低其二氧化碳排放。
          </p>
		<h2 className="desc-div1">立即下载</h2>
		<a href="/" target="_blank">
			<div className="btn-div">
				<FaGooglePlay className="btn" />
			</div>
		</a>
		<a href="/" target="_blank">
			<div className="btn-div">
				<FaApple className="btn" />
			</div>
		</a>
	</div>
</div>

CSS

.start {
    height: 100vh;
    background-image: url("../../public/main2.jpg");
    background-size: cover;
    background-size: 100%;
    background-repeat: no-repeat;
}

.firstContainer {
    padding-left: 50px;
    width: 50vw;
    z-index: -1;
}

.subheading-div1 {
    font-size: 1.7rem;
    margin: 0px;
}

.home-heading {
    font-size: 3rem;
    margin: 20px;
    margin-bottom: 0px;
}

.greenText {
    color: #79ca5d;
}

.desc-div1 {
    font-size: 1.4rem;
    font-weight: 700;
    margin-top: 20px;
}

.btn-div {
    background-color: transparent;
    display: inline-block;
    width: 68.46px;
    height: 51.86px;
    border-radius: 5px;
    margin: 20px 20px 0 0;
    border: 1px solid #79ca5d;
}

.btn-div:hover {
    background-color: #79ca5d;
}

.btn {
    color: #fff;
    padding-right: 20px;
    width: 45px;
    height: 45px;
    padding-left: 10px;
    padding-top: 5px;
}
英文:

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

 
&lt;div className=&#39;start&#39;&gt;
	&lt;Navbar/&gt;
	&lt;div className=&#39;firstContainer&#39;&gt;
		&lt;h3 className=&quot;subheading-div1&quot;&gt;
            Experience limitless charging possibilities with Charzit
          &lt;/h3&gt;
		&lt;h1 className=&quot;home-heading&quot;&gt;
			&lt;span&gt;EV Charging&lt;/span&gt;
			&lt;span className=&quot;greenText&quot;&gt; Solutions &lt;/span&gt;
			&lt;span&gt;Where you like it &lt;/span&gt;
		&lt;/h1&gt;
		&lt;p className=&quot;desc-div1&quot;&gt;
            Our electric vehicle charging solutions are adaptable and efficient
            , allowing businesses to fulfill the rising demand for clean energy
            transportation while lowering their CO2 footprint.
          &lt;/p&gt;
		&lt;h2 className=&quot;desc-div1&quot;&gt;Download Now&lt;/h2&gt;
		&lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
			&lt;div className=&quot;btn-div&quot;&gt;
				&lt;FaGooglePlay className=&quot;btn&quot; /&gt;
			&lt;/div&gt;
		&lt;/a&gt;
		&lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
			&lt;div className=&quot;btn-div&quot;&gt;
				&lt;FaApple className=&quot;btn&quot; /&gt;
			&lt;/div&gt;
		&lt;/a&gt;
	&lt;/div&gt;
&lt;/div&gt;

CSS

.start {
    height: 100vh;
    background-image: url(&quot;../../public/main2.jpg&quot;);
    background-size: cover;
    background-size: 100%;
    background-repeat: no-repeat;
}

.firstContainer {
    padding-left: 50px;
    width: 50vw;
    z-index: -1;
}

.subheading-div1 {
    font-size: 1.7rem;
    margin: 0px;
}

.home-heading {
    font-size: 3rem;
    margin: 20px;
    margin-bottom: 0px;
}

.greenText {
    color: #79ca5d;
}

.desc-div1 {
    font-size: 1.4rem;
    font-weight: 700;
    margin-top: 20px;
}

.btn-div {
    background-color: transparent;
    display: inline-block;
    width: 68.46px;
    height: 51.86px;
    border-radius: 5px;
    margin: 20px 20px 0 0;
    border: 1px solid #79ca5d;
}

.btn-div:hover {
    background-color: #79ca5d;
}

.btn {
    color: #fff;
    padding-right: 20px;
    width: 45px;
    height: 45px;
    padding-left: 10px;
    padding-top: 5px;
}

答案1

得分: 0

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

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

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

.start {
    height: 100vh;
    background-image: url("https://kajabi-storefronts-production.kajabi-cdn.com/kajabi-storefronts-production/blogs/18725/images/PuRv7TFRzGdNsmp3iRmj_Tetons.jpg");
    background-size: cover;
    background-size: 100%;
    background-repeat: no-repeat;
}

.firstContainer {
    padding-left: 50px;
    width: 50vw;
    z-index: -1;
}

.subheading-div1 {
    font-size: 1.7rem;
    margin: 0px;
}

.home-heading {
    font-size: 3rem;
    margin: 20px;
    margin-bottom: 0px;
}

.greenText {
    color: #79ca5d;
}

.desc-div1 {
    font-size: 1.4rem;
    font-weight: 700;
    margin-top: 20px;
}

.btn-div {
    background-color: transparent;
    display: inline-block;
    width: 68.46px;
    height: 51.86px;
    border-radius: 5px;
    margin: 20px 20px 0 0;
    border: 1px solid #79ca5d;
}

.btn-div:hover {
    background-color: #79ca5d;
}

.btn {
    color: #fff;
    padding-right: 20px;
    width: 45px;
    height: 45px;
    padding-left: 10px;
    padding-top: 5px;
}
<div class='start'>
    <div class='firstContainer'>
        <h3 class="subheading-div1">
            使用Charzit体验无限充电可能性
        </h3>
        <h1 class="home-heading">
            <span>电动汽车充电</span>
            <span class="greenText">解决方案</span>
            <span>您喜欢的地方</span>
        </h1>
        <p class="desc-div1">
            我们的电动车充电解决方案是可适应和高效的,允许企业满足对清洁能源交通不断增长的需求,同时降低其二氧化碳排放。
        </p>
        <h2 class="desc-div1">立即下载</h2>
        <a href="/" target="_blank">
            <div class="btn-div">
                <FaGooglePlay class="btn" />
            </div>
        </a>
        <a href="/" target="_blank">
            <div class="btn-div">
                <FaApple class="btn" />
            </div>
        </a>
    </div>
</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 -->

.start {
    height: 100vh;
    background-image: url(&quot;https://kajabi-storefronts-production.kajabi-cdn.com/kajabi-storefronts-production/blogs/18725/images/PuRv7TFRzGdNsmp3iRmj_Tetons.jpg&quot;);
    background-size: cover;
    background-size: 100%;
    background-repeat: no-repeat;
}

.firstContainer {
    padding-left: 50px;
    width: 50vw;
    z-index: -1;
}

.subheading-div1 {
    font-size: 1.7rem;
    margin: 0px;
}

.home-heading {
    font-size: 3rem;
    margin: 20px;
    margin-bottom: 0px;
}

.greenText {
    color: #79ca5d;
}

.desc-div1 {
    font-size: 1.4rem;
    font-weight: 700;
    margin-top: 20px;
}

.btn-div {
    background-color: transparent;
    display: inline-block;
    width: 68.46px;
    height: 51.86px;
    border-radius: 5px;
    margin: 20px 20px 0 0;
    border: 1px solid #79ca5d;
}

.btn-div:hover {
    background-color: #79ca5d;
}

.btn {
    color: #fff;
    padding-right: 20px;
    width: 45px;
    height: 45px;
    padding-left: 10px;
    padding-top: 5px;
}

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

&lt;div class=&#39;start&#39;&gt;
    &lt;div class=&#39;firstContainer&#39;&gt;
        &lt;h3 class=&quot;subheading-div1&quot;&gt;
            Experience limitless charging possibilities with Charzit
          &lt;/h3&gt;
        &lt;h1 class=&quot;home-heading&quot;&gt;
            &lt;span&gt;EV Charging&lt;/span&gt;
            &lt;span class=&quot;greenText&quot;&gt; Solutions &lt;/span&gt;
            &lt;span&gt;Where you like it &lt;/span&gt;
        &lt;/h1&gt;
        &lt;p class=&quot;desc-div1&quot;&gt;
            Our electric vehicle charging solutions are adaptable and efficient
            , allowing businesses to fulfill the rising demand for clean energy
            transportation while lowering their CO2 footprint.
          &lt;/p&gt;
        &lt;h2 class=&quot;desc-div1&quot;&gt;Download Now&lt;/h2&gt;
        &lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
            &lt;div class=&quot;btn-div&quot;&gt;
                &lt;FaGooglePlay class=&quot;btn&quot; /&gt;
            &lt;/div&gt;
        &lt;/a&gt;
        &lt;a href=&quot;/&quot; target=&quot;_blank&quot;&gt;
            &lt;div class=&quot;btn-div&quot;&gt;
                &lt;FaApple class=&quot;btn&quot; /&gt;
            &lt;/div&gt;
        &lt;/a&gt;
    &lt;/div&gt;
&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:

确定