替换< center> [英] Replacement for <center>

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

问题描述

以前其他人都曾问过这个问题,但我从未见过适当的答案。是否可以有效替换< center> 标记?

This has been asked before by other people, but I've never seen an adequate answer. Is there a valid replacement to the <center> tag?

我知道有页边距:0自动; ,但这需要设置宽度。还有 align = center ,但是我认为这也是无效的代码。

I know there's margin: 0 auto;, but that requires setting a width. There's also align="center", but I believe that that's invalid code as well.

是否有像< center> 这样简单的东西有效?即使不推荐使用,在极少数情况下我仍然会使用它。就在今天,我最终使用< center> 将需要在网页上居中的一个按钮居中。是的,我可以设置宽度并为其设置 margin:0 auto ,但是居中单个元素的工作量很大,这使我的代码很脏,我以保持秩序为荣。我真的不明白为什么< center> 首先被弃用,如果没有什么替代它的话。

Is there something as simple as <center> that's valid? There are rare occasions where I still end up using it, even though it is deprecated. Just today, I ended up using a <center> to center the one button that needed centered on a web page. Yes, I could have set the width and gave it margin: 0 auto, but that's a lot of work for centering one single element, and it dirties up my code, which I take pride in keeping orderly. I don't really understand why <center> was deprecated in the first place, if nothing has replaced it.

谢谢!

推荐答案

text-align:center 应该使用。实际上,理想的方式是这样:

text-align: center is what you should use. In fact, the ideal way is this:

.center {
    text-align: center;
}
.center > div, .center > table /* insert any other block-level elements here */ {
    margin-left: auto;
    margin-right: auto;
}

很显然,它并不像您希望的那么简单。

Obviously, it's not quite as simple as you might hope for.

我个人只是使​​用:

.center {text-align: center;}
.tmid {margin: 0px auto;}

然后在需要的地方应用类。

Then apply classes where needed.

这篇关于替换&lt; center&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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