首页 > 其他 > 详细

margin-left: auto;为什么可以使元素靠右

时间:2021-06-05 17:55:43      阅读:18      评论:0      收藏:0      [点我收藏+]
div {
  width: 100px;
  margin-left: auto;
}

上面的代码可以使得div靠近浏览器的右侧显示是为什么?

有点弱智的问题,竟然现在才知道...记录一下

The following constraints must hold among the used values of the other properties:
‘margin-left‘ + ‘border-left-width‘ + ‘padding-left‘ + ‘width‘ + ‘padding-right‘ +‘border-right-width‘ + ‘margin-right‘ = width of containing block
你的属于其中一种情况:
If there is exactly one value specified as ‘auto‘, its used value follows from the equality.

也就是在上述等式中,只有你设置的margin-left: auto,那么margin-left将会被设置为满足上述等式,而等式的右边是容器盒子宽度,等式中的其它值(除过width)都为0,因此margin-left = width of containing block - width(div)

再来看一下我们经常使用的 margin: auto 水平居中的原理。

If both ‘margin-left‘ and ‘margin-right‘ are ‘auto‘, their used values are equal. This horizontally centers the element with respect to the edges of the containing block.

转自知否 https://segmentfault.com/q/1010000008431088

tips:对行级元素不生效,除非设置display: block;或者display: flex;

margin-left: auto;为什么可以使元素靠右

原文:https://www.cnblogs.com/c1nema/p/14852364.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!