美文网首页
CSS 布局

CSS 布局

作者: 小凡凡520 | 来源:发表于2019-07-31 09:35 被阅读0次

在网页中,元素有三种布局模型:

流动模型(Flow)== 标准流 == 文档流
浮动模型 (Float)
层模型(Layer) == 定位

流动模型,块级元素占一行,内联元素并排显示

浮动模型 ,让块级元素并排显示

div{
    width: 200px;
    height: 200px;
    float: left;
}

层模型

绝对定位(position: absolute)
相对定位(position: relative)
固定定位(position: fixed)

绝对定位,相对于浏览器窗口(屏幕内的网页内容),脱离文档流

div{
    width: 200px;
    height: 200px;
    border: black 1px solid;
    position: absolute;
    left: 20px;
    top: 20px;
}

相对定位,相对于以前的位置移动,可层叠

div{
    width: 200px;
    height: 200px;
    border: black 1px solid;
    position: relative;
    left: 20px;
    top: 20px;
}

固定定位,相对移动的坐标是视图(屏幕内的网页窗口)本身,脱离文档流

#id1{
    width: 200px;
    height: 200px;
    border: black 1px solid;
    position: fixed;
    left: 20px;
    top: 20px;
}

继承定位

以父定位为主

水平居中

1、行内元素
text-align: center

2、定宽块元素
div{
            width: 200px;
            border: blue 1px solid;
            margin: 200px auto;
}

3、不定宽块元素
table{
            border: blue 1px solid;
            margin: 20px auto;
}

4、浮动
.container{
            float: left;
            left: 50%;
            position: relative;
}
.container ul{
            list-style: none;
            display: inline;
}
.container li{
            list-style: none;
            display: inline;
}

垂直居中

table td{
           border: blue 1px solid;
           height: 400px;
           vertical-align: middle;
}


设置为表格单元显示
.container{
           height: 400px;
           display: table-cell;
           vertical-align: middle;
           border: blue 1px solid;
}


内联块元素
.container a{
           position: absolute;
           width: 200px;
           background: brown;
           border: blue 1px solid;
}

Z-index

值越大,层级在上

相关文章

网友评论

      本文标题:CSS 布局

      本文链接:https://www.haomeiwen.com/subject/mzznrctx.html