gpt4 book ai didi

css - 为什么不用margin-top : auto and margin-bottom:auto work the same as their left and right counterparts?

转载 作者:行者123 更新时间:2023-12-04 23:51:46 26 4
gpt4 key购买 nike

如果我像这样设置一个 div 的 CSS 边距属性:

div { margin-left: auto; margin-right: auto; }

我得到一个在页面中水平居中的 div,like so .

但是,如果我将 CSS 更改为:

div { margin-top: auto; margin-bottom: auto; }

我的 div 没有垂直居中。我不需要知道解决方法( plenty of solutions are available ),但我想知道这种行为的原因。为什么 margin-top 和 margin-bottom 的工作方式不同?我错过了什么?

最佳答案

简而言之,规范是这么说的。

10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements

If 'margin-top', or 'margin-bottom' are 'auto', their used value is 0.

http://www.w3.org/TR/CSS2/visudet.html#Computing_heights_and_margins

关于css - 为什么不用margin-top : auto and margin-bottom:auto work the same as their left and right counterparts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10871795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com