1 项目准备
1.1 项目介绍
学成在线项目学自黑马程序员大圈老师前端视频,项目采用样式与结构分离的思想,目前整个项目只有前端部分,项目整体效果如下图所示:
1.2 项目主要技术
- 版心
- 清除默认样式
- 盒子模型
- 字体声明块、文本声明块、背景声明块
Flex布局
1.3 创建项目目录
网站根目录是指存放网站的第一层文件夹,内部包含当前网站的所有素材,包含 HTML、CSS、图片、JavaScript等等。
-
项目名称:
studyimages文件夹:存放固定使用的图片素材,例如:logo、样式修饰图等等。uploads文件夹:存放非固定使用的图片素材,例如:商品图、宣传图需要上传的图片。css文件夹:存放CSS文件夹(link标签引入样式)base.css文件:基础公共样式,例如:清除默认样式,设置网页基本样式。index.css文件:首页CSS样式。
index.html文件:首页HTML文件
-
首页引入CSS文件
<!-- 顺序要求:先清除再设置 -->
<link rel="stylesheet" href="./css/base.css">
<link rel="stylesheet" href="./css/index.css">
- 设置
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;
}
1.4 页面布局思路
- 第1步 确定页面的版心(可视区)。
- 第2步 布局:先整体再局部,从外到内,从上到下,从左到右。
- 第3步 编写:先注释,再结构,后样式。
- 第4步 CSS实现思路
- 画盒子,调整盒子范围 ➡️ 宽高背景色
- 调整盒子位置 ➡️ flex布局、内外边距
- 控制图片、文字内容样式
2 设置版心居中
.wrapper {
margin: 0 auto;
width: 1200px;
}
body {
background-color: #f3f5f7;
}
3 进行整体布局
通过观察知道,页面头部背景色为白色,banner背景色为蓝色,内容区域背景色为灰色。
由于内容区域占据面积较大,因此可以将整个页面可视区也即body的背景色设置为灰色,再对页面头部以及banner区域分别设置背景色即可。
body {
background-color:#f3f5f7;
}
4 创建头部区域(header)
4.1 整体布局
通过观察设计稿,我们知道头部区域是与浏览器窗口同宽度的盒子,这种盒子被叫做通栏。
- 头部区域的组成成分: 通栏 > 版心 >
logo➕ 导航 ➕ 搜索 ➕ 用户。 - 头部区域的组成成分位置:从左到右水平排布。
我们用HTML结构以及CSS样式表示:
- 头部区域的
HTML结构为:.header>.wrapper>logo➕.nav➕.search➕.user。 - 头部区域里的版心采用
display:flex;布局。
HTML结构
<!-- 头部区域 -->
<div class="header">
<div class="wrapper">
<!-- logo -->
<div class="logo">logo</div>
<!-- 导航 -->
<div class="nav">导航</div>
<!-- 搜索 -->
<div class="search">search</div>
<!-- 用户 -->
<div class="user">用户</div>
</div>
</div>
CSS样式
/* 头部区域 */
.header {
height: 100px;
background-color: #fff;
}
.header .wrapper {
padding-top: 29px;
display: flex;
}
4.2 logo
-
logo 功能:
- 单击跳转到首页(要用到超链接
a标签) - 搜索引擎优化:提升网站百度搜索排名(表示很重要要的标题用
h1标签,得用h1标签嵌套a标签) - 这个链接是通过点击一张图片实现的。(
a标签是行内元素,而img标签是行内块元素,行内元素不能嵌套行内块元素,所以可以给a标签设置背景图片。) - 超链接
a标签与标题h1标签的没有空隙,但是行内元素会产生空隙。(所以,得使用font-size:0;。)
- 单击跳转到首页(要用到超链接
-
实现方法:
- 标签结构:
h1 > a > 网站名称(搜索关键字)
- 标签结构:
HTM结构
<div class="logo">
<h1><a href="#">学成在线</a></h1>
</div>
CSS样式
/* logo */
.logo a {
display: block;
width: 195px;
height: 41px;
background-image: url(../images/logo.png);
font-size: 0;
}
4.3 导航
-
导航功能:
- 单击跳转页面(要用到超链接
a)
- 单击跳转页面(要用到超链接
-
实现方法:
- 标签结构:
ul>li * 3>a - 优势:避免堆砌 a 标签,网站搜索排名降级
- 标签结构:
-
布局思路:
li设置 右侧margina设置 左右padding
HTML结构
<ul>
<li><a href="#" class="active">首页</a></li>
<li><a href="#">课程</a></li>
<li><a href="#">职业规划</a></li>
</ul>
CSS样式
/* 导航 */
.nav {
margin-left: 102px;
}
.nav ul {
display: flex;
}
.nav li {
margin-right: 24px;
}
.nav li a {
display: block;
padding: 6px 8px;
line-height: 27px;
font-size: 19px;
}
/* actvie 类选择器,表示默认选中的a */
.nav li .active,
.nav li a:hover {
border-bottom: 2px solid #00a4ff;
}
4.4 搜索布局
-
搜索功能:
- 能够输入内容(要用
input) - 能够进行点击搜索(要有
a或button)
- 能够输入内容(要用
-
搜索样式:
- 搜索按钮在搜索框的右边
- 对搜索区域要使用
flex布局
-
标签结构:
.search>input➕a/buuton
HTML结构
<div class="search"></div>
CSS样式
.search {
display: flex;
margin-left: 64px;
padding-left: 19px;
padding-right: 12px;
width: 412px;
height: 40px;
background-color: #f3f5f7;
border-radius: 20px;
}
4.5 搜索内容
因为在input输入框中,输入文本,会出现焦点框,因此得使用outline:none;去掉焦点框。
要改变input输入框中的提示信息文字与颜色,得使用input::placeholder选择器。
HTML结构
<div class="search">
<input type="text" placeholder="请输入关键词">
<a href="#"></a>
</div>
CSS样式
.search input {
flex: 1;
border: 0;
background-color: transparent;
/* 去掉表单控件的焦点框 */
outline: none;
}
/* ::placeholder 选中就是 placeholder 属性文字样式*/
.search input::placeholder {
font-size: 14px;
color: #999;
}
/* 父级是flex布局,子级变弹性盒子:加宽高生效 */
.search a {
align-self: center;
width: 16px;
height: 16px;
background-image: url(../images/search.png);
}
4.6 用户区域
- 用户功能:
- 要有用户头像(要用
image)以及用户昵称(要用span)。 - 点击用户头像或者用户昵称将跳转至首页(要用
a)
- 要有用户头像(要用
- 标签结构:
.user>a>img➕span
HTML结构
<div class="user">
<a href="#">
<img src="./uploads/user.png" alt="">
<span>播仔学前端</span>
</a>
</div>
CSS样式
/* 用户 */
.user {
margin-left: 32px;
margin-top: 4px;
}
.user img {
margin-right: 7px;
/* vertical-align 行内块和行内垂直方向对齐方式 */
vertical-align: middle;
}
.user span {
font-size: 16px;
color: #666;
}
5 创建banner区域
5.1 布局
- 标签结构:通栏
.banner> 版心.wrapper>.left➕.right
HTML结构
<div class="banner">
<div class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
</div>
</div>
CSS样式
/* banner 区域 */
.banner {
height: 420px;
background-color: #0092cb;
}
.banner .wrapper {
display: flex;
justify-content: space-between;
height: 420px;
background-image: url(../uploads/banner.png);
}
5.2 侧导航
- 侧导航功能:
- 整齐有序的列表(要用
ul>li) - 每一项都是链接(要用
a)
- 整齐有序的列表(要用
- 侧导航标签结构:
.left>ul>li*9>a
- 侧导航布局思路:
a默认状态:背景图为白色右箭头(重要的图片用img,起装饰作用图标的用background)a悬停状态:背景图为蓝色右箭头
HTML结构
<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>
CSS样式
/* 侧导航 */
.banner .left {
padding: 3px 20px;
width: 191px;
height: 420px;
background-color: rgba(0,0,0,0.42);
}
.banner .left a {
/* 块级:宽度是父级的100% */
display: block;
height: 46px;
background: url(../images/right.png) no-repeat right center;
line-height: 46px;
font-size: 16px;
color: #fff;
}
.banner .left a:hover {
background-image: url(../images/right-hover.png);
color: #00a4ff;
}
5.3 课程表布局
- 我的课程表 标签结构:
.right>h3➕.content
HTML布局
<div class="right">
<h3>我的课程表</h3>
<div class="content">1</div>
</div>
CSS样式
/* 课程表 */
.banner .right {
margin-top: 60px;
width: 218px;
height: 305px;
background-color: #209dd5;
border-radius: 10px;
}
.banner .right h3 {
margin-left: 14px;
height: 48px;
line-height: 48px;
font-size: 15px;
color: #fff;
font-weight: 400;
}
.banner .right .content {
padding: 14px;
height: 257px;
background-color: #fff;
border-radius: 10px;
}
5.4 课程表内容
- 我的课程表内容功能:
- 每一项都有一个标题一个解释,所以每项用
dl列表。 - 每一项中的解释部分中的“正在学习”用
span - 每一项中的解释部分中的另外一部分表强调用
strong
- 每一项都有一个标题一个解释,所以每项用
HTML结构
<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>
CSS样式
.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;
}
5.5 全部课程
- 全部课程:
- 该部分为一个链接用
a
- 该部分为一个链接用
HTML结构
<a href="#">全部课程</a>
CSS样式
.banner .right a {
display: block;
height: 32px;
background-color: #00a4ff;
text-align: center;
line-height: 32px;
font-size: 14px;
color: #fff;
border-radius: 15px;
}
6 创建精品推荐区域(recommend)
6.1 区域布局
- 实现方法:
- 组成成分:标题➕技术区域➕链接
- 标签结构:
.recommend>h3➕ul➕a.modify - 外观表现:水平排布
- CSS样式:使用
flex布局
HTML结构
<!-- 精品推荐 -->
<div class="recommend wrapper">
<h3>精品推荐</h3>
<ul>
<li><a href="#">HTML</a></li>
</ul>
<a href="#" class="modify">修改兴趣</a>
</div
CSS样式
/* 精品推荐 */
.recommend {
display: flex;
margin-top: 11px;
padding: 0 20px;
height: 60px;
background-color: #fff;
box-shadow: 0px 1px 2px 0px rgba(211, 211, 211, 0.5);
line-height: 60px;
}
.recommend ul {
/* 除去标题和修改兴趣的尺寸,父级剩余尺寸都给ul,实现把修改兴趣挤到最右侧 */
flex: 1;
}
6.2 内容样式
如何让技术区域占据盒子空间?得使用flex:1;。
HTML结构
<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>
CSS样式
.recommend h3 {
font-size: 18px;
color: #00a4ff;
font-weight: 400;
}
.recommend ul {
/* 除去标题和修改兴趣的尺寸,父级剩余尺寸都给ul,实现把修改兴趣挤到最右侧 */
flex: 1;
display: flex;
}
.recommend ul li a {
padding: 0 24px;
border-right: 1px solid #e0e0e0;
font-size: 18px;
}
.recommend ul li:last-child a {
border-right: 0;
}
.recommend .modify {
font-size: 16px;
color: #00a4ff;
}
7 创建推荐课程区域(course)
7.1 标题区域
- 实现方法:
- 标签结构:
.course>.hd+.bd - 布局思路:盒子模型
- 为了避免默认行间距影响内外间距,可以先设置好行高,再设置内外间距。
- 设计稿中的标题样式均相似,因此可以只做一个
.hd
- 标签结构:
HTML结构
<!-- 精品推荐课程 -->
<div class="course wrapper">
<!-- 标题 -->
<div class="hd">
<h3>精品推荐</h3>
<a href="#" class="more">查看全部</a>
</div>
<!-- 内容 -->
<div class="bd">1</div>
</div>
CSS样式
/* 推荐课程 */
.course {
margin-top: 15px;
}
/* 标题 - 公共类,与其他区域共用 */
.hd {
display: flex;
justify-content: space-between;
height: 60px;
line-height: 60px;
}
.hd h3 {
font-size: 21px;
font-weight: 400;
}
.hd .more {
padding-right: 20px;
background: url(../images/more.png) no-repeat right center;
font-size: 14px;
color: #999;
}
7.2 内容布局
- 实现方法:
- 设计稿中的卡边内容样式均相似,因此可以只做一个
.bd。 - 水平的间距相似,可以使用
justify-content: space-between;实现两端对齐。
- 设计稿中的卡边内容样式均相似,因此可以只做一个
HTML结构
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
CSS样式
/* 课程内容 - 公共类 */
.bd ul {
/* 让父级把剩余尺寸都给ul,才能让主轴对齐有剩余空间 */
flex:1;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.bd li {
margin-bottom: 14px;
width: 228px;
height: 271px;
background-color: pink;
}
7.3 内容样式
- 实现方法:
- 每一张卡片都能点击,要用
a。 - 每一张卡片包含三个部分,从上到下分别为图片、文本内容(标题文字以及学习情况)。
- 标签结构:
a>.pic(>img)➕.text(>h4+p)。 - 学习情况中的数字要用
i标签包裹。
- 每一张卡片都能点击,要用
HTML结构
<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>
CSS样式
.bd li .pic {
height: 156px;
}
.bd li .text {
padding: 20px;
height: 115px;
background-color: #fff;
}
.bd li .text h4 {
margin-bottom: 13px;
height: 40px;
font-size: 14px;
line-height: 20px;
font-weight: 400;
}
.bd li .text p {
font-size: 14px;
line-height: 20px;
color: #999;
}
.bd li .text p span {
color: #fa6400;
}
.bd li .text p i {
font-style: normal;
}
8 前端开发工程师区域
HTML结构
<!-- 前端 -->
<div class="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>
CSS样式
/* 前端 */
.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 {
width: 228px;
/* background-color: pink; */
}
.bd .right {
width: 957px;
/* background-color: pink; */
}
.bd .right .top {
margin-bottom: 15px;
height: 100px;
}
9 版权(footer)
9.1 布局
- 实现方法:
- 标签结构:通栏
.footer> 版心.wrapper>.left➕.right
- 标签结构:通栏
HTML结构
<!-- 版权 -->
<div class="footer">
<div class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
</div>
</div>
CSS样式
/* 版权 */
.footer {
margin-top: 60px;
padding-top: 60px;
height: 273px;
background-color: #fff;
}
.footer .wrapper {
display: flex;
justify-content: space-between;
}
.footer .left {
width: 440px;
background-color: pink;
}
9.2 内容
- 实现方法:
- “学成在线”图标要能点击得放在链接
a里面。 - “下载APP”能点击,要用链接
a包裹。 - 右侧解释要用
dl列表。
- “学成在线”图标要能点击得放在链接
HTML结构
<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>
CSS样式
.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;
border: 1px solid #00a4ff;
text-align: center;
line-height: 34px;
font-size: 16px;
color: #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;
}