为什么 margin: auto 只能水平工作而不能垂直工作?

发布于 2024-12-22 16:56:42 字数 238 浏览 3 评论 0原文

我发现,在开发网站时,在随机高度的容器内垂直居中容器(固定高度)对于 Web 开发人员(至少是我)来说总是一场噩梦,而当涉及到水平居中容器(固定高度)时, width)在随机宽度的容器内,margin:0px auto; 往往在标准模型中提供一种简单的解决方法。

当事情可以如此简单时,为什么 CSS 在将固定高度的容器置于随机高度的容器内居中时不使用 margin:auto 0px; 来解决呢?这样做有什么具体原因吗?

I have seen that while developing websites, vertically centering a container (of fixed height) inside a container of random height always comes as a nightmare for the web developer (at least, me) while when it comes to horizontal centering a container (of fixed width) inside a container of random width, the margin:0px auto; tends to serve an easy way out in the standard model.

When things can be as simple as that why doesn't CSS work out with the margin:auto 0px; when it comes to centering a container of fixed height inside a container of random height? Is there any specific reason to do so?

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

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

发布评论

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

评论(4

独留℉清风醉 2024-12-29 16:56:42

这确实没有你想象的那么可怕,只是不要使用边距。 vertical-align 确实是您应该依赖的流体高度垂直居中。我整理了一个快速演示来证明我的观点:

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  text-align: center;
}

span {
  height: 100%;
  vertical-align: middle;
  display: inline-block;
}

#any-height {
  background: #000;
  text-align: left;
  width: 200px;
  height: 200px;
  vertical-align: middle;
  display: inline-block;
}
<span></span>
<div id="any-height"></div>

请参阅:http://jsfiddle.net/Wexcode/jLXMS/

It's really less of a nightmare than you think, just don't use margins. vertical-align is really what you should rely on for fluid-height vertical centering. I threw together a quick demo to demonstrate my point:

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  text-align: center;
}

span {
  height: 100%;
  vertical-align: middle;
  display: inline-block;
}

#any-height {
  background: #000;
  text-align: left;
  width: 200px;
  height: 200px;
  vertical-align: middle;
  display: inline-block;
}
<span></span>
<div id="any-height"></div>

See: http://jsfiddle.net/Wexcode/jLXMS/

迟月 2024-12-29 16:56:42

您问题的正确答案是 margin: auto 0 的工作方式与 margin: 0 auto 的工作方式不同,因为 width: autoheight: auto 的工作方式不同。

垂直自动边距适用于具有已知高度的绝对定位元素。

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    width: 150px;
    height: 150px;
    margin: auto;
}

The right answer for your question is that margin: auto 0 doesn't work the same way that margin: 0 auto works because width: auto doesn't work the same way height: auto does.

Vertical auto margin works for absolutely positioned elements with a known height.

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 0; right: 0; bottom: 0; left: 0;
    width: 150px;
    height: 150px;
    margin: auto;
}
没有你我更好 2024-12-29 16:56:42

使用 flexboxparent

justify-content: center / align-items: center 相比,使用 margin: auto 的优点之一是能够滚动溢出父项的子项

html, body {
  height: 100%;
  margin: 0;
}

.parent {
  display: flex;
  width: 100%;
  height: 100%;
}

.inner {
  width: 100px;
  height: 100px;
  background-color: green;
  margin: auto;
}
<div class="parent">
  <div class="inner">
  </div>
</div>

Use flexbox parent

One of advantages of using margin: auto compared to justify-content: center / align-items: center is ability to scroll child item that is overflowing parent

html, body {
  height: 100%;
  margin: 0;
}

.parent {
  display: flex;
  width: 100%;
  height: 100%;
}

.inner {
  width: 100px;
  height: 100px;
  background-color: green;
  margin: auto;
}
<div class="parent">
  <div class="inner">
  </div>
</div>

海螺姑娘 2024-12-29 16:56:42

CSS

.aligncenter{
      display: -webkit-box;
      display: -moz-box;
      display: box;
      -webkit-box-align: center;
      -moz-box-align: center;
      flex-align: center;
      -webkit-box-pack: center;
      -moz-box-pack: center;
      flex-pack: center;
}

HTML

<div class="aligncenter">
    ---your content appear at the middle of the parent div---
</div>

.aligncenter {
  display: -webkit-box;
  display: -moz-box;
  display: box;
  -webkit-box-align: center;
  -moz-box-align: center;
  flex-align: center;
  -webkit-box-pack: center;
  -moz-box-pack: center;
  flex-pack: center;
}
<div class="aligncenter">
  ---your content appear at the middle of the parent div---
</div>

注意

此 CSS 类适用于几乎所有浏览器

CSS

.aligncenter{
      display: -webkit-box;
      display: -moz-box;
      display: box;
      -webkit-box-align: center;
      -moz-box-align: center;
      flex-align: center;
      -webkit-box-pack: center;
      -moz-box-pack: center;
      flex-pack: center;
}

HTML

<div class="aligncenter">
    ---your content appear at the middle of the parent div---
</div>

.aligncenter {
  display: -webkit-box;
  display: -moz-box;
  display: box;
  -webkit-box-align: center;
  -moz-box-align: center;
  flex-align: center;
  -webkit-box-pack: center;
  -moz-box-pack: center;
  flex-pack: center;
}
<div class="aligncenter">
  ---your content appear at the middle of the parent div---
</div>

Note

This CSS class work with almost all browsers

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