前端布局实战-仿php中文网移动端首页
1. 实战心得
这次心得写在最前。实现其实听课的同学基本都能实现,而做实战作业最重要的,我认为是总结实现过程中遇到的坑,并为其中的一些疑惑寻找答案。下面是我的一些心得和疑问,请老师帮忙解惑。
- 一个实战作业,花了半天时间才完成,挺挫败的,里面用到部分老师没有教过的知识,需要去百度,如:标题过长自动隐藏并显示“…”等。
- 一个头疼的问题,各个区块之间有部分相同的样式,也有自己单独的样式,如何安排才能让css样式代码易读性高一些?当我完成编码后,回头再看自己写的css样式,简直是乱到不想再看……
- 一个奇怪的现象,用position把
<header>
和<footer>
分别固定在顶部和底部,在火狐中的预览,有时会出现顶部或底部没有固定在其应在的地方的情况。 - 发现自己一个类似孔乙己茴字有几种写法的怪癖,文字图标的iconfont样式优先级高于元素选择器,不得不为头部/底部文字图标增加单独的样式类,使用类选择器来控制它们的大小。虽然功能实现了,但是总感觉是“曲线救国”的方式,很不舒服。
- 一个纠结的想法,在实现过程中,大量使用了flex弹性布局。这种做法是否欠妥?算不算是“滥用”?
2. 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php中文网</title>
<link rel="stylesheet" href="static/css/font-icon.css">
<style>
html {
width: 100vw;
height: 100vw;
font-size: 14px;
}
body {
margin: 0;
padding: 0;
display: flex;
flex-flow: column nowrap;
flex: 0 0 auto;
background-color: #f1f1f1;
}
header {
/* 弹性布局 */
display: flex;
/* 主轴水平, 不折行 */
flex-flow: row nowrap;
/* 两端对齐 */
justify-content: space-between;
background-color: black;
color: white;
font-size: 2rem;
height: 45px;
align-items: center;
padding: 0 10px;
position: fixed;
top: 0;
width: 95vw;
}
header .head-icon {
font-size: 1.8rem;
}
header img {
height: 38px;
}
body>section:first-of-type {
margin-top: 45px;
height: 150px;
}
body>section:first-of-type img {
height: 150px;
width: 100vw;
}
body .nav {
padding: 10px;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-content: space-between;
background-color: white;
max-height: 172px;
box-sizing: border-box;
}
body .nav .nav-item {
width: 75px;
text-decoration: none;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
body .nav .nav-item img {
width: 60%;
}
body .nav .nav-item span {
text-align: center;
font-weight: bold;
color: #666;
margin-top: 5px;
}
body .recommend,
.newest-course,
.newest-article,
.newest-blog,
.newest-QA {
padding: 10px;
box-sizing: border-box;
}
body .recommend h2,
.newest-course h2,
.newest-article h2,
.newest-blog h2,
.newest-QA h2 {
color: #aaa;
font-size: 18px;
}
body .recommend .course-box,
.newest-course .course-box {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
flex: 1 1 auto;
width: 100%;
}
body .recommend .course-box .course-item:first-child {
margin-right: 10px;
}
body .recommend .course-box .course-item img {
width: 100%;
}
body .course-list {
display: flex;
flex-flow: column nowrap;
justify-content: flex-start;
width: 100%;
/*
padding: 10px; */
margin-top: 15px;
}
body .course-list .course-item1 {
display: flex;
flex-flow: row nowrap;
flex: 1 1 auto;
width: 100%;
text-decoration: none;
background-color: white;
padding: 10px;
box-sizing: border-box;
}
body .course-list .course-item1:not(:first-child) {
margin-top: 20px;
}
body .newest-article .course-list .course-item1:not(:first-child) {
margin-top: 10px;
}
body .newest-article .course-list .course-item1:last-child {
padding: 0;
text-align: center;
}
body .newest-article .course-list .course-item1 .more {
display: block;
margin: 10px auto;
color: #333;
}
body .course-list .course-item1 img {
width: 40%;
}
body .newest-article .course-list .course-item1 img {
width: 30%;
}
body .course-list .course-item1 .course-desc {
width: 60%;
margin-left: 10px;
/* 强制不换行 */
white-space: nowrap;
overflow: hidden;
/* 加省略号 */
text-overflow: ellipsis;
}
body .newest-article .course-list .course-item1 .course-desc {
width: 70%;
margin-right: 10px;
}
body .course-list .course-item1 .course-desc>p:first-of-type {
margin-top: 0;
font-size: 18px;
color: #666;
overflow: hidden;
/* 加省略号 */
text-overflow: ellipsis;
}
body .course-list .course-item1 .course-desc>p:nth-of-type(2) {
color: #999;
overflow: hidden;
text-overflow: ellipsis;
}
body .course-list .course-item1 .course-desc>p:last-of-type span:first-of-type {
background-color: #333;
color: white;
font-size: 10px;
padding: 3px;
border-radius: 10px;
}
body .newest-article .course-list .course-item1 .course-desc>p:last-of-type span:first-of-type {
font-size: 12px;
margin-left: unset;
background-color: unset;
color: #666;
border-radius: unset;
}
body .course-list .course-item1 .course-desc>p:last-of-type {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
}
body .recommend .course-list .course-item1 .course-desc>p:last-of-type {
justify-content: flex-start;
}
body .course-list .course-item1 .course-desc>p:last-of-type span:last-of-type {
font-size: 12px;
margin-left: 5px;
color: #666
}
body .newest-blog .course-list .course-item1,
body .newest-QA .course-list .course-item1 {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
margin-top: 10px;
color: #666;
}
body .newest-blog .course-list .course-item1 h4,
body .newest-QA .course-list .course-item1 h4 {
margin: 0;
white-space: nowrap;
overflow: hidden;
/* 加省略号 */
text-overflow: ellipsis;
margin-right: 10px;
}
body .newest-blog .course-list .course-item1:last-child,
body .newest-QA .course-list .course-item1:last-child {
padding: 0;
text-align: center;
}
body .newest-blog .course-list .course-item1 .more,
body .newest-QA .course-list .course-item1 .more {
display: block;
margin: 10px auto;
color: #333;
}
footer {
display: flex;
flex-flow: row nowrap;
flex: 0 0 auto;
justify-content: space-around;
position: fixed;
bottom: 0;
width: 100%;
background: #ddd;
padding: 10px 0;
border-top: 1px solid #aaa;
}
footer .bottom-btn {
display: flex;
flex-flow: column nowrap;
font-size: 14px;
color: #999
}
footer .bottom-icon {
font-size: 1.5rem;
text-align: center;
}
footer .bottom-btn.active {
color: red;
}
</style>
</head>
<body>
<!-- 头部 -->
<header>
<span class="iconfont head-icon"></span>
<img src="static/images/logo.png" alt="php中文网">
<span class="iconfont head-icon"></span>
</header>
<!-- 轮播 -->
<section class="Carousel">
<img src="static/images/lb1.png" alt="">
</section>
<!-- 导航 -->
<section class="nav">
<a class="nav-item" href="#">
<img src="static/images/html.png" alt="">
<span>HTML/CSS</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/JavaScript.png" alt="">
<span>JavaScript</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/code.png" alt="">
<span>服务端</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/sql.png" alt="">
<span>数据库</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/app.png" alt="">
<span>移动端</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/manual.png" alt="">
<span>手册</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/tool2.png" alt="">
<span>工具</span>
</a>
<a class="nav-item" href="#">
<img src="static/images/live.png" alt="">
<span>直播</span>
</a>
</section>
<!-- 推荐课程 -->
<section class="recommend">
<h2>推荐课程</h2>
<div class="course-box">
<a class="course-item">
<img src="static/images/course-item1.jpg" alt="">
</a>
<a class="course-item">
<img src="static/images/course-item2.jpg" alt="">
</a>
</div>
<div class="course-list">
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
</div>
</section>
<!-- 最新更新 -->
<section class="newest-course">
<h2>最新更新</h2>
<div class="course-list">
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>老的PHPExcel已经停止更新了</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>老的PHPExcel已经停止更新了</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>老的PHPExcel已经停止更新了</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>老的PHPExcel已经停止更新了</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>老的PHPExcel已经停止更新了</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
<a href="#" class="course-item1">
<img src="static/images/course-item1-1.jpg" alt="">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>老的PHPExcel已经停止更新了啊啊啊啊啊啊啊啊啊啊啊啊</p>
<p><span>中级</span><span>61263次播放</span></p>
</div>
</a>
</div>
</section>
<!-- 最新文章 -->
<section class="newest-article">
<!-- 最新文章 -->
<h2>最新文章</h2>
<div class="course-list">
<a href="#" class="course-item1">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>
<!-- <span>中级</span> --><span>发布时间:2020-04-11</span></p>
</div>
<img src="static/images/course-item1-1.jpg" alt="">
</a>
<a href="#" class="course-item1">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>
<!-- <span>中级</span> --><span>发布时间:2020-04-11</span></p>
</div>
<img src="static/images/course-item1-1.jpg" alt="">
</a>
<a href="#" class="course-item1">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>
<!-- <span>中级</span> --><span>发布时间:2020-04-11</span></p>
</div>
<img src="static/images/course-item1-1.jpg" alt="">
</a>
<a href="#" class="course-item1">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>
<!-- <span>中级</span> --><span>发布时间:2020-04-11</span></p>
</div>
<img src="static/images/course-item1-1.jpg" alt="">
</a>
<a href="#" class="course-item1">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>
<!-- <span>中级</span> --><span>发布时间:2020-04-11</span></p>
</div>
<img src="static/images/course-item1-1.jpg" alt="">
</a>
<a href="#" class="course-item1">
<div class="course-desc">
<p>CI框架30分钟极速入门</p>
<p>
<!-- <span>中级</span> --><span>发布时间:2020-04-11</span></p>
</div>
<img src="static/images/course-item1-1.jpg" alt="">
</a>
<a href="#" class="course-item1">
<p class="more">更多内容</p>
</a>
</div>
</section>
<!-- 最新博文 -->
<section class="newest-blog">
<!-- 最新博文 -->
<h2>最新博文</h2>
<div class="course-list">
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<p class="more">更多内容</p>
</a>
</div>
</section>
<!-- 最新问答 -->
<section class="newest-QA">
<!-- 最新问答 -->
<h2>最新问答</h2>
<div class="course-list">
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<h4>不出现tp6.0页面aaaaaaaaaaaaadddddddddddddddddddddddddddffffffffffffffffffffffff</h4>
<span>2020-04-12</span>
</a>
<a href="#" class="course-item1">
<p class="more">更多内容</p>
</a>
</div>
</section>
<!-- 底部 -->
<footer>
<div class="bottom-btn active">
<span class="iconfont bottom-icon"></span>
<span>首页</span>
</div>
<div class="bottom-btn">
<span class="iconfont bottom-icon"></span>
<span>视频</span>
</div>
<div class="bottom-btn">
<span class="iconfont bottom-icon"></span>
<span>社区</span>
</div>
<div class="bottom-btn">
<span class="iconfont bottom-icon"></span>
<span>我的</span>
</div>
</footer>
</body>
</html>