HTML
网页的构成
- HTML:通常用来定义网页内容的含义和基本结构。
- CSS:通常用来描述网页的表现与展示效果。
- JavaScript:通常用来执行网页的功能与行为。
一、概述
HTML(超文本标记语言——HyperText Markup Language)是构成 Web 世界的一砖一瓦。它是一种用来告知浏览器如何组织页面的标记语言。
所谓超文本Hypertext
,是指连接单个或者多个网站间的网页的链接。我们通过链接,就能访问互联网中的内容。
所谓标记Markup
,是用来注明文本,图片等内容,以便于在浏览器中显示,例如<head>
,<body>
等。
二、组成
HTML页面由一系列的元素(elements) 组成,而元素是使用标签创建的。
1)标签
一对标签( tags)可以设置一段文字样式,添加一张图片或者添加超链接等等。
2)属性
HTML标签可以拥有属性。属性提供了有关 HTML 元素的更多的信息。我们只能在开始标签中,加入属性。通常以名称=值
成对的形式出现,比如:name=‘value’。
页面说明
<!DOCTYPE html>
: 声明文档类型。<html>
:这个标签包裹了整个完整的页面,是一个根元素(顶级元素)。<head>
:这个标签是一个容器
,它包含了所有你想包含在HTML页面中但不想在HTML页面中显示的内容<body>
:包含了文档内容
,你访问页面时所有显示在页面上的文本,图片,音频,游戏等等。
三、基本语法
注释
将其用特殊的记号<!--注释-->包括起来
标签
空元素
一些元素只有一个标签,叫做空元素
例如:
<br/>
嵌套元素
<p>这是<b>第一个</b>页面</p>
块级和行内
概念
-
块级元素:
独占一行。块级元素(block)在页面中
以块的形式展现
。相对于其前面的内容它会出现在新的一行,其后的内容也会被挤到下一行展现。比如<p>
,<hr>
,<li>
,<div>
等。 -
行内元素
行内显示。行内元素
不会导致换行
。通常出现在块级元素中并环绕文档内容的一小部分,而不是一整个段落或者一组内容。比如<b>
,<a>
,<i>
,<span>
等。注意:一个块级元素不会被嵌套进内联元素中,但可以嵌套在其它块级元素中。
div和span
-
<div>
是一个通用的内容容器,并没有任何特殊语义。它可以被用来对其它元素进行分组,一般用于样式化相关的需求。它是一个块级元素。 -
<span>
是短语内容的通用行内容器,并没有任何特殊语义。它可以被用来编组元素以达到某种样式。它是一个行内元素。
注意:div和span在页面布局中有重要作用。
属性
-
属性名:同一个标签中,属性名不得重复。
-
大小写:属性和属性值对大小写不敏感。不过W3C标准中,推荐使用小写的属性/属性值。
-
引号:双引号是最常用的,不过使用单引号也没有问题。
-
常用属性:
属性名 作用 class 定义元素类名,用来选择和访问特定的元素 id 定义元素唯一标识符,在整个文档中必须是唯一的 name 定义元素名称,可以用于提交服务器的表单字段 value 定义在元素内显示的默认值 style 定义CSS样式,这些样式会覆盖之前设置的样式
特殊字符
原义字符 | 等价字符引用 |
---|---|
< | < |
> | > |
" | " |
’ | ' |
& | & |
空格 | |
文本标签
标签名 | 作用 |
---|---|
p | 表示文本的一个段落 |
h | 表示文档标题,<h1>–<h6> ,呈现了六个不同的级别的标题,<h1> 级别最高,而 <h6> 级别最低 |
hr | 表示段落级元素之间的主题转换,一般显示为水平线 |
li | 表示列表里的条目。 |
ul | 表示一个无序列表,可含多个元素,无编号显示。 |
ol | 表示一个有序列表,通常渲染为有带编号的列表 |
em | 表示文本着重,一般用斜体显示 |
strong | 表示文本重要,一般用粗体显示 |
font | 表示字体,可以设置样式(已过时) |
i | 表示斜体 |
b | 表示加粗文本 |
div的class值
<style>
div{ border: 1px solid blue;}
</style>
<div >left</div>
<div >center</div>
<div>right</div>
浮动布局和清除
<!-- 加入浮动 -->
float:none;不浮动
float:left;左浮动
float:right;右浮动
<!-- 清除浮动 -->
clear:both;清除两侧浮动,此元素不再收浮动元素布局影响。
设置背景
背景色:
background-color: black;
背景图:
background-image: url("../img/bg.png");
图片标签
标签名 | 作用 | 备注 |
---|---|---|
img | 可以显示一张图片(本地或网络) | src属性,这是一个必需的属性,表示图片的地址。 |
其他属性:
属性名 | 作用 | 备注 |
---|---|---|
title | 鼠标悬停(hover)时显示文本。 | |
alt | 图形不显示时的替换文本。 | |
height | 图像的高度。 | |
width | 图像的宽度。 |
超链接
标签名 | 作用 | 备注 |
---|---|---|
a | 表示超链接。 | href属性,表示超链接指向的URL地址。 |
属性名 | 作用 |
---|---|
target | 页面的打开方式(_self当前页 _blank新标签页)。 |
去掉下划线
根据某些样式的布局需求,去除下划线更为美观。
a {
text-decoration:none; // none 表示不显示
}
表单标签
标签名 | 作用 | 备注 |
---|---|---|
form | 表示表单,是用来收集用户输入信息并向 Web 服务器提交的一个容器 |
<form >
//表单元素
</form>
表单的属性
属性名 | 作用 | 备注 |
---|---|---|
action | 处理此表单信息的Web服务器的URL地址 | |
method | 提交此表单信息到Web服务器的方式 | 可能的值有get和post,默认为get |
autocomplete | 自动补全,指示表单元素是否能够拥有一个默认值,配合input标签使用 | HTML5 |
GET与POST说明:
post
:指的是 HTTP POST 方法;表单数据会包含在表单体内然后发送给服务器。
get
:指的是 HTTP GET 方法;表单数据会附加在 action
属性的URI中,并以 ‘?’ 作为分隔符,然后这样得到的 URI 再发送给服务器。
GET方式举例:
GET与POST对比:
地址栏可见 | 数据安全 | 数据大小 | |
---|---|---|---|
GET | 可见 | 不安全 | 有限制(取决于浏览器) |
POST | 不可见 | 相对安全 | 无限制 |
表单元素入门
表单元素指的是 input 元素、复选框、下拉框、提交按钮等等。
标签名 | 作用 | 备注 |
---|---|---|
label | 表单元素的说明,配合表单元素使用 | for属性值为相关表单元素的id属性值 |
input | 表单中输入控件,多种输入类型,用于接受来自用户数据 | type属性值决定输入类型 |
button | 页面中可点击的按钮,可以配合表单进行提交 | type属性值决定按钮类型 |
简单的文本输入框
-
label标签:表单的说明。
- for属性值:匹配input标签的id属性值
-
input标签:输入控件。
- type属性:表示输入类型,text值为普通文本框
- id属性:表示标签唯一标识
- name属性:表示标签名称
- value属性:表示标签数据值
提交用户名的表单
-
button标签:表示按钮。
- type属性:表示按钮类型,submit值为提交按钮。
关于属性值
NAME和VALUE属性
属性名 | 作用 |
---|---|
name | <input> 的名字,在提交整个表单数据时,可以用于区分属于不同<input> 的值 |
value | 这个<input> 元素当前的值,允许用户通过页面输入 |
TYPE属性
- 基本的文本属性
属性值 | 作用 | 备注 |
---|---|---|
text | 单行文本字段 | |
password | 单行文本字段,值被遮盖 | |
用于编辑 e-mail 的字段,可以对e-mail地址进行简单校验 | HTML5 |
单选多选属性
属性值 | 作用 |
---|---|
radio | 单选按钮。 1. 在同一个”单选按钮组“中,所有单选按钮的 name 属性使用同一个值;一个单选按钮组中是,同一时间只有一个单选按钮可以被选择。 2. 必须使用 value 属性定义此控件被提交时的值。 3. 使用checked 必须指示控件是否缺省被选择。 |
checkbox | 复选框。 1. 必须使用 value 属性定义此控件被提交时的值。 2. 使用 checked 属性指示控件是否被选择。 3. 选中多个值时,所有的值会构成一个数组而提交到Web服务器 |
-
按钮属性
属性值 作用 button 无行为按钮,用于结合JavaScript实现自定义动态效果 submit 提交按钮,用于提交表单数据。 reset 重置按钮,用于将表单中内容恢复为默认值。 image 图片提交按钮。必须使用 src 属性定义图片的来源及使用 alt 定义替代文本。还可以使用 height 和 width 属性以像素为单位定义图片的大小。 -
HTML5新增的type值
属性值 作用 备注 date HTML5 用于输入日期的控件 年,月,日,不包括时间 time HTML5 用于输入时间的控件 不含时区 datetime-local HTML5 用于输入日期时间的控件 不包含时区 number HTML5 用于输入浮点数的控件 range HTML5 用于输入不精确值控件 max-规定最大值
min-规定最小值
step-规定步进值
value-规定默认值search HTML5 用于输入搜索字符串的单行文本字段 可以点击 x
清除内容tel HTML5 用于输入电话号码的控件 url HTML5 用于编辑URL的字段 可以校验URL地址格式
属性值 | 作用 | 备注 |
---|---|---|
file | 此控件可以让用户选择文件,用于文件上传。 | 使用 accept 属性可以定义控件可以选择的文件类型。 |
hidden | 此控件用户在页面上不可见,但它的值会被提交到服务器,用于传递隐藏值 |
-
button标签的type属性
属性值 作用 备注 submit 此按钮将表单数据提交给服务器。如果未指定属性,或者属性动态更改为空值或无效值,则此值为默认值。 同 <input type="submit"/>
reset 此按钮重置所有组件为初始值。 同 <input type="reset"
/>button 此按钮没有默认行为。它可以有与元素事件相关的客户端脚本,当事件出现时可触发。 同 <input type="button"/>
HTML5新增属性
属性名 | 作用 | 备注 |
---|---|---|
placeholder | 提示用户输入框的作用。用于提示的占位符文本不能包含回车或换行。 | 仅适用于当type 属性为text, search, tel, url or email时; 否则会被忽略。 |
required | 这个属性指定用户在提交表单之前必须为该元素填充值 | 1. 布尔属性,可省略属性值表示true 2. 当type属性是hidden,image或者button类型时不可使用 |
autocomplete | 自动补全,规定表单或输入字段是否应该自动完成。当自动完成开启,浏览器会基于用户之前的输入值自动填写值。 | 1. 开启为on,关闭为off 2. 可以设置指定的字段为off,关闭自动补全 |
更多表单元素
标签名 | 作用 | 备注 |
---|---|---|
select | 表单的控件,下拉选项菜单 | 与option配合实用 |
optgroup | option的分组标签 | 与option配合实用 |
option | select的子标签,表示一个选项 | |
textarea | 表示多行纯文本编辑控件 | rows表示行高度, cols表示列宽度 |
fieldset | 用来对表单中的控制元素进行分组(也包括 label 元素) | |
legend | 用于表示它的fieldset内容的标题。 | fieldset 的子元素 |
四、实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.wai{
background-color: orange;
}
.zi{
width: 70px;
height: 20px;
color: gray;
float: right;
margin: 20px 10px;
}
.logo{
width: 200px;
height: 25px;
float: left;
}
.team{
width: 300px;
height: 600px;
float: left;
}
a{
text-decoration: none;
}
a:link{
color: gray;
}
a:visited{
color:gray;
}
a:hover{
color: orange;
text-decoration: none;
cursor: pointer;
}
a:active{
color: red;
text-decoration: underline;
}
.quan:visited{
color:orange;
}
.quan:hover{
color: blue;
text-decoration: none;
cursor: pointer;
}
.quan:active{
color: red;
/* text-decoration: underline; */
}
input{
background-color: orange;
color: white;
border: 2px solid white;
}
input:hover{
color: red;
background-color: blue;
border:2px solid red;
}
</style>
</head>
<body>
<div class="wai">
<div style="width: 1000px;height: 24px; margin: auto;"><!--第一层-->
<span style="color:white;line-height:20px;font-size: 13px;">电话:0512-57995109 地址:昆山市庆丰西路639号智谷创意工业园301</span>
</div>
<div style="background-color: rgb(253, 241, 218); ""><!--第二层 logo-->
<div style="background-color: rgb(253, 241, 218);width: 1000px;height: 60px;margin: auto;">
<div class="logo" style="margin: 20px 0 1px 0px;"><img src="./images/logo.png" alt="" width="155px" height="36px"></div>
<div class="zi" ><b><a href="#">联系我们</a></b></div>
<div class="zi" ><b><a href="#">关于我们</a></b></div>
<div class="zi" ><b><a href="#">萌宠咨询</a></b></div>
<div class="zi" ><b><a href="#">宠物用品</a></b></div>
<div class="zi" ><b><a href="#">宠物品种</a></b></div>
<div class="zi" ><b> <a href="#">首页</a>
<div style="margin:-7px 27px;">——</div>
</b></div>
</div>
</div>
<div style="width: 100%;height: 600px; margin: 0px;"><!--第三层 小女孩-->
<img src="./images/bg_slide2.jpg" alt="" height="100%" width="100%"">
</div>
<div style="width: 100%;height: 90px; text-align: center;line-height:80px;"><!--第四层-->
<div style="width: 1350px;"><span style="color: white;font-size: 24px; ">我们的宠物服务更涵盖了宠物的衣食住行医疗等方面。</span></div>
<div style="color: white; border-radius: 10px; width: 100px; height: 50px; text-align: center;line-height:50px;margin: -68px 1050px;">
<a href="#"><input style="border-radius: 10px;width:103px;height: 53px;" type="button" value="联系我们" οnclick="alert('已联系')"></a>
</div>
</div>
<div style="background-color: rgb(253, 241, 218); width: 100%;height: 404px;padding: 10px 0 10px 0;"><!--第五层 萌宠展示 盒中盒-->
<div style=" width: 1000px;height: 400px;background-color: white;margin: auto;">
<div style=" width: 275px;height: 401px;float: left;background-color: orange;">
<div style="margin: 125px 10px;"><span style="color: white;font-size:small;">动物是人类的朋友。在这里,每一个热爱宠物的人
<br>
不但能找到他们想要的,而且能得到比他们想要的更多。
</span></div>
<div style="color: gray;width: 241px;height: 36px;background-color: white;border-radius: 5px;text-align: center;line-height:36px;margin: -118px 16px;">所有品种</div>
</div>
<div style=" width: 715px;height: 62px;float: top;float: right; line-height:65px;">
<b style="font-size: 25px;">萌宠展示</b>
<div style="float: right;width: 40px;height: 40px;background-color: lightgray;text-align: center;line-height:40px;margin:15px 7px 15px 5px;"><b>
<a href="#"><input style="float: right;width: 42px;height: 43px;background-color: lightgray;text-align: center;line-height:40px;margin:-1px -1px 15px 5px;color:black;font-weight:bold;" type="button" value=">" οnclick="alert('正在前往下一页')"></a>
</b></div>
<div style="float: right;width: 40px;height: 40px;background-color: lightgray;text-align: center;line-height:40px;margin:15px 0px 15px 0;"><b>
<a href="#"><input style="float: right;width: 42px;height: 43px;background-color: lightgray;text-align: center;line-height:40px;margin:-1px -2px 15px 5px;color: black;font-weight:bold;" type="button" value="<" οnclick="alert('正在前往上一页')"></a>
</b></div>
</div>
<div style=" width: 234px;height: 322px;float: right;margin: 10px 0;"><img src="images/depositphotos_1.jpg" alt="" width="100%" height="100%">
<div style="color: orange;width: 220px;height: 38px;background-color: white;border-radius: 5px;text-align: center;line-height:37px;margin: -84px 7px;position: absolute;">
<b><a class="quan" href="#">萨摩耶</a></b>
</div>
<div style="color: orange;width: 220px;height: 19px;background-color: gray;text-align: center;line-height:20px;margin: -38px 7px;position: absolute;">
<span style="color: white;font-size:small;">乖巧可爱 微笑天使</span>
</div>
</div>
<div style=" width: 234px;height: 322px;float: right;margin: 10px 5px;"><img src="images/img5-350x500.jpg" alt="" width="100%" height="100%">
<div style="color: orange;width: 220px;height: 38px;background-color: white;border-radius: 5px;text-align: center;line-height:37px;margin: -84px 7px;position: absolute;">
<b><a class="quan" href="#">金毛犬</a></b>
</div>
<div style="color: orange;width: 220px;height: 19px;background-color: gray;text-align: center;line-height:20px;margin: -38px 7px;position: absolute;">
<span style="color: white;font-size:small;">热情友善 自信且不怕生</span>
</div>
</div>
<div style=" width: 234px;height: 322px;float: right;margin: 10px 0;"><img src="images/img4-350x500.jpg" alt="" width="100%" height="100%">
<div style="color: orange;width: 220px;height: 38px;background-color: white;border-radius: 5px;text-align: center;line-height:37px;margin: -84px 7px;position: absolute;">
<b><a class="quan" href="#">法国斗牛犬</a></b>
</div>
<div style="color: orange;width: 220px;height: 19px;background-color: gray;text-align: center;line-height:20px;margin: -38px 7px;position: absolute;">
<span style="color: white;font-size:small;">聪明敏捷 开朗不常犬吠</span>
</div>
</div>
</div>
</div>
<div style="width: 100%;height: 160px;"><!--第六层-->
<img src="images/b2.jpg" alt="" width="100%">
<!-- <div style="color: white; width: 100%;height: 100px;text-align: center;line-height:50px;margin: -500px 0px;float: top; font-size: 25px;">宠物之恋,在于关爱,让你的爱宠享受家的感觉!</div> -->
</div>
<div style="color: white; width: 100%;height: 183px;text-align: center;margin: -93px 0px;font-size: 25px;position: relative;">宠物之恋,在于关爱,让你的爱宠享受家的感觉!</div>
<div style="background-color: rgb(253, 241, 218); width: 100%;height: 660px;position: relative;"><!--第七层 我们的团队 盒中盒-->
<div style="width: 1180px;height: 650px;margin: auto;">
<br>
<p align="center" style="font-size: 22px;"><b>———我们的团队———</b></p>
<br>
<div style="text-align: center;color: gray;"><span style="font-size: 13px;"> 凭借阵容强大的专家团队、媲美港台的医疗设备、遵循规范、严谨的作业流程</span></div>
<div class="team" style="width: 287px;height: 548px;margin: 10px 5px 10px 0px;background-color: white;"><img src="images/image1-500x320.jpg" alt="" height="400" width=100%>
<div style="width: 100px; height: 50px;line-height:40px;"><span style="color: orange;"><span> </span><ins>Dr.Helen</ins></span></div>
<div style="width: 272px; height: 50px;color: gray;margin: 0 10px;"><span>B级美容师,注册兽医师,理论与实践经验丰富。</span></div>
</div>
<div class="team" style="width: 287px;height: 548px;margin: 10px 5px 10px 5px;background-color: white;"><img src="images/image4-500x320.jpg" alt="" height="400" width="100%">
<div style="width: 100px; height: 50px;line-height:40px;"><span style="color: orange;"><span> </span><ins>Dr.Alan</ins></span></div>
<div style="width: 272px; height: 50px;color: gray;margin: 0 10px;"><span>执业兽医师,国际认证资深宠物美容导师。</span></div>
</div>
<div class="team" style="width: 287px;height: 548px;margin: 10px 5px 10px 5px;background-color: white;"><img src="images/image5-500x320.jpg" alt="" height="400" width="100%">
<div style="width: 100px; height: 50px;line-height:40px;"><span style="color: orange;"><span> </span><ins>Dr.Ariv</ins></span></div>
<div style="width: 272px; height: 50px;color: gray;margin: 0 10px;"><span>A级宠物美容师,擅长泰迪的各种造型。</span></div>
</div>
<div class="team" style="width: 287px;height: 548px;margin: 10px 0 10px 5px;background-color: white;"><img src="images/image6-500x320.jpg" alt="" height="400" width="100%">
<div style="width: 100px; height: 50px;line-height:40px;"><span style="color: orange;"><span> </span><ins>Dr.Edwards</ins></span></div>
<div style="width: 272px; height: 50px;color: gray; margin: 0 10px;"><span>宠物医院主诊,宠物美容培训中心讲师。</span></div>
</div>
</div>
</div>
<div style="width: 100%;height: 40px;text-align: center;line-height:40px;"><!--最后一层-->
<span style="color: white;">©XXX市城市宠物医院</span>
</div>
</div>
</body>
</html>
效果图: