前端常用-面试常考-的布局问题

1 左边固定,右边自适应

           css:
            * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			html, body {
				height: 100%;
			}
			.container {
				padding-left: 200px;
				position: relative;
				height: 100%;
			}
			.left {
				position: absolute;
				width: 200px;
				height: 100%;
				top: 0;
				left: 0;
				background: red;
			}

        html:
        <div class="container">
			<div class="left">这边是导航</div>
			<div class="con">
				这里是内容盒子
			</div>
		</div>

效果图:

2 右边固定 

            * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			html, body {
				height: 100%;
			}
			.container {
				padding-right: 200px;
				position: relative;
				height: 100%;
			}
			.left {
				position: absolute;
				width: 200px;
				height: 100%;
				top: 0;
				right: 0;
				background: red;
			}
        <div class="container">
			<div class="left">这边是导航</div>
			<div class="con">
				这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子
				这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子
			</div>
		</div>

 效果图

3 两边固定,中间自适应

            * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			html, body {
				height: 100%;
			}
			.container {
				padding-left: 200px;
				padding-right: 200px;
				position: relative;
				height: 100%;
			}
			.left {
				position: absolute;
				width: 200px;
				height: 100%;
				top: 0;
				left: 0;
				background: red;
			}
			.right {
				position: absolute;
				width: 200px;
				height: 100%;
				top: 0;
				right: 0;
				background: red;
			}
        <div class="container">
			<div class="left">这边是导航</div>
			<div class="right">这边是导航</div>
			<div class="con">
				这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子
				这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子
			</div>
		</div>

 效果图:

 4 四周固定,中间自适应

            * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			html, body {
				height: 100%;
			}
			.container {
				position: relative;
				height: 100%;
			}
			/* 写法一  */
			/*.con {
				padding: 100px;
			}*/
			/*  写法二  */
			.con {
				position: absolute;
				top: 100px;
				left: 100px;
				bottom: 100px;
				right: 100px;
			}
			.left {
				position: absolute;
				width: 100px;
				top: 100px;
				bottom: 0;
				background: red;
				left: 0;
			}
			.right {
				position: absolute;
				width: 100px;
				top: 0;
				right: 0;
				bottom: 100px;
				background: yellow;
			}
			.top {
				position: absolute;
				height: 100px;
				background: deepskyblue;
				top: 0;
				left: 0;
				right: 100px;
			}
			.bottom {
				position: absolute;
				height: 100px;
				background: grey;
				bottom: 0;
				right: 0;
				left: 100px;
			}
        <div class="container">
			<div class="top">这边是头部这边是头部这边是头部这边是头部这边是头部这边是头部------------------------------这边是头部这边是头部这边是头部这边是头部这边是头部这边是头部</div>
			<div class="left">这边是导航</div>
			<div class="bottom">这边是导航</div>
			<div class="right">这边是导航</div>
			<div class="con">
				这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子
				这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子这里是内容盒子
			</div>
		</div>

效果图:

绝对居中问题:

1 固定宽高

        css:
          * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			/*已知宽高,绝对居中*/
		    div {
		    	width: 200px;
		    	height: 200px;
		    	background: red;
		    	position: absolute;
		    	top: 50%;
		    	left: 50%;
		    	/*写法一:IE9+*/
		    	/*transform: translate(-50%, -50%);*/
		    	/*写法二:兼容性高*/
		    	margin-top: -100px;
		    	margin-left: -100px;
		    }
        html:
            <div></div>
        

 2 固定宽度,高度不定

css:
        * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			/*已知宽高,绝对居中*/
		    div {
		    	width: 100px;
		    	background: red;
		    	position: absolute;
		    	top: 50%;
		    	left: 50%;
		    	/*写法一:IE9+*/
		    	transform: translate(-50%, -50%);
		    }
html:

            <div>韩联社11月22日消息,韩国国家情报院(国情院)22日在国会
			举行的情报委员会闭门会议上表示,韩朝近期将举行首脑会谈。
			由于上次会谈在平壤举行,韩方将力促此次会谈在韩国举行。</div>

效果: 

3 宽高均不固定

css:
            * {
				padding: 0;
				margin: 0;
				list-style: none;
			}
			html, body {
				height: 100%;
			}
			.container {
				height: 100%;
				width: 100%;
				text-align: center;
				display: table;
				overflow:hidden;
			}
			.con {
				display:table-cell;
				vertical-align: middle;
			}
			span {
				display:inline-block;
				width: 15px;
				height: 8px;
				background: url(img/slide.PNG) no-repeat;
				background-size: contain;
				margin-left: 4px;
			}
html:
        <div class="container">
			<div class="con">
				点击加载更多<span></span>
			</div>
		</div>

 效果图:

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值