I am creating a program to allow a user to change most CSS properties of a div. I need it to be centred though, the way I usually do this is with the CSS code below.
div {
position: fixed;
background: black;
width: 100px;
height: 100px;
top: calc(50% - 50px);
right: calc(50% - 50px);
}
I need to make the width and height their own variables however and then do a calculation for the top and right properties which will divide them by 2 and take away from 50%.
var width = 100;
var height = 100;
var top = (height / 2);
var right = (width / 2);
$('div')
.css('position','fixed')
.css('background','black')
.css('width',width + 'px')
.css('height',height + 'px')
.css('top','calc(50% - ' + top + 'px)')
.css('right','calc(50% - ' + right + 'px)');
How can I go about achieving a centred div while using variables as the values for CSS properties?