相当于
CSS 中的标签?

发布于 2024-12-01 20:59:26 字数 231 浏览 4 评论 0原文

我读到

标签已被弃用,但是我在 CSS 中找不到任何真正等效的标签。 text-align 适用于文本,但不适用于其他元素,并且自动边距仅在您知道容器的宽度时才起作用(因此,如果您事先不知道宽度,则不是解决方案)。那么这个
标签有真正的等价物吗?

I read that the <center> tag is deprecated, however I cannot find any real equivalent to it in CSS. text-align works for text but not other elements, and auto margins only work if you know the width of the container (so not a solution if you don't know the width in advance). So is there any real equivalent to this <center> tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-12-08 20:59:26

text-align 应该适用于其他类型的元素。这有效吗?

.center { margin: auto; text-align: center; }

三年后编辑 :-D

margin: auto; 也使顶部和底部边距“自动”。那可能不是你想要的。或者,您可以有类似的内容:

.center {
  margin-left: auto;
  margin-right: auto;
  margin-top: 3px;
  margin-bottom: 3px;
  text-align: center;
  display: flex;
  justify-content: center;
}

这个特定示例将使文本水平居中,同时将上下边距硬连接到 3 个像素。

也可以说类似 margin: 3px auto 3px auto; 但我更喜欢明确地拼写出方向,因为如果我将它们全部放在一个参数上,我永远记不清参数的顺序是什么边距: 设置。

text-align should work for other kinds of elements. Does this work?

.center { margin: auto; text-align: center; }

Edit Three Years Later :-D

margin: auto; also makes the top and bottom margins "auto". That might not be what you want. Alternatively you could have something like:

.center {
  margin-left: auto;
  margin-right: auto;
  margin-top: 3px;
  margin-bottom: 3px;
  text-align: center;
  display: flex;
  justify-content: center;
}

This particular example will center the text horizontally while hardwiring the upper and lower margins to 3 pixels.

One can also say something like margin: 3px auto 3px auto; but I prefer spelling out the directions explicitly, as I can never quite remember what the order of the parameters are if I put them all on the one margin: setting.

找回味觉 2024-12-08 20:59:26

有一个区别:

<center>
  <span>Works</span>
</center>

<span style="margin: auto; text-align: center;"> And this is broken</span>

<div style="margin: auto; text-align: center;">But this works works</div>

There is a difference:

<center>
  <span>Works</span>
</center>

<span style="margin: auto; text-align: center;"> And this is broken</span>

<div style="margin: auto; text-align: center;">But this works works</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文