Horizontally center a DIV in page
Asked Answered
P

5

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?

Prelect answered 8/6, 2013 at 6:30 Comment(1)
may want to look at this questionGrimaud
A
7

you can use css only

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

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

Athal answered 8/6, 2013 at 6:35 Comment(1)
Or use margin:0 auto... Much simpler.Dnieper
D
19

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

Other possibility, align=center

Dnieper answered 8/6, 2013 at 6:36 Comment(0)
A
7

you can use css only

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

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

Athal answered 8/6, 2013 at 6:35 Comment(1)
Or use margin:0 auto... Much simpler.Dnieper
M
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/

Monetary answered 13/11, 2014 at 6:39 Comment(0)
Y
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>

Yellowbird answered 25/9, 2015 at 6:20 Comment(0)
P
-6

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

Philips answered 8/6, 2013 at 6:45 Comment(1)
This is not supported by html5Contra

© 2022 - 2024 — McMap. All rights reserved.