小黑子的HTML入土过程第二章
2.HTML+CSS系列教程第二章
2.1 嵌套列表
注意:列表之间可以互相嵌套,形成多层级列表。
什么意思呢?就拿定义列表举例。
<!DOCTYPE html> <!-- 文档声明:告诉浏览器这是一个html文件 -->
<html lang="en"> <!-- html文件的最外层标签:包裹着所有html标签代码 lang="en"是一个英文网站 lang="zh-CH"表示一个中文网站 -->
<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>标题 李在赣神魔</title> <!--头部相关的内容 -->
</head>
<body>
<h1>好美味小吃</h1>
<ul>
<li>
小吃类
<ul>
<li>煮粉干</li>
<li>拌青菜</li>
<li>蛋炒饭</li>
<li>煎蛋</li>
<li>米饭</li>
</ul>
</li>
</ul>
<ul>
<li>
卤味类
<ul>
<li>鸭肠</li>
<li>面筋</li>
<li>牛肚
</li>
<li>猪耳朵</li>
<li>大肠</li>
<li>鱿鱼</li>
</ul>
</li>
</ul>
<ul>
<li>
套餐类
<ul>
<li>卤面筋饭</li>
<li>猪肉肉饭</li>
<li>猪耳朵饭</li>
<li>卤猪脚饭</li>
<li>卤猪舌饭</li>
</ul>
</li>
</ul>
<ul>
<li>
炖罐类
<ul>
<li>猪蹄黄豆</li>
<li>猪肚莲子</li>
<li>猪心枸杞</li>
<li>羊肉枸杞</li>
<li>牛肉枸杞</li>
</ul>
</li>
</ul>
<em>
亲,20元以上可送餐哦!!
</em>
</body>
</html>
效果:
同样的有序列表也是可以嵌套。
2.2 表格标签
<table>:表格的最外层容器
<tr>;定义表格行
<th>:定义表头
<td>:定义表格单元
<caption>:定义表格标题
注意:它们之间是有嵌套关系的,要符合嵌套规范
例子:
<!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>
</head>
<body>
<table>
<caption>天气预报</caption>
<tHead>
<tr>
<th>日期</th>
<th>天气情况</th>
<th>出行情况</th>
</tr>
</tHead>
<tBody>
<tr>
<td>2022年10月23日</td>
<td>晴天</td>
<td>天气晴朗,适合出行</td>
</tr>
</tBody>
<tr>
<td>2022年10月24日</td>
<td>小雨</td>
<td>有小雨,出门带伞</td>
</tr>
<tFood>
</tFood>
</table>
</body>
</html>
效果:
2.3 表格属性
border:表格边框
cellpadding:单元格内的空间
cellspacing:单元格之间的空间
rowspan:合并行
colspan:合并列
align:左右对齐方式(left,center,right)
valign:上下对齐方式(top,middle,bottom)
咪意思呢?请看范例(在2.2代码的前提下):
2.3.1 border:
- (1)
–>
- (2)
—>
2.3.2 cellpadding:
—>将填充的区域扩大了
2.3.3 cellspacing:
—>明显看到每个单元格之间间隙变大
2.3.4 colspan:
当出现两个单元格,例如日期列时:
—>
colspan的作用就是合并两个列,不过要先把相同的单元代码去掉
如图所示:
有几个相同,就colspan=几
—>
2.3.5 rowspan:
同理colspan合并的道理,不过就是换为了合并行:
注意相同的单元一定要去掉
修改前:
修改后:
—>
2.3.6 align:
—>命令上例字体向左
2.3.7 valign:
—>命令表格行上下对齐
2.4 表单标签
<form> :表单的最外层容器
<input>:标签用于搜集用户信息,根据不同的type属性值,展示不同的控件,
如输入框、密码框、复选框等。
input(单标签)标签有一个type属性,决定是什么控件。
2.4.1 text和password
<!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>
</head>
<body>
<form action="填入你想要上传的后台">
<h2>输入框</h2>
<input type="text">
<h2>密码框</h2>
<input type="password">
</form>
</body>
</html>
—>在输入框时输入想要的内容,而在密码框时输入密码时会被隐藏。
2.4.2 checkbox:复选框
<body>
<form action="填入你想要上传的后台">
<h2>输入框</h2>
<input type="text">
<h2>密码框</h2>
<input type="password">
<h2>复选框</h2>
<input type="checkbox">香蕉
<input type="checkbox">桃子
<input type="checkbox">西瓜
</form>
</body>
—>如图复选框可以任意勾选想要的单位
2.4.3 radio:单选框
<h2>单选框</h2>
<input type="radio">男
<input type="radio">女
<input type="radio">第三性别者
—>
但是神奇的是这样写,单选框的单元是能都被选择的,与我们的需求不符
所以要进行更改
<input type="radio" name="你随便自定义一个值">男
<input type="radio" name="你随便自定义一个值">女
<input type="radio" name="你随便自定义一个值">第三性别者
—>这样就可以单选切换,不会重复了
2.4.4 file:上传文件
<h2>上传文件</h2>
<input type="file">
—>
2.4.5 submit:提交到后台
<h2>提交</h2>
<input type="submit">
—>点击后提交到后台
2.4.6 reset:回到初始状态
<h2>重置</h2>
<input type="reset">
—>点击后重置到初始状态
2.4.7 checked:表示一开始就被选中
<input type="checkbox" checked>香蕉
2.4.8 disabled:表示有的不能选择
<input type="checkbox" disabled>西瓜
2.4.9 placeholder:起到提示作用
<h2>输入框</h2>
<input type="text" placeholder="请输入用户名">
<h2>密码框</h2>
<input type="password" placeholder="请输入密码">
2.5 表单相关标签
2.5.1 textarea :多行文本框
<h2>多行文本框</h2>
<textarea cols="30" rows="10"></textarea>
或者
<h2>多行文本框</h2>
<textarea cols="10" rows="30"></textarea>(参数自己定义)
—>可以在文本框内输入自己想输的内容
2.5.2select和option :下拉菜单
<h2>下拉菜单</h2>
<select>
<option>北京</option>
<option>上海</option>
<option>杭州</option>
</select>
—>
2.5.3 selected:改变菜单第一项
<h2>下拉菜单</h2>
<select>
<option>北京</option>
<option selected>上海</option>
<option>杭州</option>
</select>
或者
<h2>下拉菜单</h2>
<select>
<option selected>请选择</option>
<option>北京</option>
<option>上海</option>
<option>杭州</option>
</select>
—>
or
当不想选择某一项时,可用2.4.8的disabled
2.5.4 size:可以显示具体几个
<h2>下拉菜单</h2>
<select>
<option selected disabled>请选择</option>
<option>北京</option>
<option>上海</option>
<option>杭州</option>
</select>
<select size="3">
<option>北京</option>
<option>上海</option>
<option>杭州</option>
</select>
2.5.5 multiple:表示可以多选
<select multiple="3">
<option>北京</option>
<option>上海</option>
<option>杭州</option>
</select>
2.5.6label:辅助表单
用于提升用户体验
比如在2.4.3 radio中点击男或女是不能选择的,只能点击空心内,但有了label就不一样了
<input type="radio" name="你随便自定义一个值" id="man">
<label for="man">男</label>
<input type="radio" name="你随便自定义一个值" id="woman">
<label for="woman">女</label>
2.6 div与span
div全称为division:
“分割、分区”的意思,<div>标签用来划分一个区域,
相当于一块区域容器,可以容纳段落、标题、表格、图像等各种网页元素。
即HTML中大多数的标签都可以嵌套在<div>标签中,
<div>中还可以嵌套多层<div>,用来将网页分割成独立的、不同的部分,
来实现网页的规划和布局。
span(内联)∶
用来修饰文字的,div与span都是没有任何默认样式的,需要配合CSS才行。
范例:
3.CSS部分
3.1 CSS基础语法
选择器{属性1:值1 ;属性2∶值2 }
width :宽
height :高
background-color :背景色
长度单位:
1. px ->像素
2. 2.%->百分比
1.外容器1->600px 当前容器50% ->300px
2.外容器2 ->400px当前容器50% ->200px
CSS注释:
/*CSS注释的内容*/
<!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>
div{width:100px;height: 100px;background-color: red;}
</style>
</head>
<body>
<div>这是一个块</div>
</body>
</html>
效果:
3.2 内联样式与内部样式
内联(行内、行间)样式:
在html标签上添加style属性来实现的
内部样式:
在<style>标签内添加的样式
注:内部样式的优点,可以复用代码
—>这样写虽然效果一样,但代码重复,繁琐
建议改成在head中写
<style>
div{width:100px;height: 100px;background-color: red;}
</style>
3.3 外部样式以及两种写法
引入一个单独的cSS文件,name. css
通过link标签引入外部资源,rel属性指定资源跟页面的关系,href属性资
源的地址。
通过@import方式引入外部样式,但这种会有很多问题,不建议使用。
link的rel属性: http://www.w3school.com.cn/tags/att_link_rel.asp
link与@import区别:https://www.cnblogs.com/my–sunshine/p/6872224.html
3.4 CSS颜色表示法
1,单词表示法: red blue green yellow ...
2.十六进制表示法:#000000, #ffffff
0 1 2 3 4 5 6 7 8 9 a b c d e f
3. rgb三原色表示法:rgb(255,255,255);取值范围~ 255
4. 提取颜色的下载地址:https : //www.baidufe.com/fehelper
--> photoshop工具
3.5 CSS背景样式
background-color:背景颜色
background-image:背景图片
url(背景地址)
默认:会水平垂直都铺满背景图
background-repeat:背景图片的平铺方式
repeat-x x轴
repeat-y y轴
repeat(x,y都进行平铺,默认值)
no-repeat都不平铺
background-position:背景图片的位置
xy : number(px、%)|单词
x : left、center、 righty : top、center. bottom
background-attachment:背景图随滚动条的移动方式
scroll :默认值(背景位置是按照当前元素进行偏移的)
fixed (背景位置是按照浏览器进行偏移的)
3.6 边框样式
border-style :边框样式
solid :实线
dashed :虚线
dotted :点线
border-width :边框大小
px ...
border-color :边框颜色
red #f00 ...
边框也可以针对某一天边进行单独设置:例 border-left-style :
方向是 left、right、top、bottom
透明颜色:transparent
范例:
<style>
div{width: 300px;height: 300px;border-right-style:dotted;
border-right-width: 10px;border-color: red;}
</style>
或者
<style>
div{width: 300px;height: 300px;border-style:dotted;
border-width: 10px;border-color: blue;}
</style>
效果:
or
transparent应用:
3.7 family字体类型
font-family :字体类型
英文字体:Arial ,'Times New Roman'
中文字体∶微软雅黑,宋体
中文字体的英文名称
微软雅黑:'Microsoft YaHei'
宋体:SimSun
注意:
1.多个字体类型的设置:在系统字体中可以找到多种
2.引号的添加:有空格的字体必须加''
补充:
3.8 CSS的字体大小和粗细
font-size :字体大小
默认:16px
写法: number(px)|单词( small large ...不推荐使用)
font-weight :字体粗细
模式:正常(normal )加粗( bold )
写法:单词(normal、bold) | number ( 100 200 ..... 900 ,
100到5oo都是正常的,60o都900都是加粗的)
font-style :字体样式
模式:正常(normal )斜体( italic )
写法:单词(normal 、 italic )
注: oblique也是表示斜体,用的比较少,一般了解即可。
区别: 1.italic带有倾斜属性的字体的才可以设置倾斜操作。
2.oblique没有倾斜属性的字体也可以设置倾斜操作。
color:字体颜色
范例:
<!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>
div{font-family: 'Times New Roman'; font-size: 30px;
font: weight 900px; font-style: oblique ; color: blue;}
</style>
</head>
<body>
<div>这是一段文字</div>
<div>this is a text</div>
</body>
</html>
效果:
3.9 文本修饰与文本大小
text-decoration:文本装饰
下划线: underline
删除线: line-through
上划线: overline
不添加任何装饰: none
注:添加多个文本修饰: line-through underline overline
text-transform:文本大小写(针对英文段落)
小写:lowercase
大写:uppercase
只针对首字母大写:capitalize
3.10 文本缩进与文本对齐
text-indent :文本缩进
首行缩进
em单位:相对单位,1em永远都是跟字体大小相同
text-align :文本对齐方式
对齐方式: left 、right、center、 justify(两端点对齐)
3.11 文本的行高
line-height:定义行高
什么是行高,一行文字的高度,上边距和下边距的等价关系。
默认行高:不是固定值,而是变化的。根据当前字体的大小再不断的变化。
取值: number( px )
scale(比例值,跟文字大小成比例的)
视图:
3.11 文本间距与英文折行
letter-spacing:字之间的间距
word-spacing :词之间的间距(针对英文段落的)
英文和数字不自动折行的问题:
1.word -break : break-all;(非常强烈的折行)
2.word-wrap : break-word;(不是那么强烈的折行)
不折行前:
强烈折行后:
非强烈折行(会有空白):
3.12 CSS复合样式
一个CSS属性只控制一种样式,叫做单一样式。
一个CSS属性控制多种样式,叫做复合样式。
复合样式:
background : red url() repeat 0 0;
border : 1px red solid;
font:
weight style size family
style weight size family
weight style size/line-height family
(注:最少要有两个值size family)
复合写法有的是不需要关心顺序,例如background、border;
有的是需要关心顺序,例如font。
注:尽量不要混写,如果非要混写,那么一定要先写复合样式再写单一样式。
<style>
div{ width: 300px; height : 300px;
如果先写复合-> background : url(./img/dog.jpg)no-repeat center center;
再写单一-> background-color : red;
border-right : dashed 2px blue;
font : bold italic
30px/100px宋体;}
</style>
范例:
单一样式的红色背景会被实现
<style>
div{ width: 300px; height : 300px;
如果先写单一-> background-color : red;
再写复合-> background : url(./img/dog.jpg)no-repeat center center;
border-right : dashed 2px blue;
font : bold italic
30px/100px宋体;}
</style>
单一的红色背景会被复合默认的背景颜色覆盖
3.13 CSS选择器
ID选择器
css : #elem{}
html : id= "elem”
注∶
1.在一个页面中,ID值是唯一的。
2.命名规范,字母_-数字(命名的第一位不能是数字)。
3.命名方式,驼峰式、下划线式、短线式。
驼峰写法:searchButton(小驼峰)
SearchButton(大驼峰)
searchSmallButton
短线写法: search-small-button
下划线写法:search_small_button
范例:
<style>
#div1{width: 700px;height: 400px;
background:red url() no-repeat center center;
font : bold italic 30px 宋体;}
/* .box{background:red;}
.content{font-size: 100px;background: blue;} */
div2{width: 700px;height: 400px;
background:red url() no-repeat center center;
font : bold italic 30px 宋体;}
</style>
</head>
<body>
<div id="div1">这是一段文字</div>
<div id="div2">this is a text</div>
<!-- <div class="box content">这是一段文字</div> -->
</body>
3.14 CLASS选择器及注意事项
CLASS选择器
.elem{}
class="elem"
注:
1. class选择器是可以复用的。
2.可以添加多个class样式。
3. 多个样式的时候,样式的优先级根据CSS决定,而不是class属性中的顺序。
4.标签+类的写法
class与id属性的区别:
class能够引用多个样式
id只能引用一个样式
范例:
<style>
#div1{width: 700px;height: 400px;
background:red url() no-repeat center center;
font : bold italic 30px 宋体;}
div2{width: 700px;height: 400px;
background:red url() no-repeat center center;
font : bold italic 30px 宋体;}
</style>
</head>
<body>
<div id="div1">这是一段文字</div>
<div id="div2">this is a text</div>
</body>
使用id属性时,在style中写div要区别,并且div签名要加#
没加#就不会产生效果
而
<style>
.box{background:red;}
.content{font-size: 100px;backgoround:blue;}
</style>
</head>
<body>
<div class="box content">这是一段文字</div>
</body>
—>样式的优先级根据CSS决定,而不是class属性中的顺序
所以背景不是红色