简体   繁体   English

我如何将背景图像应用于仅 ONE Component React 的主体

[英]How i can apply a background image to the body of only ONE Component React

I have a main component on every page, which is Navbar, and I have another component, which is a page to contact us.我在每个页面上都有一个主要组件,即导航栏,我还有另一个组件,即联系我们的页面。 I want to make a background for a component that contact us, but it does not affect the Navbar?我想给一个联系我们的组件做背景,但是不影响Navbar? Also, if I give body tag the background image, all the other components will take the same background as the photo cuz i give it to the body?另外,如果我给身体标签背景图像,所有其他组件将采用与我给身体的照片相同的背景? what i can do in this case?在这种情况下我能做什么?

You have to wrap each component in a different div.您必须将每个组件包装在不同的 div 中。

<div>
 Navbar
</div>

<div style={{backgroundColor: 'color here'}}>
 Contact us
</div>

Style inside the div or add a className and style in a separate file with CSS:在 div 中设置样式,或者使用 CSS 在单独的文件中添加类名和样式:

<div className="yourClass">
 Contact us
</>

and in a separate.css file:并在一个单独的.css 文件中:

.yourClass {
 background-color: 'color name';
}

Simple HTML question, just apply the class to only one of your elements, and don't have them nested.简单的 HTML 问题,只需将 class 应用于您的一个元素,并且不要嵌套它们。

<containing-element>
  <navbar />
  <contact-page className="contact" />
</containing-element>

You can then style .contact然后,您可以.contact样式

.contact {
  background-color: #FF0000;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM