简体   繁体   English

如何更改div的背景图像?

[英]How to change background image of div?

This is my code:这是我的代码:

<div style = {{ backgroundImage: "url(img.jpg)" }} className="card">
    <h3>{character.name}</h3>
</div>

This code doesnt work.此代码不起作用。 Output just white color Output 只是白色

Try this:尝试这个:

<div style={{ backgroundImage: `url(${require("img.jpg")})` }} className="card">
    <h3>{character.name}</h3>
</div>

Providing you the generic solution which will work in any javascript frameworks.为您提供适用于任何 javascript 框架的通用解决方案。

HTML HTML

<div id="parentDiv" className="card">
    <h3>XYZ </h3>
</div>

JS JS

document.getElementByID('parentDiv').style.backgroundImage = "url('your_image_name.png')";

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

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