目标:运用前6天学习的知识,完成静态网页的布局。
一、任务目标
1、工具和素材
工具:Mac版PxCook像素大厨,Windows版PxCook像素大厨,需要的请自行下载安装。
素材:PSD素材和图片素材
PxCook 像素大厨是用来查看 PSD 格式的设计稿的,没用过的可以阅读 Day05的课程,里面有讲解。
完整项目代码的可以点击这里获取完整代码,仅供学习参考使用。
2、目标网页
要完成布局的网页样式如下图:
二、实战开发要点
1、项目目录
这里将网站项目命名为 study,网站根目录是指存放网站的第一层文件夹,内部包含当前网站的所有素材,包含 HTML、CSS、图片、JavaScript等等。 项目目录结构如下:
2、布局思路
- 布局思路:先整体再局部,从外到内,从上到下,从左到右
- CSS 实现思路
- 画盒子,调整盒子范围 → 宽高背景色
- 调整盒子位置 → flex 布局、内外边距
- 控制图片、文字内容样式
3、网页 logo 优化
- 单击跳转到首页
- 搜索引擎优化:提升网站百度搜索排名
- 标签结构:h1 > a > 网站名称(搜索关键字)
4、导航优化
- 标签结构:ul > li * 3 > a
- 优势:避免堆砌 a 标签,网站搜索排名降级
三、代码
1、index.html 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>学成在线</title>
<!-- 顺序要求:先清除再设置 -->
<link rel="stylesheet" href="./css/base.css">
<link rel="stylesheet" href="./css/index.css">
</head>
<body>
<!-- 头部区域 -->
<div class="header">
<div class="wrapper">
<!-- logo -->
<div class="logo">
<h1><a href="#">学成在线</a></h1>
</div>
<!-- 导航 -->
<div class="nav">
<ul>
<li><a href="#" class="active">首页</a></li>
<li><a href="#">课程</a></li>
<li><a href="#">职业规划</a></li>
</ul>
</div>
<!-- 搜索 -->
<div class="search">
<input type="text" placeholder="请输入关键词">
<a href="#"></a>
</div>
<!-- 用户 -->
<div class="user">
<a href="#">
<img src="./uploads/user.png" alt="">
<span>播仔学前端</span>
</a>
</div>
</div>
</div>
<!-- banner 区域 -->
<div class="banner">
<div class="wrapper">
<div class="left">
<ul>
<li><a href="#">前端开发</a></li>
<li><a href="#">后端开发</a></li>
<li><a href="#">移动开发</a></li>
<li><a href="#">人工智能</a></li>
<li><a href="#">商业预测</a></li>
<li><a href="#">云计算&大数据</a></li>
<li><a href="#">运维&测试</a></li>
<li><a href="#">UI设计</a></li>
<li><a href="#">产品</a></li>
</ul>
</div>
<div class="right">
<h3>我的课程表</h3>
<div class="content">
<dl>
<dt>数据可视化课程</dt>
<dd><span>正在学习</span>-<strong>echarts使用步骤</strong></dd>
</dl>
<dl>
<dt>Vue3医疗项目课程 </dt>
<dd><span>正在学习</span>-<strong>认识组合式API</strong></dd>
</dl>
<dl>
<dt>React核心技术课程</dt>
<dd><span>正在学习</span>-<strong>rudex配合TS使用</strong></dd>
</dl>
<a href="#">全部课程</a>
</div>
</div>
</div>
</div>
<!-- 精品推荐 -->
<!-- .recomend>h3{精品推荐}+(ul>(li>a{HTML})*9)+a.modify -->
<div class="recommend wrapper">
<h3>精品推荐</h3>
<ul>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
<li><a href="#">Node.js</a></li>
<li><a href="#">Ajax</a></li>
<li><a href="#">Vue2.0</a></li>
<li><a href="#">Vue3.0</a></li>
<li><a href="#">TypeScript</a></li>
<li><a href="#">React</a></li>
</ul>
<a href="#" class="modify">修改兴趣</a>
</div>
<!-- 精品推荐课程 -->
<div class="course wrapper">
<!-- 标题 -->
<div class="hd">
<h3>精品推荐</h3>
<a href="#" class="more">查看全部</a>
</div>
<!-- 内容 -->
<div class="bd">
<ul>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course01.png" alt=""></div>
<div class="text">
<h4>JavaScript数据看板项目实战</h4>
<p><span>高级</span> · <i>1125</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course02.png" alt=""></div>
<div class="text">
<h4>Vue.js实战——面经全端项目</h4>
<p><span>高级</span> · <i>2726</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course03.png" alt=""></div>
<div class="text">
<h4>玩转Vue全家桶,iHRM人力资源项目</h4>
<p><span>高级</span> · <i>9456</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course04.png" alt=""></div>
<div class="text">
<h4>Vue.js实战医疗项目——优医问诊</h4>
<p><span>高级</span> · <i>7192</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course05.png" alt=""></div>
<div class="text">
<h4>小程序实战:小兔鲜电商小程序项目</h4>
<p><span>高级</span> · <i>2703</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course06.png" alt=""></div>
<div class="text">
<h4>前端框架Flutter开发实战</h4>
<p><span>高级</span> · <i>2841</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course07.png" alt=""></div>
<div class="text">
<h4>熟练使用React.js——极客园H5项目</h4>
<p><span>高级</span> · <i>95682</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course08.png" alt=""></div>
<div class="text">
<h4>熟练使用React.js——极客园PC端项目</h4>
<p><span>高级</span> · <i>904</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course09.png" alt=""></div>
<div class="text">
<h4>前端实用技术,Fetch API 实战</h4>
<p><span>高级</span> · <i>1516</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/course10.png" alt=""></div>
<div class="text">
<h4>前端高级Node.js零基础入门教程</h4>
<p><span>高级</span> · <i>2766</i>人在学习</p>
</div>
</a>
</li>
</ul>
</div>
</div>
<!-- 前端 -->
<div class="course wrapper">
<!-- 标题 -->
<div class="hd">
<h3>前端开发工程师</h3>
<ul>
<li><a href="#" class="active">热门</a></li>
<li><a href="#">初级</a></li>
<li><a href="#">中级</a></li>
<li><a href="#">高级</a></li>
</ul>
<a href="#" class="more">查看全部</a>
</div>
<div class="bd">
<div class="left">
<img src="./uploads/web_left.png" alt="">
</div>
<div class="right">
<div class="top"><img src="./uploads/web_top.png" alt=""></div>
<div class="bottom">
<ul>
<li>
<a href="#">
<div class="pic"><img src="./uploads/web01.png" alt=""></div>
<div class="text">
<h4>JS高级javaScript进阶面向对象ES6</h4>
<p><span>高级</span> · <i>101937</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/web02.png" alt=""></div>
<div class="text">
<h4>零基础玩转微信小程序</h4>
<p><span>高级</span> · <i>133781</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/web03.png" alt=""></div>
<div class="text">
<h4>JavaScript基础——语法解析+项目实战</h4>
<p><span>高级</span> · <i>8927</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/web04.png" alt=""></div>
<div class="text">
<h4>前端框架Vue2+Vue3全套视频</h4>
<p><span>高级</span> · <i>26022</i>人在学习</p>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Python -->
<div class="course wrapper">
<!-- 标题 -->
<div class="hd">
<h3>Python+大数据开发</h3>
<ul>
<li><a href="#" class="active">热门</a></li>
<li><a href="#">初级</a></li>
<li><a href="#">中级</a></li>
<li><a href="#">高级</a></li>
</ul>
<a href="#" class="more">查看全部</a>
</div>
<div class="bd">
<div class="left">
<img src="./uploads/python_left.png" alt="">
</div>
<div class="right">
<div class="top"><img src="./uploads/python_top.png" alt=""></div>
<div class="bottom">
<ul>
<li>
<a href="#">
<div class="pic"><img src="./uploads/python01.png" alt=""></div>
<div class="text">
<h4>Django视频教程_Django入门视频教程</h4>
<p><span>高级</span> · <i>9037</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/python02.png" alt=""></div>
<div class="text">
<h4>python实战项目从0开发一个Django博客系统</h4>
<p><span>高级</span> · <i>88320</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/python03.png" alt=""></div>
<div class="text">
<h4>Python+大数据进阶教程6天掌握NoSQL实时计算基础</h4>
<p><span>高级</span> · <i>8863</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/python04.png" alt=""></div>
<div class="text">
<h4>数据分析入门教程|300分钟用Matplotlib打造疫情展示地图</h4>
<p><span>高级</span> · <i>54093</i>人在学习</p>
</div>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- 人工智能开发 -->
<div class="course wrapper">
<!-- 标题 -->
<div class="hd">
<h3>人工智能开发</h3>
<ul>
<li><a href="#" class="active">热门</a></li>
<li><a href="#">初级</a></li>
<li><a href="#">中级</a></li>
<li><a href="#">高级</a></li>
</ul>
<a href="#" class="more">查看全部</a>
</div>
<!-- 内容 -->
<div class="bd">
<ul>
<li>
<a href="#">
<div class="pic"><img src="./uploads/ai01.png" alt=""></div>
<div class="text">
<h4>4天快速入门Python数据挖掘</h4>
<p><span>高级</span> · <i>83556</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/ai02.png" alt=""></div>
<div class="text">
<h4>计算机视觉入门及案例实战</h4>
<p><span>高级</span> · <i>422234</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/ai03.png" alt=""></div>
<div class="text">
<h4>AI深度学习自然语言处理NLP零基础入门</h4>
<p><span>高级</span> · <i>33846</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/ai05.png" alt=""></div>
<div class="text">
<h4>Python进阶课程-Web基础开发</h4>
<p><span>高级</span> · <i>61644</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/ai06.png" alt=""></div>
<div class="text">
<h4>AI-OpenCV图像处理10小时零基础入门</h4>
<p><span>高级</span> · <i>34922</i>人在学习</p>
</div>
</a>
</li>
</ul>
</div>
</div>
<!-- JavaEE -->
<div class="course wrapper">
<!-- 标题 -->
<div class="hd">
<h3>JavaEE</h3>
<ul>
<li><a href="#" class="active">热门</a></li>
<li><a href="#">初级</a></li>
<li><a href="#">中级</a></li>
<li><a href="#">高级</a></li>
</ul>
<a href="#" class="more">查看全部</a>
</div>
<!-- 内容 -->
<div class="bd">
<ul>
<li>
<a href="#">
<div class="pic"><img src="./uploads/java01.png" alt=""></div>
<div class="text">
<h4>Mybatis注解开发及注解配置</h4>
<p><span>高级</span> · <i>27925</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/java02.png" alt=""></div>
<div class="text">
<h4>详细分析LinkedList数据链表的实现原理</h4>
<p><span>高级</span> · <i>25885</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/java03.png" alt=""></div>
<div class="text">
<h4>全面深入Mysql数据库优化_java进阶教程</h4>
<p><span>高级</span> · <i>94577</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/java04.png" alt=""></div>
<div class="text">
<h4>全面解剖RocketMQ和项目实战_Java进阶教程</h4>
<p><span>高级</span> · <i>47554</i>人在学习</p>
</div>
</a>
</li>
<li>
<a href="#">
<div class="pic"><img src="./uploads/java05.png" alt=""></div>
<div class="text">
<h4>三天系统学习ZooKeeper</h4>
<p><span>高级</span> · <i>110510</i>人在学习</p>
</div>
</a>
</li>
</ul>
</div>
</div>
<!-- 版权 -->
<div class="footer">
<div class="wrapper">
<div class="left">
<a href="#"><img src="./images/logo.png" alt=""></a>
<p>学成在线致力于普及中国最好的教育它与中国一流大学和机构合作提供在线课程。
© 2017年XTCG Inc.保留所有权利。-沪ICP备15025210号</p>
<a href="#" class="download">下载APP</a>
</div>
<div class="right">
<dl>
<dt>关于学成网</dt>
<dd><a href="#">关于</a></dd>
<dd><a href="#">管理团队</a></dd>
<dd><a href="#">工作机会</a></dd>
<dd><a href="#">客户服务</a></dd>
<dd><a href="#">帮助</a></dd>
</dl>
<dl>
<dt>新手指南</dt>
<dd><a href="#">如何注册</a></dd>
<dd><a href="#">如何选课</a></dd>
<dd><a href="#">如何拿到毕业证</a></dd>
<dd><a href="#">学分是什么</a></dd>
<dd><a href="#">考试未通过怎么办</a></dd>
</dl>
<dl>
<dt>合作伙伴</dt>
<dd><a href="#">合作机构</a></dd>
<dd><a href="#">合作导师</a></dd>
</dl>
</div>
</div>
</div>
</body>
</html>
2、base.css 代码
/* 基础公共样式:清除默认样式 + 设置通用样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
li {
list-style: none;
}
body {
font: 14px/1.5 "Microsoft Yahei", "Hiragino Sans GB", "Heiti SC", "WenQuanYi Micro Hei", sans-serif;
color: #333;
}
a {
color: #333;
text-decoration: none;
}
3、index.css 代码
/* 首页样式 */
/* 版心 */
.wrapper {
margin: 0 auto;
width: 1200px;
}
body {
background-color: #f3f5f7;
}
/* 头部区域 */
.header {
background-color: #fff;
height: 100px;
}
/* 设置 flex 布局, 侧轴方向居中显示 */
.header .wrapper {
height: 100%;
display: flex;
align-items: center;
}
/* logo */
.logo a {
background-image: url(../images/logo.png);
display: block;
width: 195px;
height: 41px;
font-size: 0;
}
/* 导航 */
.nav {
margin-left: 102px;
}
.nav ul {
display: flex;
}
.nav li {
margin-right: 24px;
}
/* 这里的 a 爷爷级(父级的父级)才设置了 flex 布局, 所以 a 并不是弹性盒子, 设置宽高 padding margin不生效*/
/* 需要将 a 转成块级元素, 设置 padding 才生效 */
.nav li a {
/* 这里的 a */
display: block;
padding: 6px 8px;
font-size: 19px;
}
/* 并集选择器, active类选择器 和 鼠标悬停 添加下划线 */
.nav li .active,
.nav li a:hover {
border-bottom: 2px solid #00a4ff;
}
/* 搜索 */
.search {
background-color: #f3F5F7;
margin-left: 64px;
padding: 0 19px 0 12px;
display: flex;
align-items: center;
width: 412px;
height: 40px;
border-radius: 20px;
}
.search input {
background-color: transparent;
flex: 1;
/* 父级设置了 align-items: center 这里要设置高度, 否则高度由内容撑开, 只有文字高度 */
height: 40px;
border: 0;
/* 去掉表单控件的焦点框 */
outline: none;
}
/* ::placeholder 选中就是 placeholder 属性文字样式 */
.search input::placeholder {
font-size: 14px;
color: #999;
}
/* 父级是 flex 布局,子级变弹性盒子:加宽高生效, 所以不需要写 display: block */
.search a {
background-image: url(../images/search.png);
width: 16px;
height: 16px;
}
/* 用户 */
.user {
margin-left: 32px;
}
.user img {
margin-right: 7px;
/* vertical-align 行内块和行内垂直方向对齐方式 */
vertical-align: middle;
}
.user span {
vertical-align: middle;
font-size: 16px;
color: #666;
}
/* banner 区域 */
.banner {
background-color: #0092ce;
height: 420px;
}
.banner .wrapper {
background-image: url(../uploads/banner.png);
display: flex;
justify-content: space-between;
height: 420px;
}
/* 侧导航 */
.banner .left {
background-color: rgba(0, 0, 0, 0.42);
padding: 3px 20px;
width: 191px;
height: 420px;
}
.banner .left a {
background: url(../images/right.png) no-repeat right center;
display: block;
height: 46px;
line-height: 46px;
color: #FFF;
}
.banner .left a:hover {
background-image: url(../images/right-hover.png);
color: #00a4ff;
}
/* 课程表 */
.banner .right {
background-color: #209dd5;
width: 218px;
height: 305px;
border-radius: 10px;
align-self: center;
}
.banner .right h3 {
margin-left: 14px;
height: 48px;
line-height: 48px;
font-size: 15px;
font-weight: 400;
color: #fff;
}
.banner .right .content {
background-color: #fff;
padding: 14px;
height: 257px;
border-radius: 10px;
}
.banner .right dl {
margin-bottom: 12px;
border-bottom: 1px solid #e0e0e0;
}
.banner .right dt {
margin-bottom: 8px;
font-size: 14px;
line-height: 20px;
font-weight: 700;
}
.banner .right dd {
margin-bottom: 8px;
font-size: 12px;
line-height: 16px;
}
.banner .right dd span {
color: #00a4ff;
}
.banner .right dd strong {
color: #7d7d7d;
font-weight: 400;
}
.banner .right a {
background-color: #00a4ff;
display: block;
height: 32px;
text-align: center;
line-height: 32px;
font-size: 14px;
color: #fff;
border-radius: 15px;
}
/* 精品推荐 */
.recommend {
background-color: #fff;
display: flex;
margin-top: 11px;
padding: 0 20px;
height: 60px;
line-height: 60px;
box-shadow: 0px 1px 2px 0px rgba(211, 211, 211, 0.5);
}
.recommend h3 {
font-size: 18px;
font-weight: 400;
color: #00a4ff;
}
.recommend ul {
/* 除去标题和修改兴趣的尺寸,父级剩余尺寸都给 ul,实现把修改兴趣挤到最右侧 */
flex: 1;
display: flex;
}
.recommend ul li a {
padding: 0 24px;
border-right: 1px solid #e0e0e0;
font-size: 17px;
}
.recommend ul li:last-child a {
border-right: 0;
}
.recommend .modify {
font-size: 16px;
color: #00a4ff;
}
/* 推荐课程 */
.course {
margin-top: 15px;
}
/* 标题 - 公共类,与其他区域共用 */
.hd {
display: flex;
justify-content: space-between;
/* 此处设置侧轴居中, 如果子级弹性盒子不设置高度, 子级高度由内容撑开 */
align-items: center;
height: 60px;
}
.hd h3 {
font-size: 21px;
font-weight: 400;
}
.hd .more {
background: url(../images/more.png) no-repeat right center;
padding-right: 20px;
font-size: 14px;
color: #999;
}
/* 课程内容 - 公共类 */
.bd ul {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
}
.bd li {
background-color: pink;
margin-bottom: 14px;
width: 228px;
height: 271px;
}
.bd li .pic {
height: 156px;
}
/* 父级设置了宽度 228px, 子级不用再设置宽度了, 会继承父级的宽度 */
.bd li .text {
background-color: #fff;
padding: 20px;
height: 115px;
}
.bd li .text h4 {
margin-bottom: 13px;
height: 40px;
line-height: 20px;
font-size: 14px;
font-weight: 400;
}
.bd li .text p {
line-height: 20px;
font-size: 14px;
color: #999;
}
.bd li .text p span {
color: #fa6400;
}
/* 学习人数标签, 真实开发中数据从后台请求, 这里单独加一个标签, 后期可以通过 js 做替换 */
.bd li .text p i {
font-style: normal;
}
/* 前端 */
.hd ul {
display: flex;
}
.hd li {
margin-right: 60px;
font-size: 16px;
}
.hd li .active {
color: #00a4ff;
}
/* 没有设置高度, 高度由子级内容撑开 */
.bd {
display: flex;
justify-content: space-between;
}
.bd .left {
/* background-color: pink; */
width: 228px;
}
.bd .right {
/* background-color: pink; */
width: 957px;
}
.bd .right .top {
margin-bottom: 15px;
height: 100px;
}
/* 版权 */
.footer {
background-color: #fff;
margin-top: 60px;
padding-top: 60px;
height: 273px;
}
.footer .wrapper {
display: flex;
justify-content: space-between;
}
.footer .left {
/* background-color: pink; */
width: 440px;
}
.footer .left p {
margin-top: 24px;
margin-bottom: 14px;
font-size: 12px;
line-height: 17px;
color: #666;
}
.footer .left .download {
display: block;
width: 120px;
height: 36px;
line-height: 34px;
font-size: 16px;
text-align: center;
color: #00a4ff;
border: 1px solid #00a4ff;
}
.footer .right {
display: flex;
}
.footer .right dl {
margin-left: 130px;
}
.footer .right dt {
margin-bottom: 12px;
font-size: 16px;
line-height: 23px;
}
.footer .right a {
font-size: 14px;
color: #666;
line-height: 24px;
}
.footer .right a:hover {
color: #00a4ff;
}
再好的讲解也不如自己亲自动手敲一遍,想要完整项目代码的可以点击这里获取完整代码,仅供学习参考使用。