文本相关属性

字体相关

字体大小:font-size
取值:数字+px
字体粗细:font-weight
正常:normal 或者 400
加粗:bold 或者 700
字体样式:font-style
正常:normal
倾斜:italic
字体系列:font-family
宋体,仿宋,微软雅黑,隶书….

字体连写
font: font-style font-weight font-size font-family (swsf 稍微舒服)

  1. <style>
  2. div{
  3. font-size: 30px;
  4. font-weight: 700;
  5. font-style: italic;
  6. font-family: 隶书;
  7. }
  8. p{
  9. /* font:italic 700 30px 隶书;*/ /* 连写 */
  10. font: 30px 隶书; /* 可以省略:但是只能省略前两个,如果省略取值就相当取默认值*/
  11. }
  12. </style>

文本样式相关

文本颜色
color
文本水平对齐
text-align (写在父元素上)
文本装饰线
text-decoration none(无)
文本首行缩进
text-indent

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. /* 文本颜色 */
  9. color: red;
  10. /* 文本水平对齐方式 left 居左(默认) center: 居中 right: 居右 */
  11. text-align: center;/*含义:让他里面的元素居中,所以一般写在父标签里面 */
  12. /* 下划线 none:无 underline:下划线 overline: 上划线 line-through :删除线 */
  13. text-decoration:none;
  14. }
  15. a{
  16. /* 一般用于去掉a标签的下划线 */
  17. text-decoration:none;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div>中国移动</div>
  23. <div>
  24. <a href="#">百度一下</a><!-- 默认带了下划线 -->
  25. </div>
  26. </body>

文本相关属性细节

行级或者块级元素的水平居中

1.对于行级元素:水平居中,可以使用text-align:center.这个属性需要写在父元素上
2,对于块级元素(div p),水平居中,一般使用 margin:0 auto 这个属性,这个属性写在当前元素上

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. body{
  10. text-align: center;
  11. }
  12. div{
  13. width: 100px;
  14. height: 100px;
  15. background: red;
  16. margin: 0 auto;/* 左右外间距平分 */
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <!-- 1.对于行级元素:水平居中,可以使用text-align:center.这个属性需要写在父元素上 -->
  22. <a href="#">百度</a>
  23. <!-- 2,对于块级元素(div p),水平居中,一般使用 margin:0 auto 这个属性,这个属性写在当前元素上 -->
  24. <div></div>
  25. </body>
  26. </html>

line-height行高

作用
控制一行的上下间距(行间距)
属性名
line-height
取值
数字+px
应用
1,可以让单行文本垂直居中显示(设置 line-height=文字父元素的高度)
2,在网页精准布局的时候,设置line-height:1,可以取消默认行间距
注意
行高和font连写的时候,注意覆盖问题
font:font-style font-weight font-size/line-height font-family

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 100px;
  9. height: 100px;
  10. background: pink;
  11. text-align: center;/* 水平居中 */
  12. line-height: 100px;/* 让行高=元素高度,就可以垂直居中 */
  13. }
  14. p{
  15. /*text-indent:首行缩进 1em=当前标签的font-size的大 */
  16. text-indent: 2em;
  17. line-height: 20px;/* 控制行间距 */
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div>中国移动</div>
  23. <!-- p>lorem50 自动换行的快捷键是alt+z-->
  24. <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Corporis, repudiandae. Eligendi repellat exercitationem quo quis aperiam quos provident dolor unde voluptatum, rem veniam ad alias, earum aut harum voluptate ducimus ratione. Blanditiis obcaecati dolor beatae ex atque, dolorem, voluptatem est, itaque reiciendis corporis similique iure facilis repellat minima quos dicta.</p>
  25. </body>

行高细节问题

  1. <style>
  2. div{
  3. width: 200px;
  4. height: 200px;
  5. background:pink;
  6. /* 1,在网页精准布局的时候,会设置行高为1.来取消上下默认间距 */
  7. /* line-height: 1; */
  8. /* 2,先设置行高,在设置字体,就会出现问题
  9. font: font-style font-weight font-size/line-height font-family (swsf 稍微舒服)
  10. 因为font-size属性后面还有一个line-height,如果没有设置,行高会被默认行高覆盖
  11. */
  12. /* line-height: 200px;
  13. font:normal 700 30px 宋体; */
  14. /* 解决方式一: 把设置行高的放在字体的右边 */
  15. /* font:normal 700 30px/200px 宋体; */
  16. /* 解决方式二:把设置行高放在下面 */
  17. font:normal 700 30px 宋体;
  18. line-height: 200px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div>中国移动</div>
  24. </body>

背景相关属性

background其他属性

1,背景颜色
background-color 或者 background
2,背景图片
background-image 或者 background
3,背景平铺
background-repeat
4,背景位置
background-position

连写
background:background-color background-image background-repeat background-position

  1. <style>
  2. div{
  3. width: 400px;
  4. height: 400px;
  5. /* 背景颜色 */
  6. background-color: pink;
  7. /* background: pink; */
  8. /* 背景图片 */
  9. background-image: url(./img/1.jpg);
  10. /* background: url(./img/1.jpg); */
  11. /* 背景平铺 repeat:平铺 no-repeat:不平铺 repeat-x:x轴平铺 repeat-y:y轴平铺 */
  12. background-repeat: no-repeat;/* 默认是平铺 */
  13. /* 背景位置 第一个参数是水平方向的位置 第二个参数是垂直方向的位置 */
  14. /* background-position: 50px 50px; */
  15. /* background-position: center center; */ /* 居中 */
  16. background-position: 50% 50%; /* 居中 */
  17. }
  18. p{
  19. width: 400px;
  20. height: 400px;
  21. background: skyblue url(./img/1.jpg) no-repeat center center;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div></div>
  27. <p></p>
  28. </body>

颜色取值了解

方式一:关键字
方式二:rgb
方式三:rgba
方式四:#16进制

  1. <style>
  2. div{
  3. /* 方式一:关键字 */
  4. color: red;
  5. /* 方式二:rgb */
  6. color: rgb(255,255,255);
  7. color: rgb(0,0,0);
  8. /* 方式三:rgba */
  9. color: rgb(0,0,0,.5);
  10. /* 方式三:16进制 */
  11. color: #ffffff;
  12. color: #fff;
  13. color: #000000;
  14. color: #000;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div>中国移动</div>
  20. </body>

_img和background-image区别

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <!-- img是一个标签,设置了图片之后,有默认的宽高 图片可以撑开img标签 -->
  9. <img src="./img/1.jpg" alt="">
  10. <!-- 使用div+背景图片 背景图片仅仅起到装饰效果,如果div没有设置高度,无法显示出来,因为背景图片不能撑开div标签 -->
  11. <div style="background: url(./img/1.jpg);"></div>
  12. </body>

精灵图应用

Ø 场景:项目中将多张小图片,合并成一张大图片,这张大图片称之为精灵图
Ø 优点:减少服务器发送次数,减轻服务器的压力,提高页面加载速度

使用步骤
1. 创建一个盒子
2. 设置盒子大小为小图片大小
3. 设置精灵图为盒子的背景图片
4. 将小图片左上角坐标 取负值,设置给盒子的background-position:x y

  1. <style>
  2. div{
  3. width: 40px;
  4. height: 40px;
  5. background: url(./img/icon.png) -81px 3px;/* 后面两个是用来调节图片的位置,用于显示不同的图标 */
  6. }
  7. </style>

CSS三大特性

层叠性

后写的样式会覆盖先写的样式(相同样式会覆盖,不同的样式就会叠加)

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. color: blue;
  9. }
  10. div{
  11. color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div>中国移动</div>
  17. </body>

继承性

子元素会继承父元素文本相关的属性,可以在一定程度上减少代码

应用场景
1,直接给ul设置 list-style: none 就可以去掉无序列表中的小圆点
2,统一浏览器默认文字的大小,直接给body标签设置统一的font-size

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. /* - font-
  8. - text-
  9. - line-
  10. - color */
  11. .father{
  12. font: 30px 宋体;
  13. text-decoration: underline;
  14. line-height: 100px;
  15. color: red;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="father">
  21. 父亲
  22. <div class="son"></div>
  23. 儿子
  24. </div>
  25. </body>

应用:

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. /* 直接给ul设置 list-style: none 就可以去掉无序列表中的小圆点*/
  8. ul{
  9. list-style: none;
  10. }
  11. /* 统一浏览器默认文字的大小,直接给body标签设置统一的font-size */
  12. body{
  13. font-size: 20px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <ul>
  19. <li>我是li元素</li>
  20. <li>我是li元素</li>
  21. <li>我是li元素</li>
  22. <li>我是li元素</li>
  23. <li>我是li元素</li>
  24. <li>我是li元素</li>
  25. <li>我是li元素</li>
  26. <li>我是li元素</li>
  27. <li>我是li元素</li>
  28. <li>我是li元素</li>
  29. </ul>

优先级

特点:
不同的选择器具有不同的优先级,优先级高的选择器样式会覆盖优先级低的选择器样式
公式:
继承<元素选择器<类选择器注意点:
1,!important 写在属性值的后面,分号的前面
2,!important 不能提升继承的优先级,只要是继承,优先级最低
3,实际开发中不建议使用!important

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. a{
  8. color: red;/* 0,0,0,1 */
  9. }
  10. li a{
  11. color: green;/* 0,0,0,2 */
  12. }
  13. .mylink{
  14. color: yellow;/* 0,0,1,0 */
  15. }
  16. #myli a{
  17. color: blue;/* 0,1,0,1 */
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="mydiv">
  23. <ul id="myul">
  24. <li id="myli">
  25. <a href="#" class="mylink">中国移动</a>
  26. </li>
  27. </ul>
  28. </div>
  29. </body>
  1. <head>
  2. <meta charset="utf-8">
  3. <title>第1题:普通题</title>
  4. <style>
  5. #father #son {
  6. color:blue; /* 2,0,0 */
  7. }
  8. #father p.c2 {
  9. color:black;/* 1,1,1 */
  10. }
  11. div.c1 p.c2 {
  12. color:red;/*0,2,2 */
  13. }
  14. #father {
  15. color:green !important;/*1,0,0 */ /* 因为!important属性被继承了 */
  16. }
  17. div#father.c1 {
  18. color: yellow ;/* 1,1,1 */
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="father" class="c1">
  24. <p id="son" class="c2">
  25. 这行文本是什么颜色的?
  26. </p>
  27. </div>
  28. </body>

盒子模型

行盒和块盒分类

万物皆盒子,行级元素对应的是行盒,块级元素对应的是块盒

  • 块盒: 独占一行, 可以设置宽高
    如果不设置宽度,默认和父盒子一样宽
    如果不设置高度,默认是由内容撑开的
    display属性的值默认是block

    - 行盒: 不独占一行, 高度由字体大小撑开
    设置宽高没有效果,宽高是由内容撑大的
    display属性的值默认是inline

    互转
    行级元素->块级元素 display: block;
    行级元素->行内块元素 display: inline-block; 可以设置宽高,也可以在一行显示 ```html <!—

    1. 万物皆盒子,行级元素对应的是行盒,块级元素对应的是块盒
    2. - 块盒: 独占一行, 可以设置宽高
    3. 如果不设置宽度,默认和父盒子一样宽
    4. 如果不设置高度,默认是由内容撑开的
  1. - 行盒: 不独占一行, 高度由字体大小撑开
  2. 设置宽高没有效果,宽高是由内容撑大的
  3. -->
  4. <h1>中国移动</h1>
  5. <p>中国联通</p>
  6. <div>中国电信</div>
  7. <span>俄罗斯</span>
  8. <a>乌克兰:欧洲的子宫</a>

  1. <a name="YrK5c"></a>
  2. ## 行盒和块盒的相互转换
  3. ```html
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Document</title>
  9. <style>
  10. a{
  11. width:200px;
  12. height: 200px;
  13. background: pink;
  14. /* 转成块元素*/
  15. /* display: block; */
  16. /* 转成行内块元素 : 可以设置宽高,也可以在一行显示 */
  17. display: inline-block;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <a href="#">我是一条酸菜鱼</a>
  23. <a href="#">我是一条酸菜鱼</a>
  24. <a href="#">我是一条酸菜鱼</a>
  25. </body>

盒子元素的组成

边框;border
内边距:padding
外边距:margin

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 100px;
  9. height: 100px;
  10. background: pink;
  11. margin: 10px;/* 外边距 */
  12. padding: 10px;/* 内边距*/
  13. border:1px solid green;/* 组成 */
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div>1</div>
  19. <div>2</div>
  20. </body>

盒子的边框详解

border-width 边框宽度
border-style 边框样式
border-color 边框颜色

连写
border: 2px solid red;

  1. <head>
  2. <meta charset="UTF-8">
  3. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. div{
  8. width: 300px;
  9. height: 300px;
  10. /* background: pink; */
  11. /* 1,单个属性 */
  12. /* 边框宽度 */
  13. border-width: 5px;/* 设置的是上下左右的边框宽度 */
  14. /* 边框样式:
  15. - none:没有边框即忽略所有边框的宽度(默认值)
  16. - solid:边框为单实线(最为常用的)
  17. - dashed:边框为虚线
  18. - dotted:边框为点线
  19. */
  20. border-style: dashed;
  21. /* 边框颜色 */
  22. border-color: green;
  23. /* 2,连写形式 */
  24. border: 2px solid red;
  25. /* 3,单方向的设置 */
  26. border-bottom: 10px dashed pink;
  27. /* 4,边框圆角 */
  28. /* border-radius: 50px; */
  29. border-radius: 50%;/* 圆形 */
  30. }
  31. p{
  32. width: 300px;
  33. height: 300px;
  34. background-color: pink;
  35. border-width: 5px 10px 15px 20px;/* 上右下左 顺时针方向 */
  36. border-style: solid;/* 这个其实也是设置的4个边框的样式 */
  37. border-radius: 0px 0px 50px 50px;/* 上右下左 顺时针方向 */
  38. border-radius: 0px 50px;/* 只写2个值 上下是0px 左右是50px */
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div></div>
  44. <p></p>
  45. </body>

浏览器调试技巧

1,打开方式
右键 -> 检查
2,选择元素
两种方式
3,控制样式
修改
添加
勾选左边方框
4,特殊情况
出现删除线
出现小三角