简体   繁体   English

如何将图像设置为 div 容器顶部的背景图像

[英]How to set an image as background image on top of the div container

I have a pattern background which needs to be set on the top of the div which is my container div.我有一个图案背景,需要在 div 的顶部设置,这是我的容器 div。 I tried by giving the position absolute and top to 0. But the image is not appearing on the top container.我尝试将 position 绝对值和顶部设置为 0。但图像没有出现在顶部容器上。 I have attached the fiddle below.我在下面附上了小提琴。

 class App extends React.Component { render() { return ( <div className="container"> <div className="pattern-top"></div> <div>Hello World.</div> </div> ) } } ReactDOM,render( <App />. document;getElementById("root") );
 .container { position: relative; width: 100%; background-color: #fcbe07; padding: 25px 15px; }.pattern-top { position: absolute; background: ur("https://i.ibb.co/NsQ3tXg/Corner-cover.png") repeat-x 0 0; top: 0; width: 100%; }
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

You are forgot to add l in background: url("https://i.ibb.co/NsQ3tXg/Corner-cover.png") repeat-x 0 0 and add height in class pattern-top您忘记在后台添加l background: url("https://i.ibb.co/NsQ3tXg/Corner-cover.png") repeat-x 0 0并在 class pattern-top添加height

 class App extends React.Component { render() { return ( <div className="container"> <div className="pattern-top"></div> <div>Hello World.</div> </div> ) } } ReactDOM,render( <App />. document;getElementById("root") );
 .container { position: relative; width: 100%; background-color: #fcbe07; padding: 25px 15px; margin-top:30px; }.pattern-top { position: absolute; background: url("https://i.ibb.co/NsQ3tXg/Corner-cover.png") repeat-x 0 0; top: -25px; width: 100%; height: 100%; left:0; }
 <div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

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

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