CSS 垂直居中和水平居中

来源:互联网 发布:软件导刊杂志社 编辑:程序博客网 时间:2024/04/29 13:05
 .wrapper {
            position: relative;
            width: 500px;
            height: 500px;
            background-color: #ddd;
        }


        .content {
            background-color: #6699FF;
            width: 50%;
            height: 50%;
            position: absolute;
            /*top: 25%;
            left: 25%;    这两行和下面四行效果一样*/
            top: 50%;
            left: 50%;
            margin-top: -25%;
            margin-left: -25%;

        }

可能的值

值描述absolute

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed

生成绝对定位的元素,相对于浏览器窗口进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative

生成相对定位的元素,相对于其正常位置进行定位。

因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

static默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。inherit规定应该从父元素继承 position 属性的值。

0 0
原创粉丝点击