css页面布局技巧

一、居中布局

<div class="parent" style="width:300px;height:300px;">
    <div class="child">居中布局</div>
</div>

水平居中(宽度自适应)

1.inline-block + text-align

.child{
    display:inline-block;  /*变成行内块元素,让元素宽度自适应,不继承父元素宽度;*/
}
.parent{
    text-align: center;
}

2.table + margin

.child{
    display: table;   /*变成table元素,可以让元素宽度自适应,不继承父元素宽度;*/
    margin: 0 auto;  
}

3.absolute + transform

.parent{
    position: relative;
}
.child{
    position: absolute;   /*定位,可以让元素宽度自适应,不继承父元素宽度;*/
    transform: translateX(-50%);  
    left: 50%;
}

4.flex + justify-content

.parent{
    display: flex;
    justify-content: center;
}
/*或者*/
.parent{
    display: flex;
}
.child{
    margin: 0 auto;
}

垂直居中(高度自适应)

1.table-cell + vertical-align

.parent{
    display: table-cell;  /*变成类似td元素*/
    vertical-align: middle;
}

2.absolute + transform

.parent{
    position: relative;
}
.child{
    position: absolute;   /*定位,可以让元素宽度自适应,不继承父元素宽度;*/
    top: 50%;
    transform: translateY(-50%);
}

3.flex + align-items

.parent{
    display: flex;
    align-items: center;
}

水平垂直居中

  1. inline-block + text-align + table-cell + vertical-align
.parent{
    text-align: center;
    display: table-cell;
    vertical-align: middle;
}
.child{
    display: inline-block;   
}

2.absolute + transform

.parent{
    position: relative;
}
.child{
    position: absolute;   /*定位,可以让元素宽度自适应,不继承父元素宽度;*/
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

3.flex + align-items + justify-content

.parent{
    display: flex;
    align-items: center;
    justify-content: center;
}

二、多列布局

<div class="parent">
    <div class="left">
        <p>left</p>
    </div>
    <div class="right">
        <p>right</p>
        <p>right</p>
    </div>
</div>

左边定宽,右边自适应

  1. float + margin
.left{
    width: 100px;
    float: left;
}
.right{
    margin-left: 120px;
}
  1. float + overflow
/*和1方法表现的效果一样*/
.left{
    width: 100px;
    float: left;
}
.right{
    margin-left: 20px;
    overflow: hidden;
}
  1. table
.parent{
    display: table;
    width: 100%;
    table-layout: fixed;
}
.left,.right{
    display: table-cell;
}
.left{
    width: 100px;
    padding-right: 20px;
}
  1. flex
.parent{
    display: flex;
}
.right{
    flex: 1;
}
.left{
    width: 100px;
}
  1. absolute
.parent{
    position: relative;
}
.right{
    position: absolute;
    left: 100px;
    right: 0;
}
.left{
    width: 100px;
}

左边不定宽,右边自适应

  1. float + overflow
.left{
    float: left;
}
.right{
    margin-left: 20px;
    overflow: hidden;
}
  1. table
.parent{
    display: table;
    width: 100%;
}
.left,.right{
    display: table-cell;
}
.left{
    width: 0.1%;
}
.left{
    padding-left: 10px;
}
  1. flex
.parent{
    display: flex;
}
.right{
    flex: 1;
}
.left{
    margin-right: 20px;
}

三、等宽布局

//假如是n个child
<div class="parent-fix">
    <div class="parent">
        <div class="child"><p>1</p></div>
        <div class="child"><p>2</p></div>
        <div class="child"><p>3</p></div>
        <div class="child"><p>4</p></div>
    </div>
</div>
  1. table
.parent-fix{
    margin-left: -20px;
}
.parent{
    display: table;
    width: 100%;
    table-layout: fixed;
}
.child{
    display: table-cell;
    padding-left: 20px;
}
  1. flex
.parent{
    display: flex;
}
.child{
    flex: 1;
}
.child+.child {
    margin-left: 20px;
}

四、等高布局

<div class="parent"  style="background: black;">
    <div class="left" style="background: red;">
        <p>left</p>
    </div>
    <div class="right" style="background: green;">
        <p>right</p>
        <p>right</p>
    </div>
</div>
  1. table
.parent{
    display: table;
    width: 100%;
    table-layout: fixed;
}
.left,.right{
    display: table-cell;
}
.left{
    width: 100px;
    border-right: 20px solid transparent;
    background-clip: padding-box;
}
  1. flex
.parent{
    display: flex;
}
.right{
    flex: 1;
}
.left{
    width: 100px;
    margin-right: 20px;
}
  1. float
//部分UI框架采用的就是这种方式,
.parent{
    overflow: hidden;
}
.left{
    float: left;
    margin-right: 20px;
}
.right{
    overflow: hidden;
}
.left,.right{
    padding-bottom: 9999px;
    margin-bottom: -9999px;
}

相关推荐