一、整体布局
1.页面左侧菜单与右侧内容固定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面布局1</title>
<style>
body{
margin:0 auto;
}
.left{
float:left;
}
.right{
float:right;
}
.pg-header{
height:48px;
background-color:#2459a2;
color:white;
}
.pg-content .menu{
position:fixed;
top:48px;
left:0;
bottom: 0;
width:200px;
background-color:red;
}
.pg-content .content{
position:fixed;
top:48px;
right:0;
bottom:0;
left:200px;
background-color: green;
overflow: auto;
}
</style>
</head>
<body>
<div class="pg-header"></div>
<div class="pg-content">
<div class="menu left">aa</div>
<div class="content left">
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
<p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p>
</div>
</div>
<div class="pg-footer"></div>
</body>
</html>
2.左侧菜单跟随右侧内容滑动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面布局2</title>
<style>
body{
margin:0 auto;
}
.left{
float:left;
}
.right{
float:right;
}
.pg-header{
height:48px;
background-color:#2459a2;
color:white;
}
.pg-content .menu{
position:absolute;
top:48px;
left:0;
bottom: 0;
width:200px;
background-color:red;
}
.pg-content .content{
position:absolute;
top:48px;
right:0;
bottom:0;
left:200px;
min-width: 800px;
background: #f1c500;
/*overflow: auto;*/
}
</style>
</head>
<body>
<div class="pg-header"></div>
<div class="pg-content">
<div class="menu left">aa</div>
<div class="content left">
<!--<div style="background-color: #f1c500">-->
<p style="margin: 0">ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<p>ff</p><p>ff</p><p>ff</p><p>ff</p><p>ff</p>
<!--</div>-->
</div>
</div>
<div class="pg-footer"></div>
</body>
</html>
强调:通过增加overflow:auto 就可以实现两种方式的转换。
二、详细布局
页面的具体展示效果,如下图所示:
具体代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>页面布局2</title>
<!--将需要用的图标文件导入进来,在这里使用压缩版-->
<link rel="stylesheet" href="fontawesome-free-5.7.2-web/css/all.min.css"/>
<style>
body {
margin: 0 auto;
z-index: 1;
}
.left {
float: left;
}
.right {
float: right;
}
.pg-header {
height: 48px;
background-color: #2459a2;
color: white;
line-height: 48px;
}
.pg-header .logo {
width: 200px;
background-color: cadetblue;
text-align: center;
}
.pg-header .icons {
padding: 0 20px;
}
.pg-header .icons:hover {
background-color: #204982;
}
.pg-header .icons span {
/*将图标变为圆形,并有数字提醒,line-height会有超出范围现象,这里设置1px;通过调整padding让圆形变圆*/
border-radius: 50%;
line-height: 1px;
display: inline-block;
padding: 10px 7px;
background-color: red
}
.pg-header .user {
position: relative;
width: 60px;
padding: 0 20px;
color: white;
height: 48px;
margin-right: 160px;
}
.pg-header .user:hover {
background-color: #204982;
}
.pg-header .user:hover .b {
/*鼠标点到的时候,将标签内的内容显示出来*/
display: block;
}
.pg-header .user .a img {
height: 40px;
width: 40px;
margin-top: 4px;
border-radius: 50%;
}
.pg-header .user .b {
/*固定显示出来的位置,并设置好显示内容的格式信息*/
position: absolute;
width: 80px;
top: 48px;
right: 10px;
overflow: auto;
z-index: 2;
background-color: red;
display: none
}
.pg-header .user .b a {
display: block;
}
.pg-content .menu {
position: absolute;
top: 48px;
left: 0;
bottom: 0;
width: 200px;
background-color: red;
}
.pg-content .content {
position: absolute;
top: 48px;
right: 0;
bottom: 0;
left: 200px;
min-width: 800px;
background: #f1c500;
/*overflow: auto;*/
}
</style>
</head>
<body>
<div class="pg-header">
<div class="logo left">五朵香花</div>
<div class="user right">
<a class="a" href="">
<img src="dingxiang.jpg"/>
</a>
<div class="b">
<a>我的资料</a>
<a>注销</a>
</div>
</div>
<div class="right icons"><i class="fas fa-bullhorn"></i>
<span>5</span>
</div>
<div class="right icons"><i class="far fa-envelope"></i></div>
</div>
<div class="pg-content">
<div class="menu left">aa</div>
<div class="content left">
<!--<div style="background: #f1c500">-->
<p style="margin: 0">ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<p>ff</p>
<!--</div>-->
</div>
</div>
<div class="pg-footer"></div>
</body>
</html>
该实例将相应的布局整理出来,可以进行参考。里面有图标的设置等信息,图标网址:https://fontawesome.com/