Html Internet Explorer CSS - 中心 Div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10607368/
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 00:26:30  来源:igfitidea点击:

Internet Explorer CSS - Center Div

htmlcssinternet-explorerdoctype

提问by John Wheal

I recently made a website but I'm having a really big problem with Internet Explorer. The CSS seems to do strange things in IE. Rather than copying hundreds of lines of CSS here is a link to a page of the site: http://www.appwheal.com/app/ios/angry-birds-spaceAll the content should be displayed in the middle of the screen. Instead with IE it is stuck to the left.

我最近创建了一个网站,但 Internet Explorer 遇到了一个非常大的问题。CSS 似乎在 IE 中做一些奇怪的事情。这里不是复制数百行 CSS 而是指向站点页面的链接:http: //www.appwheal.com/app/ios/angry-birds-space所有内容都应显示在屏幕中间. 而对于 IE,它被卡在左边。

I'm using

我正在使用

margin-left: auto;

margin-right: auto;

This is supported by IE, right.

这是IE支持的,对。

Please help and thanks in advance.

请帮助并提前致谢。

回答by saluce

You need to declare a DOCTYPE, or Internet Explorer defaults to Quirks mode (IE5 compatibility). Go into Internet Explorer, hit F12 to bring up Developer tools, and notice that it shows "Quirks" mode under Document Mode. Quirks doesn't support any of the known div centering methods around, and declaring the DOCTYPEis the easiest (and recommended) way to fix it.

您需要声明DOCTYPE,否则 Internet Explorer 默认为 Quirks 模式(IE5 兼容)。进入 Internet Explorer,按 F12 调出开发人员工具,注意它在文档模式下显示“Quirks”模式。Quirks 不支持任何已知的 div 居中方法,声明DOCTYPE是修复它的最简单(也是推荐的)方法。

To set your page for XHTML 1.0 Transitional (which is the most common), use

要将您的页面设置为 XHTML 1.0 Transitional(这是最常见的),请使用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

To declare the page is HTML5 compatible, use

要声明页面与 HTML5 兼容,请使用

<!DOCTYPE html>

The DOCTYPEline needs to be the first line in the html file, appearing BEFORE the opening <html>tag.

DOCTYPE行必须是 html 文件中的第一行,出现在开始<html>标记之前。

回答by Kristian

You must also set

您还必须设置

body {
    text-align: center;
}

#yourDiv {
    margin: 0 auto;
    text-align: left;
}

回答by user2094324

Set margin and width :

设置边距和宽度:

#yourDiv {
  margin: 0 auto;
  width: 300px;
}

回答by Gang Su

You may use the old school text-align:center And try top:50%; left:50%; and offset suitable negative pixels

你可以使用老式的 text-align:center 并尝试 top:50%; 左:50%;并偏移合适的负像素