简体   繁体   English

如何在反应中设置背景图像内联样式的高度和宽度

[英]How to set height and width of background image inline style in react

can someone please help me out with this ?有人可以帮我解决这个问题吗?

I have this problem: I am fetching an image from api and I want to set it as background (FULL-SCREEN background), somehow it doesn´t work for me when I want to set 100% height and width and other stylings to that image as inline styling.我有这个问题:我正在从 api 获取图像,我想将它设置为背景(全屏背景),当我想设置 100% 高度和宽度以及其他样式时,不知何故它对我不起作用图像作为内联样式。 How am I supposed to do that ?我该怎么做?

Or is it possible to fetch an image and somehow use it in css as background-image: ... ?或者是否可以获取图像并以某种方式在 css 中将其用作 background-image: ... ?

I am fetching an image from django back end.我正在从 django 后端获取图像。

render() {
        return (
        <section style={{backgroundImage: `url(${this.state.pic})`}}>
        <h3>404 Page Not Found!</h3>
        </section>
         )
    
    }
  1. Give your section a classname.给你的部分一个类名。

  2. Create a new CSS-styling sheet.创建一个新的 CSS 样式表。 Like "style.css" and link it to your react app.像“style.css”并将其链接到您的反应应用程序。

  3. Do the following请执行下列操作

    render() { return ( <section className="mySection" style={{backgroundImage: `url(${this.state.pic})`}}> <h3>404 Page Not Found!</h3> </section> ) } //This goes in your style.css .mySection{ background-position: center; background-repeat: no-repeat; background-size: cover; width: 100%; heigth: 100%; } h3{ z-index: 99; }

I don't know about styling elements.我不知道样式元素。 This may help you!.这可能对你有帮助!。 You can refer here!你可以参考这里! https://reactjs.org/docs/dom-elements.html#style https://reactjs.org/docs/dom-elements.html#style

render() {
  return (
    <section style={{
      backgroundImage: `url(${this.state.pic})`,
      backgroundPosition: center,
      backgroundRepeat: no - repeat,
      backgroundSize: cover
    }}>
      <h3>404 Page Not Found!</h3>
    </section >
  )
}

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

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