on-internet
← Community

How to set button in the middle of the screen in JS

Stack Overflow ·

I have a button and I need to set it in the middle for all devices not only one. I am making a interface and I want to button stay in the middle and I can't use transform because I will move this button after clicking it. I tried this: function centerObject (obj) { obj.style.marginTop = (window.innerHeight / 2) - (obj.offsetHeight) + "px" obj.style.marginLeft = (window.innerWidth / 2) - (obj.offsetWidth)+ "px" } and this: margin: auto; but it doesn't work.

Comments

Sign in or sign up to comment and upvote.

No comments yet.