PC 端布局与移动端布局
PC 端布局
1、示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PC端通用布局</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: #ccc;
}
body {
min-width: 800px;
background-color: #ccc;
/* flex布局 */
display: flex;
/* 主轴为垂直方向 */
flex-flow: column nowrap;
}
header,
footer {
height: 50px;
background-color: #333;
}
header {
/* flex布局 */
display: flex;
}
header > a {
width: 100px;
height: 50px;
/* 使a标签内文本居中 */
line-height: 50px;
text-align: center;
}
header > a:not(:first-of-type):hover {
color: white;
background-color: seagreen;
}
header > a:first-of-type {
margin: 0 50px 0 10px;
}
header > a:last-of-type {
margin: 0 10px 0 auto;
}
.container {
min-height: 600px;
display: flex;
margin: 10px auto;
}
.container > aside:first-of-type {
width: 200px;
border: 1px solid #000;
}
.container > main {
min-width: 600px;
border: 1px solid #000;
margin: 0 10px;
}
.container > aside:last-of-type {
width: 200px;
border: 1px solid #000;
}
footer {
display: flex;
flex-flow: column nowrap;
align-items: center;
}
</style>
</head>
<body>
<header>
<a href="">LOGO</a>
<a href="">项目1</a>
<a href="">项目2</a>
<a href="">项目3</a>
<a href="">项目4</a>
<a href="">项目5</a>
<a href="">登录/注册</a>
</header>
<div class="container">
<aside>左边栏</aside>
<main>主体内容区</main>
<aside>右边栏</aside>
</div>
<footer>
<p>
php中文网© 版权所有 (2018-2022)|备案号:
<a href="">皖ICP-18*********</a>
</p>
<p>中国.合肥市政务新区xxx号|电话:*********</p>
</footer>
</body>
</html>
2、显示效果
移动端布局
1、示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="static/css/font-icon.css" />
<title>移动端布局</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: #000;
}
html {
width: 100vw;
height: 100vh;
font-size: 14px;
}
body {
display: flex;
flex-flow: column nowrap;
}
header {
width: 100vw;
background-color: #ccc;
padding: 0 5px;
/* flex布局 两端对齐 */
display: flex;
justify-content: space-between;
/* 固定位置 */
position: fixed;
}
.slider > img {
width: 100vw;
}
nav {
margin: 10px 0;
height: 20vh;
display: flex;
/* 转成多行容器 分散对齐 */
flex-flow: row wrap;
align-content: space-around;
}
nav > div {
width: 25vw;
/* 主轴方向转为垂直方向 */
display: flex;
flex-flow: column nowrap;
justify-content: space-around;
}
nav > div > a > img {
width: 60%;
}
nav > div > a {
text-align: center;
}
h2 {
border-bottom: 1px solid #000;
text-align: center;
}
.hot-goods {
margin: 10px 0;
display: flex;
flex-flow: row wrap;
}
.hot-goods > .goods-item {
width: 33vw;
text-align: center;
}
.hot-goods > .goods-item img {
width: 60%;
}
.hot-goods > .goods-item {
font-size: 0.9rem;
}
.goods-list {
margin: 10px 0;
display: flex;
flex-flow: column nowrap;
}
.goods-list > .goods-desc {
display: flex;
align-items: center;
}
.goods-list > .goods-desc img {
width: 100%;
}
.goods-list > .goods-desc a {
padding-right: 10px;
}
footer {
width: 100vw;
background-color: #ccc;
padding: 5px 5px;
display: flex;
justify-content: space-around;
}
footer > div {
display: flex;
flex-flow: column nowrap;
align-items: center;
}
</style>
</head>
<body>
<!-- 页眉 -->
<header>
<a href="">LOGO</a>
<a href=""><span class="iconfont"></span></a>
</header>
<!-- 轮播图 -->
<div class="slider">
<img src="static/images/banner.jpg" alt="" />
</div>
<!-- 主导航区 -->
<nav>
<div>
<a href=""><img src="static/images/link1.webp" alt="" /></a>
<a href="">京东超市</a>
</div>
<div>
<a href=""><img src="static/images/link2.webp" alt="" /></a>
<a href="">服装百货</a>
</div>
<div>
<a href=""><img src="static/images/link3.webp" alt="" /></a>
<a href="">数码精品</a>
</div>
<div>
<a href=""><img src="static/images/link4.webp" alt="" /></a>
<a href="">优惠券</a>
</div>
<div>
<a href=""><img src="static/images/link1.webp" alt="" /></a>
<a href="">京东超市</a>
</div>
<div>
<a href=""><img src="static/images/link2.webp" alt="" /></a>
<a href="">服装百货</a>
</div>
<div>
<a href=""><img src="static/images/link3.webp" alt="" /></a>
<a href="">数码精品</a>
</div>
<div>
<a href=""><img src="static/images/link4.webp" alt="" /></a>
<a href="">优惠券</a>
</div>
</nav>
<!-- 热销商品区 -->
<h2>热销商品<span class="iconfont"></span></h2>
<div class="hot-goods">
<div class="goods-item">
<a href=""><img src="static/images/goods1.jpg" alt="" /></a>
<p>Apple iphone 11 128G</p>
<div>
<span>1111 元</span>
<a href=""><span class="iconfont"></span></a>
</div>
</div>
<div class="goods-item">
<a href=""><img src="static/images/goods2.jpg" alt="" /></a>
<p>华为笔记本</p>
<div>
<span>1111 元</span>
<a href=""><span class="iconfont"></span></a>
</div>
</div>
<div class="goods-item">
<a href=""><img src="static/images/goods3.jpg" alt="" /></a>
<p>海尔洗衣机</p>
<div>
<span>1111 元</span>
<a href=""><span class="iconfont"></span></a>
</div>
</div>
<div class="goods-item">
<a href=""><img src="static/images/goods4.jpg" alt="" /></a>
<p>Apple iphone X 128G</p>
<div>
<span>1111 元</span>
<a href=""><span class="iconfont"></span></a>
</div>
</div>
<div class="goods-item">
<a href=""><img src="static/images/goods5.png" alt="" /></a>
<p>荣耀手机 128G</p>
<div>
<span>1111 元</span>
<a href=""><span class="iconfont"></span></a>
</div>
</div>
<div class="goods-item">
<a href=""><img src="static/images/goods5.png" alt="" /></a>
<p>荣耀手机 128G</p>
<div>
<span>1111 元</span>
<a href=""><span class="iconfont"></span></a>
</div>
</div>
</div>
<!-- 商品列表 -->
<h2>商品列表<span class="iconfont"></span></h2>
<div class="goods-list">
<div class="goods-desc">
<a href=""><img src="static/images/goods1.jpg" alt="" /></a>
<a href="">
[白条什么什么的]随便买信息时代受到极大激发好的
<span class="iconfont"></span>
</a>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/goods2.jpg" alt="" /></a>
<a href="">
[白条什么什么的]随便买信息时代受到极大激发好的
<span class="iconfont"></span>
</a>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/goods3.jpg" alt="" /></a>
<a href="">
[白条什么什么的]随便买信息时代受到极大激发好的
<span class="iconfont"></span>
</a>
</div>
<div class="goods-desc">
<a href=""><img src="static/images/goods4.jpg" alt="" /></a>
<a href="">
[白条什么什么的]随便买信息时代受到极大激发好的
<span class="iconfont"></span>
</a>
</div>
</div>
<!-- 页脚 -->
<footer>
<div>
<a href=""><span class="iconfont"></span></a>
<a href="">首页</a>
</div>
<div>
<a href=""><span class="iconfont"></span></a>
<a href="">分类</a>
</div>
<div>
<a href=""><span class="iconfont"></span></a>
<a href="">购物车</a>
</div>
<div>
<a href=""><span class="iconfont"></span></a>
<a href="">登录</a>
</div>
</footer>
</body>
</html>
2、显示效果
总结
- 页面布局先需要设计页面框架
- 从框架上去找 flex 盒子
- flex 主要是灵活运用以下属性
flex-flow: row/column wrap/nowrap
justify-content
align-content
align-items