JSFiddle - React, Tailwind, and code Playground
HTML
<div class="myDivHere">
lorem ipsum dolor
</div>
CSS
.myDivHere {
position: absolute;
left: 50%;
top: 50%;
text-align: center;
width: 20%;
height: 20%;
background: red;
}
JavaScript
$(function() {
function updateDivPosition() {
var myWidth = $( '.myDivHere' ).width()
, myHeight = $( '.myDivHere' ).height();
$( '.myDivHere' ).css( {
marginLeft: -( parseInt( myWidth, 10 ) / 2 ) + 'px',
marginTop: -( parseInt( myHeight, 10 ) / 2 ) + 'px'
});
}
updateDivPosition(); // first time set correct position on onload
$( window ).resize( updateDivPosition ); // update on window resize
});