JSFiddle - React, Tailwind, and code Playground

HTML

<div id="box">
    My box
</div>

CSS

#box {
    position:absolute;
    border:1px solid red;
    text-align:center;
    width:300px;
    left: 50%;
    margin-left:-150px; /* half of the box */
}

JavaScript

function leftmargin(){
    var w_box    =  $('#box').width();
    $("#box").css("marginLeft", -(w_box/2) + "px");
}

var resizeTimer;
$(window).resize(function() {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(function() {
        leftmargin();
    },75);
});