对齐div内的内容 [英] Align contents inside a div

查看:191
本文介绍了对齐div内的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用css style text-align在HTML中对齐容器中的内容。这工作正常,而内容是文本或浏览器是IE。



也因为名称建议它基本上用于对齐文本。



有没有其他方法来对齐html中的内容?

解决方案

text-align对齐文本和其他内联内容。它不匹配块元素的子元素。



要做到这一点,你想给元素对齐一个宽度,'auto'左右边距。这是符合标准的方式,除了IE5.x以外的所有地方都有效。

 < div style =width:50% margin:0 auto;> Hello< / div> 

要在IE6中使用,您需要确保标准模式正在使用合适的DOCTYPE。



如果您确实需要支持IE5 / Quirks模式,这些天你不应该真的,可以结合两种不同的方法来定中心:

 < div style =text-align:center> 
< div style =width:50%; margin:0 auto; text-align:left> Hello< / div>
< / div>

(很明显,样式最好放在样式表中, p>

I use css style text-align to align contents inside a container in HTML. This works fine while the content is text or the browser is IE. But otherwise it does not work.

Also as the name suggests it is used basically to align text. The align property has been deprecated long back.

Is there any other way to align contents in html?

解决方案

text-align aligns text and other inline content. It doesn't align block element children.

To do that, you want to give the element you want aligned a width, with ‘auto’ left and right margins. This is the standards-compliant way that works everywhere except IE5.x.

<div style="width: 50%; margin: 0 auto;">Hello</div>

For this to work in IE6, you need to make sure Standards Mode is on by using a suitable DOCTYPE.

If you really need to support IE5/Quirks Mode, which these days you shouldn't really, it is possible to combine the two different approaches to centering:

<div style="text-align: center">
    <div style="width: 50%; margin: 0 auto; text-align: left">Hello</div>
</div>

(Obviously, styles are best put inside a stylesheet, but the inline version is illustrative.)

这篇关于对齐div内的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆