前端面试之CSS布局

一、三栏布局:
1. 高度已知,左右两栏固定,中间自适应

(1) float实现

<section class="float">
    <article>
        <div class="left"></div>
        <div class="right"></div>
        <div class="center"></div>
    </article>
</section>

<style>
    .float .left {
        width: 300px;
        float: left;
        background: green;
     }
    .float .center {
    	margin-left: 300px;
    	margin-right: 300px;
        background: yellow;
        
     }
    .float .right {
        width: 300px;
        float: right;
        background: red;
     }
</style>

(2)absolute实现

<section class="absolute">
    <article>
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </article>
</section>

<style>
   .absolute article > div {
        position: absolute;
    }
   .absolute .left {
   		width: 300px;
        left: 0;
        background: green;
    }
    .absolute .center {
		left: 300px;
		right: 300px;
 		background: yellow;
	}
	.absolute .right {  
		width: 300px;
		right: 0;
		background: red;
	}
</style>

(3)flex实现

<section class="flex">
    <arctile class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </arctile>
</section>

<style>
	.flex .left-center-right {
		display: flex;
	}
	.flex .left {
		width: 300px;
		background: green;
	}
	.flex .center {
		flex:1;
		background: yellow;
	}
	.flex .right {
		width: 300px;
		background: red;
	}
</style>

(4)table实现

<section class="table">
    <arctile class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </arctile>
</section>

<style>
	.table .left-center-right {
		width: 100%;
		height: 100px;
		display: table;
	}
	.table .left-center-right div {
		display: table-cell;
	}
	.table .left {
		width: 300px;
		background: green;
	}
	.table .center {
		background: yellow;
	}
	.table .right {
		width: 300px;
		background: red;
	}
</style>

(5)gird实现

<section class="grid">
    <arctile class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </arctile>
</section>

<style>
	.grid .left-center-right {
		display: grid;
		width: 100%;
		grid-template-rows : 100px;
		grid-template-columns : 300px auto 300px;
	}
	.grid .left {
		background: green;
	}
	.grid .center {
		background: yellow;
	}
	.grid .right {
		background: red;
	}
</style>
2. 高度未知,左右两栏固定,中间自适应

(1)flex:可用
(2)table:可用

3. 上下固定,中自适应
<body>
    <article class="top"></article>
    <article class="center"></article>
    <article class="bottom"></article>
</body>

<style>
	article {
		width: 100%;
	}
	.top{
		height: 200px;
		background: red;
		position: absolute;
		top: 0;
		left: 0;
	}
	.bottom {
		height: 200px;
		background: blue;
		position: absolute;
		bottom: 0;
		left: 0;
	}
	.center {
		background: yellow;
		position: absolute;
		height: auto;
		top: 200px;
		bottom: 200px;
	}
</style>
二、垂直居中:

(1)弹性盒方法——flex布局

display: flex;
justify-content: center;
align-items: center;

(2)定位+transform

position: relative   //父元素
position: absolute;  //子元素
top: 50%;
left: 50%;
transform: translate(-50%,-50%)

(3)纯absolute

position: relative   //父元素
position: absolute;  //子元素
top: 50%;
left: 50%; 
margin-left:-1/2子元素宽度;
margin-top: -1/2子元素高度;

(4)margin:auto+absolute

position: relative   //父元素
position: absolute; //子元素 
top: 0; 
left: 0; 
right:0; 
bottom:0;
margin:auto

(5)table-cell

display table   //父元素上一层
display: table-cell;   //父元素
vertical-align: middle;
text-align: center;
display: inline-block;  //子元素
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值