zoukankan      html  css  js  c++  java
  • 详解margin: auto

    auto是margin的可选值之一。相信大家平时使用auto值时,最多的用法大概是 margin: 0 auto; 和 margin: auto;

    不过你可能也发现了不论是 margin: auto; 还是 margin: 0 auto; 效果都是一样的,都是让 #demo 水平居中了,但纵向并没有任何变化。

    大家都知道 margin 是复合属性,也就是说 margin: auto; 其实相当于 margin: auto auto auto auto;,margin: 0 auto;相当于 margin: 0 auto 0 auto;,四个值分别对应上右下左。

    水平方向:

    > 正常流中的块级元素框的水平总和就等于父元素的width

    'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block


    > 在水平格式化的“7大属性”中,只有3个可以设置为auto: width, margin-left, margin-right;

    水平方向的 auto,其计算值取决于可用空间(剩余空间)。

    竖直方向:

    宽度计算默认涉及包含块(可粗略理解为父级元素),而高度计算默认涉及内部元素

    默认行为的高度计算则是一系列“撑高”规则,而非“适应于父级”规则。

    对于绝对定位元素,有以下算式:

    top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height of containing block

    .father {
                    position: relative;
                     100px;
                    height: 100px;
                    background-color: black;
                }
                
                .son {
                    position: absolute;
                    top: 0;
                    left: 0;
                    bottom: 0;
                    right: 0;
                     50%;
                    height: 50%;
                    margin: auto;
                    background-color: red;
                }
  • 相关阅读:
    centos7 python3.5中引入sqlite3
    转载nginx+uwsgi+django
    浮点数计算精度丢失问题#W01
    五大JavaScript 自动化测试框架
    deepin 安装Samba并设置为开机启动
    搭建macaca android环境
    open-MAT 安装部署
    基于Jmeter BackEnd+InfluxDB+Grafana实现性能指标实时可视监控
    使用开源libimobiledevice查看iphone信息
    Java 开发者必备测试框架
  • 原文地址:https://www.cnblogs.com/coderL/p/7667474.html
Copyright © 2011-2022 走看看