简体   繁体   中英

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

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.

HTML

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

JS

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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