<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> header footer div{ border: 1px solid red; } header{ width: 100%; height: auto; border: 1px solid red; margin-bottom: 10px; position: relative; } .content{ border: 1px solid blue; } .logo{ height: 100px; width: 100px; float: left; border: 1px solid red; margin: 10px; } .user{ position:absolute; height: 50px; width: 200px; float: right; border: 1px solid red; bottom: 10px; right: 10px; } .aside{ float: right; width: 200px; border: 1px solid red; height: 100px; margin-left: 10px; } .clear{ clear: both; line-height: 0; height: 0; } .content{ height: 500px; border: 1px solid red; margin-right: 200px; float: left; } footer{ width: 100%; height: 100px; border: 1px solid red; margin-top: 10px; } </style> </head> <body> <header> <div class="logo"></div> <div class="user"></div> <div class="clear"></div> </header> <div class="content">左侧内容</div> <div class="aside"></div> <div class="clear"></div> <footer></footer> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>考基本布局和自适应双栏布局</title> <style> .header { width: 100%; padding: 15px; display: flex; justify-content: space-between; align-items: baseline; border: 1px solid green; margin-bottom: 15px; box-sizing: border-box; } .logo { width: 100px; height: 100px; border: 1px solid blueviolet; } .cententCenter { width: 100%; display: flex; justify-content: space-between; margin-bottom: 15px; } .content { width: calc(100% - 230px); height: 400px; border: 1px solid brown; } .aside { width: 200px; height: 50px; border: 1px solid black; } .footer { width: 100%; height: 50px; border: 1px solid darkgrey; } </style> </head> <body> <div class="header"> <div class="logo"></div> <input class="userName" placeholder="用户名" type="text"> </div> <div class="cententCenter"> <div class="content"></div> <div class="aside"></div> </div> <div class="footer"></div> </body> </html>
absolute+margin
<div id="content">
<div id="main"> 主内容栏自适应宽度</div>
<div id="aside"> 侧边栏固定宽度</div>
</div>
#aside{
position:absolute;
top:0;
left|right:0;
width:200px;
}
#main{
margin-left|right:210px; // 参考侧边固定宽度
}
优点:
可任意调换位置
可主内容优先显示
缺点:
侧边无法撑开父元素的高度,会溢出父元素区域。
float+margin
<div id="content">
<div id="aside"> 侧边栏固定宽度</div>
<div id="main"> 主内容栏自适应宽度</div>
</div>
#aside{
float:left|right;
width:200px;
}
#main{
margin-left|right:210px;// 参考侧边固定宽度
}
优点:
可任意调换位置
父元素自适应高度
缺点:
不支持主内容优先显示
float+-margin
<div id="content">
<div id="main"> 主内容栏自适应宽度</div>
<div id="aside"> 侧边栏固定宽度</div>
</div>
#content{
padding-left:210px; // 参考侧边固定宽度
}
#main{
float:left;
width:100%;
}
#aside{
float:left;
position:relative;
width:200px;
left:-210px;// 等于包含块内补白
margin-left:-100%;
}
#content{
padding-right:210px; // 参考侧边固定宽度
}
#aside{
float:left;
position:relative;
width:200px;
right:-210px; // 等于包含块内补白
margin-left:-200px;
}
#main{
float:left;
width:100%;
}
<!-- 头部 --> <header> <div class="logo"> logo </div> <div class="username"> 用户名 </div> <div class="clearfloat"></div> </header> <!-- 主要内容 --> <div class="wrap"> <div class="content"> content-自适应宽度 </div> <aside> aside-宽度200px </aside> <div class="clearfloat"></div> </div> <!-- 页脚 --> <footer> footer </footer>
header{ border:1px solid green; } .clearfloat{ clear:both; } .logo{ float:left; height:60px; width:60px; margin:10px; border:1px solid purple; } .username{ float:right; margin-right:10px; margin-top:49px; width:120px; text-align:right; border:1px solid black; } .wrap{ width:100%; } .content{ border:1px solid blue; margin-top:10px; float:left; width:calc(100% - 220px); height:400px; height:calc(100vh - 150px); } aside{ border:1px solid red; float:right; margin-top:10px; width:200px; } footer{ border:1px solid black; margin-top:10px; text-align:center; }
body { margin: 0; } .fn-clear:after { content: " "; clear: both; display: block; font-size: 0; visibility: hidden; height: 0; } .fn-clear { zoom: 1; } .container { padding: 10px; } .header { background: #eee; position: relative; margin-bottom: 10px; } .logo { width: 100px; height: 100px; float: left; background: #f60; } .username { position: absolute; right: 10px; bottom: 10px; } .main { margin-bottom: 10px; } .side-bar { width: 200px; float: right; background: #ccc; } .content { margin-right: 200px; background: #f6f6f6; } .footer { background: #999; }
<div class="container"> <div class="header fn-clear"> <div class="logo">logo</div> <div class="username">zhoumingXXX@163.com</div> </div> <div class="main"> <div class="side-bar">menu</div> <div class="content">左侧内容</div> </div> <div class="footer"> footer </div> </div>
<!DOCTYPE html> <meta charset=utf-8> <html> <head> <title>alibaba</title> <style type="text/css"> .header{ overflow: hidden; margin: 5px; border: 1px solid #000; height: 70px; } .main{ margin: 5px; overflow: hidden; /* border: 1px solid #000;*/ } .footer{ text-align: center; margin: 5px; border: 1px solid #000; } .logo{ width: 50px; height: 50px; float: left; margin: 10px; border: 1px solid #000; } .username{ border: 1px solid #000; float: right; width: 140px; height: 20px; margin-top: 40px; margin-right: 10px; text-align: right; } .column{ border: 1px solid #000; } .left{ float: left; width: 100%; } .right{ float: left; width: 200px; margin-left: -204px; } .real{ margin-right: 210px; border: 1px solid blue; height: 300px; } </style> </head> <body> <div class="header"> <div class="logo">logo</div> <div class="username">用户名</div> </div> <div class="main"> <div class="left column"> <div class="real">content - 自适应宽度</div> </div> <dis class="right column">aside - 定宽200px</div> </div> <div class="footer">footer</div> </body> </html>参考了淘宝的双飞翼布局,column类均左浮动,侧边栏aside通过负值左外边距得以和left column共处一行,而真正的自适应内容则放置在left column的内部,使用右外边距得以和right column保持固定距离
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> body,html { height:calc(100% - 16px); width: calc(100% - 16px); } header { height: 150px; border: 1px black solid; } .middle { height: calc(100% - 250px); } .content { float: right; width: 200px; height: 100%; background-color: #00FFD1; } .aside { height: 100%; margin-right: 200px; background-color: deeppink; } footer { height: 50px; border: 1px black solid; } </style> </head> <body> <header><img class="logo" alt="logo"></header> <div class="middle"> <div class="content"></div> <div class="aside"></div> </div> <footer></footer> </body> </html>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><style>.header{width: 90%;height: 100px;margin: 0 auto;border: 1px solid greenyellow;}.header .logo{width: 50px;height: 50px;border: 1px solid red;margin-top: 25px;}.header .user{width: 100px;height: 30px;float: right;border: 1px solid black;margin-top: 50px;}.content{width: 90%;margin: 0 auto;}.content .content-left{height: 300px;border: 1px solid blue;margin-right: 210px;}.content .content-right{width: 200px;float: right;border: 1px solid rebeccapurple;}.footer{width: 90%;height: 50px;margin: 0 auto;border: 1px solid black;}</style><body><div class="header"><div class="user">用户名</div><div class="logo">logo</div></div><div class="content"><div class="content-right">aside-定宽200px</div><div class="content-left">content-自适应宽度</div></div><div class="footer">footer</div></body></html>
<!doctype html> <html> <head> <meta charset="utf-8" > <style> *{ padding:0; margin:0; } #head{ display:flex; padding:10px; background:red; align-items:flex-end; justify-content:space-between; } #user{ } #main{ height:400px; display:flex; flex-direction:row-reverse; background:blue; } #aside{ background:yellow; width:200px; } #footer{ padding:10px; background:#ccc; } </style> </head> <body> <header id="head"> <span><img src="log.jpg" alt="logo图片"/></span> <span id="user">用户名</span> </header> <div id="main"> <section id="content"> </section> <aside id="aside"> </aside> </div> <footer id="footer"> </footer> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> header{ overflow: hidden; border: 1px solid #0f0; padding: 5px; margin: 5px; } .logo{ float: left; height: 50px; width: 50px; border: 1px solid #f00; } .user{ float: right; height: 20px; width: 100px; margin-top: 30px; border: 1px solid #000; } article{ height: 500px; border: 1px solid #00f; margin: 5px; margin-right: 250px; } aside{ float: right; width: 200px; border: 1px solid #f00; } footer{ text-align: center; margin: 5px; border: 1px solid #000; } </style> </head> <body> <header> <div class="logo">Logo</div> <div class="user">用户名</div> </header> <main> <aside>aside-定宽200px</aside> <article>content-自适应宽度</article> </main> <footer>footer</footer> </body> </html>