/    Sign up×
Community /Pin to ProfileBookmark

I’m loosing background-position and background-size

I’m trying to change my background image via JS.

I can do this but I loose my

background-position: center;
background-size: cover;

settings when I do so.

Here’s my JS code.

Any ideas?

“`
setTimeout(changeImg, 3000);
function changeImg(){
document.getElementById(“banner”).style.background = “url(‘../images/img3.jpg’)”;
}
“`

to post a comment
CSSJavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorJul 19.2020 — A solution is to add this to my JS

document.getElementById("banner").style.backgroundPosition = "center";
document.getElementById("banner").style.backgroundSize = "cover";

But why does it happen and can you prevent it.
Copy linkTweet thisAlerts:
@daveyerwinJul 20.2020 — style.backgroundImage = "url('../images/img3.jpg')";

Copy linkTweet thisAlerts:
@siddhi_patelJul 20.2020 — hello..

it was happened because of whan you set the image at that time is is set the default style property so you need to your style property whan that function is call but you did that so it is happen.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...