头闻号

寿光鲁恒化工有限公司

氯化物

首页 > 新闻中心 > 科技常识:css页面常用布局技巧
科技常识:css页面常用布局技巧
发布时间:2024-11-30 15:41:57        浏览次数:3        返回列表

今天小编跟大家讲解下有关css页面常用布局技巧 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关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; 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; 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>

左边定宽,右边自适应

float + margin

.left{ width: 100px; float: left;}.right{ margin-left: 120px;}

float + overflow

.left{ width: 100px; float: left;}.right{ margin-left: 20px; overflow: hidden;}

table

.parent{ display: table; width: 100%; table-layout: fixed;}.left,.right{ display: table-cell;}.left{ width: 100px; padding-right: 20px;}

flex

.parent{ display: flex;}.right{ flex: 1;}.left{ width: 100px;}

absolute

.parent{ position: relative;}.right{ position: absolute; left: 100px; right: 0;}.left{ width: 100px;}

左边不定宽,右边自适应

float + overflow

.left{ float: left;}.right{ margin-left: 20px; overflow: hidden;}

table

.parent{ display: table; width: 100%;}.left,.right{ display: table-cell;}.left{ width: 0.1%;}.left{ padding-left: 10px;}

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>

table

.parent-fix{ margin-left: -20px;}.parent{ display: table; width: 100%; table-layout: fixed;}.child{ display: table-cell; padding-left: 20px;}

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>

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

flex

.parent{ display: flex;}.right{ flex: 1;}.left{ width: 100px; margin-right: 20px;}

float

//部分UI框架采用的就是这种方式,.parent{ overflow: hidden;}.left{ float: left; margin-right: 20px;}.right{ overflow: hidden;}.left,.right{ padding-bottom: 9999px; margin-bottom: -9999px;}

来源:爱蒂网