英文:
Why isn't the div's background image showing up behind child elements in my React app?
问题
你想给一个 div
应用背景图像,但是该 div
的子元素不显示背景图像。图像只显示在空白区域。我正在使用 ReactJS。
以下是正在显示的内容:
以下是 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:
Here is the code for the div
<div className='start'>
<Navbar/>
<div className='firstContainer'>
<h3 className="subheading-div1">
Experience limitless charging possibilities with Charzit
</h3>
<h1 className="home-heading">
<span>EV Charging</span>
<span className="greenText"> Solutions </span>
<span>Where you like it </span>
</h1>
<p className="desc-div1">
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.
</p>
<h2 className="desc-div1">Download Now</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;
}
答案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("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;
}
<!-- language: lang-html -->
<div class='start'>
<div class='firstContainer'>
<h3 class="subheading-div1">
Experience limitless charging possibilities with Charzit
</h3>
<h1 class="home-heading">
<span>EV Charging</span>
<span class="greenText"> Solutions </span>
<span>Where you like it </span>
</h1>
<p class="desc-div1">
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.
</p>
<h2 class="desc-div1">Download Now</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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论