12

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?

4

5 回答 5

19

Add margin:auto in your style="" tag and that should do the trick.

Other possibility, align=center

于 2013-06-08T06:36:31.840 回答
7

you can use css only

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

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

于 2013-06-08T06:35:11.910 回答
4

you can use simply this class to absolute center

.ab_center
{
    height:300px;
    width:300px;
    background-color:#27f305;
    position:absolute;
    margin:auto;
    top:0;
    left:0;
    right:0;
    bottom:0;
}

Example fiddle : http://jsfiddle.net/devesh_dc/dzyvmt0o/

于 2014-11-13T06:39:14.513 回答
0

Found on other site for HTML5, C# and VS2015. Add to css: #wrapper { width: 600px;margin: auto;} and wrap your content with:
<form id="form1"> <div id="wrapper"> Content </div> </form>

于 2015-09-25T06:20:42.780 回答
-6

you can use align='center' in <div> tag

于 2013-06-08T06:45:58.103 回答