Rikard
Rikard

Reputation: 7805

set background position with auto

How to apply and getback the background-size using plain javascript?

I tried:

var element = document.createElement('div');
element.style.backgroundPosition = '40px 50px';
console.log(element.style.backgroundPosition);
// logs correct

element.style.backgroundPosition = '40px auto';
console.log(element.style.backgroundPosition);
// logs empty, expected '40px auto'

Fiddle

Upvotes: 0

Views: 102

Answers (2)

R3tep
R3tep

Reputation: 12864

The property css background-position not support this syntaxes.

Documentation W3C.org

Maybe the solution is :

element.style.backgroundPosition = '40px 0%';

Upvotes: 1

random_user_name
random_user_name

Reputation: 26160

Unfortunately, background-position doesn't support auto. I suspect, however, that you are looking for it to center - so what I believe you are looking for is this:

element.style.backgroundPosition = '40px center';
console.log(element.style.backgroundPosition);
// logs "40px center"

Upvotes: 2

Related Questions