我正在尝试将整个页面与框架的中心对齐,如果我使用中心标签,它工作得很好。但是,如果我使用
<body align=center>
or
<body style="align:center">
它不会对齐页面,但默认保持左对齐。现在,说实话,我是 HTML 的新手,所以我不太清楚是什么原因造成的。此外,这种情况仅发生在非 IE 浏览器中。 IE 显示页面居中对齐,即使没有任何标签。
虽然,这没有多大意义,因为我总是可以使用中心标签,但很高兴知道为什么对齐属性不起作用。如果有人知道问题出在哪里,请告诉我。
编辑:
事实证明,这是因为怪癖模式。谢谢大卫和镇! :D
I am trying to align a complete page, to the center of a frame, and if I use center tag, it works fine. But, if I use
<body align=center>
or
<body style="align:center">
it doesn't align the page, but keep it left-aligned as default. Now, I am a newbie at HTML to be honest, so I don't have much idea, what may be causing this. Also, this happens only in non-IE browsers. IE shows the page center aligned, even without any tags.
Although, it is not of much significance, as I can always use center tag, but its nice to know why align attribute doesn't work. Kindly let me know, if anyone got any idea, as to what is the problem.
Edit:
Turns out, it is because of quirks mode. Thanks David and Town! :D
发布评论
评论(3)
要将正文中的所有内容居中,请使用 text-align:
要将元素在其容器中居中,请为其指定宽度并使用边距,如下所示:
编辑: 正如其他人所建议的,您应该使用样式表使一切保持整洁。将以下内容放入
中(或引用单独的 CSS 文件)。
To centre everything within the body, use text-align:
To centre an element within its container, give it a width and use margin, like this:
EDIT: as other people have suggested, you should use a stylesheet to keep everything tidy. Put the following in your
<head>
(or reference a separate CSS file).不要使用这个。不推荐使用对齐属性。
CSS 是首选方法,但是:
align
属性如果要将应用该属性的元素的内联内容居中或设置左右边距,请使用
text-align
属性toauto
将块元素居中。有关更多详细信息和图表,请参阅使用 CSS 居中。如果您没有触发标准模式的 Doctype,那么浏览器将模拟以下错误:
早期的浏览器(Quirks 模式)。这是你应该避免的事情,因为它会使事情变得不那么一致。
这些错误之一是,如果祖先设置了
text-align: center
(并且不支持自动边距),则 Internet Explorer 会将块元素居中。Don't use this. The align attribute is deprecated.
CSS is the preferred method but:
align
property in CSSUse the
text-align
property if you want to centre the inline content of the element to which you apply it or set the left and right margins toauto
to centre block elements. For more details, and diagrams, see Centring using CSS.If you don't have a Doctype that triggers Standards mode, then browsers will emulate bugs in
earlier browsers (Quirks mode). This is something you should avoid as it makes things much less consistent.
One of these bugs is that Internet Explorer will centre block elements if an ancestor has
text-align: center
set (and won't support auto margins).是的,你想要的CSS标签是“text-align”而不是“align”。另外,您应该尽量避免内联样式,尽可能使用样式表;它有助于将所有样式保存在一个位置,并让浏览器缓存样式。
Yes, the CSS tag you want is "text-align" not "align". Also, you should try and avoid inline styling, whenever possible use a stylesheet; it helps to keep all of your styling together in one place, and lets the browser cache the styling.