在其包含的元素中居中html元素的最好方法是什么? [英] What is the best way to center an html element within its containing element?

查看:105
本文介绍了在其包含的元素中居中html元素的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下大块代码:

Let's say you have the following chunk of code:

<div id="container">
<someelement>This is any element.</someelement>
</div>

我可以使用的最好的CSS水平居中其中包含div的someelement如果您的< someelement> 是内联元素(即默认为显示),则

What's the best CSS I could use to horizontally center "someelement" within its containing div?

推荐答案

:inline)apply text-align:居中到其容器。如果< someelement> 是块元素设置左右边距自动,并记住设置宽度(块元素默认采取所有可用的水平空间,除非指定宽度显式)。
如果你想在IE 5.5及以下版本中工作,你可能需要同时使用这两种方法。

if your <someelement> is an inline element (i.e defaults to display: inline) apply text-align: center to its container. If <someelement> is a block element set left and right margin to auto and remember to set a width (block elements default to take all available horizontal space unless a width is stated explicitly). You may have to use both methods if you want it to work in IE 5.5 and below too.

这篇关于在其包含的元素中居中html元素的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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