wangnantjobj 2019-11-03
<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; }
.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>
.left{ width: 100px; float: left; } .right{ margin-left: 120px; }
/*和1方法表现的效果一样*/ .left{ width: 100px; float: left; } .right{ margin-left: 20px; overflow: hidden; }
.parent{ display: table; width: 100%; table-layout: fixed; } .left,.right{ display: table-cell; } .left{ width: 100px; padding-right: 20px; }
.parent{ display: flex; } .right{ flex: 1; } .left{ width: 100px; }
.parent{ position: relative; } .right{ position: absolute; left: 100px; right: 0; } .left{ width: 100px; }
.left{ float: left; } .right{ margin-left: 20px; overflow: hidden; }
.parent{ display: table; width: 100%; } .left,.right{ display: table-cell; } .left{ width: 0.1%; } .left{ padding-left: 10px; }
.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>
.parent-fix{ margin-left: -20px; } .parent{ display: table; width: 100%; table-layout: fixed; } .child{ display: table-cell; padding-left: 20px; }
.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>
.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; }
.parent{ display: flex; } .right{ flex: 1; } .left{ width: 100px; margin-right: 20px; }
//部分UI框架采用的就是这种方式, .parent{ overflow: hidden; } .left{ float: left; margin-right: 20px; } .right{ overflow: hidden; } .left,.right{ padding-bottom: 9999px; margin-bottom: -9999px; }