通过flex来完成一个移动端首页的布局练习
目标效果图(因为页面太大,缩小截图有点问题,可以看下面的分析图)
页面结构分析
按照结构分析图来写HTML架构
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用flex完成PHP中文网(移动端)的布局</title>
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="static/font/iconfont.css">
</head>
<body>
<!--头部-->
<header>
<a href=""><img src="static/images/user.jpg" alt=""></a>
<a href=""><img src="static/images/logo.png" alt=""></a>
<a href=""><i class="iconfont icon-ai-list"></i></a>
</header>
<!-- 轮播图-->
<div class="banner">
<img src="static/images/banner.jpg" alt="">
</div>
<!-- 导航菜单-->
<nav>
<ul>
<li>
<a href="">
<img src="static/images/html.png" alt="">
<span>HTML/CSS</span>
</a>
</li>
<li>
<a href="">
<img src="static/images/JavaScript.png" alt="">
<span>JavaScipt</span>
</a>
</li>
<li>
<a href="">
<img src="static/images/code.png" alt="">
<span>服务器端</span>
</a>
</li>
<li>
<a href="">
<img src="static/images/sql.png" alt="">
<span>数据库</span>
</a>
</li>
</ul>
<ul>
<li>
<a href="">
<img src="static/images/app.png" alt="">
<span>移动端</span>
</a>
</li>
<li>
<a href="">
<img src="static/images/manual.png" alt="">
<span>手册</span>
</a>
</li>
<li>
<a href="">
<img src="static/images/tool2.png" alt="">
<span>工具</span>
</a>
</li>
<li>
<a href="">
<img src="static/images/live.png" alt="">
<span>直播</span>
</a>
</li>
</ul>
</nav>
<!--主体-->
<main>
<!-- 推荐课程-->
<div class="kecheng">
<h3>推荐课程</h3>
<div>
<a href=""><img src="static/images/tl1.jpg" alt=""></a>
<a href=""><img src="static/images/tl2.jpg" alt=""></a>
</div>
<div>
<div>
<a href=""><img src="static/images/wz1.jpg" alt=""></a>
<div>
<a href="">CI框架30分钟极速入门</a>
<div>
<span>中级</span>
<span>55871次播放</span>
</div>
</div>
</div>
<div>
<a href=""><img src="static/images/wz2.jpg" alt=""></a>
<div>
<a href="">2018前端入门_HTML5</a>
<div>
<span>中级</span>
<span>55871次播放</span>
</div>
</div>
</div>
</div>
</div>
<!--最新更新-->
<div class="new">
<h3>最新更新</h3>
<div>
<a href=""><img src="static/images/wz3.jpg" alt=""></a>
<div>
<a href="">2019python自学视频</a>
<span>本课程适合想从零开始学习 Python 编程语言的开发人员。由浅入深的</span>
<div>
<span>初级</span>
<span>3654播放</span>
</div>
</div>
</div>
<div>
<a href=""><img src="static/images/wz4.png" alt=""></a>
<div>
<a href="">PHP开发免费公益直播课</a>
<span>本课程适合想从零开始学习 Python 编程语言的开发人员。由浅入深的</span>
<div>
<span>初级</span>
<span>3654播放</span>
</div>
</div>
</div>
<div>
<a href=""><img src="static/images/wz5.jpg" alt=""></a>
<div>
<a href="">从零开始到WEB响应式布局</a>
<span>本课程适合想从零开始学习 Python 编程语言的开发人员。由浅入深的</span>
<div>
<span>初级</span>
<span>3654播放</span>
</div>
</div>
</div>
<div>
<a href=""><img src="static/images/wz6.png" alt=""></a>
<div>
<a href="">PHP文件基础操作</a>
<span>本课程适合想从零开始学习 Python 编程语言的开发人员。由浅入深的</span>
<div>
<span>初级</span>
<span>3654播放</span>
</div>
</div>
</div>
</div>
<!--最新文章-->
<div class="article">
<h3>最新文章</h3>
<div>
<div>
<a href="">5个PHP开发者应该知道的Composer小技巧</a>
<span>发布时间:2019-12-31</span>
</div>
<a href=""><img src="static/images/wz1.jpg" alt=""></a>
</div>
<div>
<div>
<a href="">解决PHP中Web程序中shell_exec()执行Shell脚本不成功问题</a>
<span>发布时间:2019-12-31</span>
</div>
<a href=""><img src="static/images/wz2.jpg" alt=""></a>
</div>
<div>
<div>
<a href="">PHP执行Linux命令的两个有用的函数exec和shell_exec</a>
<span>发布时间:2019-12-31</span>
</div>
<a href=""><img src="static/images/wz3.jpg" alt=""></a>
</div>
<div>
<div>
<a href="">php实现利用expat方式解析xml文件</a>
<span>发布时间:2019-12-31</span>
</div>
<a href=""><img src="static/images/wz4.png" alt=""></a>
</div>
<h4><a href="">更多内容</a></h4>
</div>
<!-- 最新博文-->
<div class="blog">
<h3>最新博文</h3>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<h4><a href="">更多内容</a></h4>
</div>
<!-- 最新问答-->
<div class="wd">
<h3>最新问答</h3>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<div>
<a href="">composer安装laravel,nginx服务器路配置解决404问题,创建简单的</a>
<span>2019-12-27</span>
</div>
<h4><a href="">更多内容</a></h4>
</div>
</main>
<!--尾部-->
<footer>
<a href="" ><i class="iconfont icon-shouye"></i>首页</a>
<a href="" ><i class="iconfont icon-bofang"></i>视频</a>
<a href="" ><i class="iconfont icon--shequ"></i>社区</a>
<a href="" ><i class="iconfont icon-renyuanguanli"></i>我的</a>
</footer>
</body>
</html>
运行效果如下(没有添加样式表):
根据页面架构按顺序写样式表
index样式表
@import "public_reset.css";
/*头部*/
header {
/*固定在顶部不动*/
position: fixed;
top: 0;
width: 100%;
max-width: 768px;
height: 44px;
background-color: #282828;
display: flex;
/*垂直居中对齐*/
align-items: center;
/*两端对齐*/
justify-content: space-between;
}
header > a > i {
color: #ffffff;
/*默认字体的1.2倍*/
font-size: 1.2rem;
margin-right: 10px;
}
header > a > img:first-of-type,
header > img:last-of-type {
width: 28px;
height: 28px;
margin: 5px;
}
header > a > img:first-of-type {
border-radius: 50%;
}
header a:nth-of-type(2) img{
width: 92px;
height: 44px;
display: block;
}
/*轮播图*/
.banner {
display: flex;
height: 200px;
margin-top: 44px;
}
/*导航菜单*/
nav {
background-color: #ffffff;
display: flex;
flex-flow: column nowrap;
}
nav img {
width: 48px;
height: 48px;
}
nav > ul {
display: flex;
}
nav > ul > li {
/*分配全部剩余空间*/
flex: 1;
}
nav > ul > li > a {
display: flex;
/*列对齐允许换行*/
flex-flow: column wrap;
/*垂直居中*/
align-items: center;
margin: 10px;
}
nav > ul > li > a {
margin-top: 5px;
}
/*主体*/
main {
display: flex;
flex-direction: column;
}
/*推荐课程*/
/*天龙八部*/
main > .kecheng > div:first-of-type {
display: flex;
}
main > .kecheng > div:first-of-type > a {
margin: 5px;
flex: 1;
}
main > .kecheng > div:first-of-type > a > img {
height: 90px;
}
/*带标题的课程*/
main > .kecheng > div:last-of-type {
display: flex;
flex-flow: column;
}
main > .kecheng > div:last-of-type > div {
background-color: #fff;
margin: 5px;
height: 100px;
display: flex;
}
main > .kecheng > div:last-of-type > div img {
width: 350px;
height: 100px;
}
main > .kecheng > div:last-of-type > div > div {
display: flex;
flex: 1;
flex-direction: column;
margin-top: 10px;
padding-left: 10px;
}
main > .kecheng > div:last-of-type > div > div a {
font-size: 16px;
}
main > .kecheng > div:last-of-type > div > div {
display: flex;
color: #888888;
}
main > .kecheng > div:last-of-type > div > div span:first-of-type {
background-color: #dddddd;
color: #ffffff;
border-radius: 3px;
padding: 0 5px;
}
/*最新更新*/
main > .new {
display: flex;
flex-direction: column;
}
main > .new > div {
display: flex;
justify-content: space-between;
background-color: #fff;
margin: 5px;
height: 100px;
}
main > .new > div img {
width: 350px;
height: 100px;
}
main > .new > div > div {
display: flex;
flex-direction: column;
margin: 5px;
}
main > .new > div > div a {
font-size: 16px;
}
main > .new > div > div span {
color: #666666;
}
main > .new > div > div > div span:first-of-type {
background-color: #dddddd;
color: #ffffff;
border-radius: 3px;
padding: 0 5px;
}
/*最新文章*/
main > .article {
display: flex;
flex-direction: column;
}
main > .article > div {
display: flex;
background-color: #fff;
margin: 5px;
height: 100px;
justify-content: space-between;
}
main > .article > div img {
width: 350px;
height: 100px;
}
main > .article > div > div {
display: flex;
flex-direction: column;
margin: 5px;
}
main > .article > div > div a {
font-size: 16px;
}
main > .article > div > div span {
color: #666666;
}
/*最新博文*/
main > .blog >
{
display: flex;
flex-direction: column;
}
main > .blog > div {
display: flex;
background-color: #fff;
margin: 5px;
padding: 10px;
box-sizing: border-box;
height: 50px;
justify-content: space-between;
align-items: center;
box-shadow: 0 2px 4px #888888;
}
/*最新问答*/
main > .wd >
{
display: flex;
flex-direction: column;
}
main > .wd > div {
display: flex;
background-color: #fff;
margin: 5px;
padding: 10px;
box-sizing: border-box;
height: 50px;
justify-content: space-between;
align-items: center;
box-shadow: 0 2px 4px #888888;
}
h4 {
display: block;
line-height: 10px;
text-align: center;
background: #fff;
padding: 10px 0;
}
/*尾部*/
footer {
display: flex;
justify-content: space-around;
position: fixed;
bottom: 0;
width: 100%;
max-width: 768px;
height: 50px;
border-top: 1px solid #ccc;
margin-top: 10px;
background-color: #ffffff;
}
footer > a {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
公共样式表
/*初始化浏览器*/
body {
background-color: #cccccc;
/*设置页面宽度*/
max-width: 768px;
/*设置居中*/
margin: 0 auto;
}
a {
color: #282828;
/*去掉字体下划线*/
text-decoration: none;
font-size: 14px;
}
img {
width: 100%;
}
ul, li {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
最终效果
最后总结
1、初学者(尤其是英语不好的)一定要做好笔记和注释,这样记不得时可以翻看前面的笔记
2、在写HTML之前一定要把页面的架构理清,区块划分好,这样写的代码更清晰
3、在写这个页面时出现一个问题,页面中最后的两个区块(最新博文和最新问答)的样式一样,我想使用
main > .blog >,
main > .wd >
{}
这种写法,但运行时不生效(只有一个区块生效),是什么原因?