Html 在页面中水平居中 DIV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16996775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:14:04  来源:igfitidea点击:

Horizontally center a DIV in page

htmlwindows-8microsoft-metrowinjs

提问by ReignOfComputer

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

我正在尝试将一个 div 居中,它是 HTML5/JavaScript 中用于 Windows 8 应用程序的 Microsoft pubCenter AdControl。

    <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?

我如何将 div 水平居中,使其始终位于页面中间?

采纳答案by sd1sd1

you can use css only

你只能使用 css

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

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

关键是将左边距设置为div宽度的一半

回答by Dummy Code

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

添加margin:auto您的style=""标签,这应该可以解决问题。

Other possibility, align=center

另一种可能, align=center

回答by Devesh Chauhan

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/

示例小提琴:http: //jsfiddle.net/devesh_dc/dzyvmt0o/

回答by MyGEARStationcom

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>

在 HTML5、C# 和 VS2015 的其他站点上找到。添加到 css:#wrapper { width: 600px;margin: auto;}并使用以下内容包装您的内容:
<form id="form1"> <div id="wrapper"> Content </div> </form>

回答by Shankar Akunuri

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

你可以align='center'<div>标签中使用