Search code examples
htmlwindows-8microsoft-metrowinjs

Horizontally center a DIV in page


I'm trying to center a div, which is the Microsoft pubCenter AdControl for Windows 8 apps in HTML5/JavaScript.

    <div id="myAd" style="; top: 0%; width: 292px; height: 60px; z-index: 1"
        data-win-control="MicrosoftNSJS.Advertising.AdControl"
        data-win-options="{applicationId: 'exampleAdId', adUnitId: 'exampleAdUnit'}">
    </div>

How would I center a div horizontally so it is always in the middle of a page?


Solution

  • you can use css only

    left:50%;margin-left:-146px;
    

    the point is to set margin left as half of the div width