css頁面佈局技巧

1、居中佈局

<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 + transformflex

.parent{
    position: relative;
}
.child{
    position: absolute;   /*定位,可讓元素寬度自適應,不繼承父元素寬度;*/
    transform: translateX(-50%);  
    left: 50%;
}

4.flex + justify-contentspa

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

垂直居中(高度自適應)

1.table-cell + vertical-aligncode

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

2.absolute + transformorm

.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 + transformip

.parent{
    position: relative;
}
.child{
    position: absolute;   /*定位,可讓元素寬度自適應,不繼承父元素寬度;*/
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

3.flex + align-items + justify-contentit

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

2、多列布局

<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;
}

3、等寬佈局

//假如是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;
}

4、等高佈局

<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;
}
相關文章
相關標籤/搜索