第06章-CSS3布局属性

2022-05-18 23:32 更新

第06章-前端核心技术-CSS3布局属性

项目经理(作者):张明星

学习目标

  1. 了解定位的概念

  1. 掌握如何实现元素的任意布局 重点 难点

  1. 掌握实际开发特效的实现 难点
  2. 掌握CSS复杂选择器的使用 重点
  3. 掌握CSS属性选择器的使用方法
  4. 掌握CSS特效制作的方法 重点 难点

CSS Float(浮动)

Float(浮动),只能使元素向左或向右移动,其周围的元素的内容会被浮动的元素的内容挤到周围。

Float(浮动),往往是用于图像,但它在布局时一样非常有用。

属性 描述
float 使元素浮动到左边或者右边(left、right)

  • 浮动元素只能左右移动而不能上下移动。
  • 一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
  • 浮动元素之后的元素将围绕它。
  • 浮动元素之前的元素将不会受到影响。
  • 浮动元素的空间将不存在,所以这个元素后边的元素会占据浮动元素原本的空间,导致浮动元素覆盖后面的元素,后面的元素需要使用clear属性清除浮动的覆盖。

案例01

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. img{
  8. float: right;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <img src="image/avatar.jpg"/>
  14. 觉得很卡机的话那的艰苦撒旦的艰苦撒谎空间大倒萨立刻就发生范德萨浪费
  15. 疯狂的思路发生分类考试的价格快速拉高饿死了咖啡连锁店过的思路开发公
  16. 疯狂的思路发生分类考试的价格快速拉高饿死了咖啡连锁店过的思路开发公
  17. 疯狂的思路发生分类考试的价格快速拉高饿死了咖啡连锁店过的思路开发公
  18. 疯狂的思路发生分类考试的价格快速拉高饿死了咖啡连锁店过的思路开发公
  19. </body>
  20. </html>

效果展示

图片浮动到最右边,将正常排版的文字挤到旁边,并不会覆盖住文字。

案例02

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. ul{
  8. list-style-type: none;
  9. }
  10. li{
  11. float: left;
  12. padding: 20px;
  13. background-color: #DC143C;
  14. color: white;
  15. }
  16. h4{
  17. background-color: blueviolet;
  18. color: white;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <ul>
  24. <li>家具</li>
  25. <li>服饰</li>
  26. <li>汽车</li>
  27. <li>装饰</li>
  28. <li>鞋包</li>
  29. <li>儿童</li>
  30. </ul>
  31. <h4>内容</h4>
  32. </body>
  33. </html>

效果展示

清除浮动

清除浮动的属性一般用于浮动元素后面的元素,用户清除上面浮动元素对其产生的影响

属性 描述
clear 清除上面浮动元素的影响(left、right、both)

案例03

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. ul{
  8. list-style-type: none;
  9. }
  10. li{
  11. float: left;
  12. padding: 20px;
  13. background-color: #DC143C;
  14. color: white;
  15. }
  16. h4{
  17. background-color: blueviolet;
  18. color: white;
  19. clear: both;/*清除上面浮动元素的影响,并从新的行开始*/
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <ul>
  25. <li>家具</li>
  26. <li>服饰</li>
  27. <li>汽车</li>
  28. <li>装饰</li>
  29. <li>鞋包</li>
  30. <li>儿童</li>
  31. </ul>
  32. <h4>内容</h4>
  33. </body>
  34. </html>

效果展示

总结

  1. 浮动的特征:
    1. 把任何元素看成:元素 = 空间 + 内容
    2. 浮动的元素只能浮动到左边 或者 右边,不能居中
    3. 浮动后的元素的空间不存在,但内容还存在
    4. 浮动元素的内容会和下面的元素显示在同一行
    5. 浮动元素的内容会把下面的元素的的内容挤压到旁边

  1. 清除浮动
    1. 想让浮动的元素独占一行,不影响后面的元素排版,需要给后面的第一个元素加clear:bath清除浮动
    2. 当一个元素的所有子元素都浮动后,子元素的空间都不存在了,这个元素的高度会变为0px,导致这个元素的背景等看不到,需要给这个元素加overflow:hidden;或者display:inline-block;

CSS position (定位)

position 属性指定了元素的定位类型。

属性 描述
Static 静态定位,默认值,就是没有定位。
Relative 相对定位,相对当前元素位置
Fixed 固定定位,在页面上固定的位置出现
Absolute 绝对定位,相对于上级已经定位的元素的位置

static 静态定位(默认)

HTML元素的默认值,即没有定位,元素出现在正常的流中。

静态定位的元素不会受到 top, bottom, left, right影响。

fixed 固定定位

元素的位置相对于浏览器窗口是固定位置。

即使窗口是滚动的它也不会移动。

案例04

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. body{
  8. height: 2000px;
  9. margin: 0;
  10. }
  11. ul{
  12. list-style-type: none;
  13. margin: 0;
  14. padding: 0;
  15. text-align: center;
  16. background-color: #DC143C;
  17. width: 100%;
  18. position: fixed;
  19. top: 0;
  20. padding: 4px 0;
  21. }
  22. li{
  23. display: inline-block;
  24. padding: 15px 5%;
  25. background-color: rgba(0,0,255,0.5);
  26. color: white;
  27. font-weight: bold;
  28. border-radius: 6px;
  29. }
  30. li:hover{
  31. background-color: rgba(0,255,0,0.5);
  32. cursor: pointer;
  33. }
  34. h4{
  35. text-align: center;
  36. }
  37. .bottom{
  38. position: fixed;
  39. background-color: #DC143C;
  40. width: 100%;
  41. bottom: 0;
  42. line-height: 50px;
  43. text-align: center;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <ul>
  49. <li>首页</li>
  50. <li>动态</li>
  51. <li>新闻</li>
  52. <li>联系</li>
  53. </ul>
  54. <div class="bottom">
  55. 底部
  56. </div>
  57. </body>
  58. </html>

效果展示

relative 相对定位

相对定位元素的定位是相对其正常位置。

相对定位元素经常被用来作为绝对定位元素的容器块。

可以移动的相对定位元素的内容和相互重叠的元素,它原本所占的空间不会改变。

absolute 绝对定位

绝对定位的元素的位置相对于最近的已定位父元素,如果元素没有已定位的父元素,那么它的位置相对于<html>

absolute 定位使元素的位置与文档流无关,因此不占据空间。

absolute 定位的元素和其他元素重叠。

案例05

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <link rel="stylesheet" type="text/css" href="2.css"/>
  7. </head>
  8. <body>
  9. <h2>商品分类</h2>
  10. <ul>
  11. <li>女装/内衣
  12. <div class="right">
  13. <div class="l">
  14. <div class="item">
  15. <div class="ll">
  16. 当季流行>
  17. </div>
  18. <div class="lr">
  19. 春季新品商场同款气质连衣裙卫衣时髦外套毛针织衫休闲裤牛仔裤毛呢大衣无痕文胸运动文胸潮流家居服百搭船袜
  20. </div>
  21. </div>
  22. </div>
  23. <div class="r">
  24. <img src="../day17/image/1-3.jpg"/>
  25. </div>
  26. </div>
  27. </li>
  28. <li>男装/运动户外</li>
  29. </ul>
  30. </body>
  31. </html>

效果展示

重叠的元素

元素的定位与文档流无关,所以它们可以覆盖页面上的其它元素

z-index属性指定了一个元素的堆叠顺序(哪个元素应该放在前面,或后面)

一个元素可以有正数或负数的堆叠顺序

案例06

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. .img1{
  8. position: absolute;
  9. width: 200px;
  10. left: 50px;
  11. top: 120px;
  12. z-index: 2;
  13. }
  14. .img2{
  15. position: absolute;
  16. width: 200px;
  17. z-index: -1;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <img src="image/avatar.jpg" class="img1"/>
  23. <img src="image/meinv.jpg" class="img2"/>
  24. </body>
  25. </html>

效果展示

CSS复杂选择器

子元素(后代)选取器

后代选取器匹配元素所有的内部元素,无论嵌套多少层都可以被选择。符号“空格”

案例07

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>后代选取器</title>
  6. <style type="text/css">
  7. #content .child{
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="content">
  14. <div class="child">
  15. 第一层前套的子元素1
  16. <div class="child">
  17. 第二层前套的子元素1
  18. </div>
  19. </div>
  20. <div class="child">
  21. 第一层前套的子元素2
  22. <div class="child">
  23. 第二层前套的子元素2
  24. </div>
  25. </div>
  26. </div>
  27. </body>
  28. </html>

效果展示

直接子元素选择器

与后代选择器相比,直接子元素选择器(Child selectors)只能选择作为某元素直接子元素,嵌套的不能被选中。

直接子元素使用“>”符号来表示

案例08

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>直接子元素选择器</title>
  6. <style type="text/css">
  7. #content > .child2-1{
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="content">
  14. <div class="child1-1">
  15. 第一层前套的子元素1
  16. <div class="child2-1">
  17. 第二层前套的子元素1
  18. </div>
  19. </div>
  20. <div class="child1-1">
  21. 第一层前套的子元素2
  22. <div class="child2-1">
  23. 第二层前套的子元素2
  24. </div>
  25. </div>
  26. </div>
  27. </body>
  28. </html>

效果展示

相邻兄弟选择器

相邻兄弟选择器(Adjacent sibling selector)可选择后面紧接在一个元素后的元素,且二者有相同父元素。只能选择一个。

相邻兄弟选择器使用“+”符号来表示

案例09

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>相邻兄弟选择器</title>
  6. <style type="text/css">
  7. p + .child1-1{
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="content">
  14. <p>段落</p>
  15. <div class="child1-1">第一层前套的子元素1</div>
  16. <div class="child1-1">第一层前套的子元素2</div>
  17. </div>
  18. </body>
  19. </html>

效果展示

后续兄弟选择器

后续兄弟选择器选取所有指定元素之后的相邻兄弟元素。

后续兄弟选择器使用“~”符号来表示

案例10

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>后续兄弟选择器</title>
  6. <style type="text/css">
  7. p ~ .child1-1{
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div id="content">
  14. <p>段落</p>
  15. <div class="child1-1">第一层前套的子元素1</div>
  16. <div class="child1-1">第一层前套的子元素2</div>
  17. </div>
  18. </body>
  19. </html>

效果展示

伪类(元素)选择器

选择器 示例 示例说明
:checked input:checked 选择所有选中的表单元素(单选和复选)
:disabled input:disabled 选择所有禁用的表单元素
:enabled input:enabled 选择所有启用的表单元素
:not(selector) :not(p) 选择所有p以外的元素
:in-range input:in-range 选择元素值在指定(数字输入框)min和max范围内的值
:out-of-range input:out-of-range 选择元素值在指定(数字输入框)min和max范围外的值
:invalid input:invalid 选择所有无效的元素
:valid input:valid 选择所有有效值的属性
:first-letter p:first-letter 选择每个<p> 元素的第一个字母
:first-line p:first-line 选择每个<p> 元素的第一行
:last-child p:last-child 选择所有p元素的最后一个元素
:last-of-type p:last-of-type 选择每个p元素是其母元素的最后一个p元素
:first-of-type p:first-of-type 选择每个父元素是p元素的第一个p子元素
:first-child p:first-child 选择器匹配属于任意元素的第一个子元素的 <p> 元素
:nth-child(n) p:nth-child(2) 选择所有p元素的第二个子元素
:nth-last-child(n) p:nth-last-child(2) 选择所有p元素倒数的第二个子元素
:nth-last-of-type(n) p:nth-last-of-type(2) 选择所有p元素倒数的第二个为p的子元素
:nth-of-type(n) p:nth-of-type(2) 选择所有p元素第二个为p的子元素
:only-of-type p:only-of-type 选择p元素在父元素中只有一个类型为p的元素
:only-child p:only-child 选择p元素在父元素中只有一个p的元素
:empty p:empty 选择所有没有子元素(任何内容)的p元素
:optional input:optional 选择没有"required"的元素属性
:required input:required 选择有“required”属性指定的元素属性
:read-only input:read-only 选择只读属性的元素属性
:read-write input:read-write 选择没有只读属性的元素属性
:root root 选择文档的根元素
:target #news:target 选择当前活动#news元素(点击URL包含锚的名字)
:link a:link 选择所有未访问链接
:visited a:visited 选择所有访问过的链接
:active a:active 选择正在活动链接
:hover a:hover 把鼠标放在链接上的状态
:focus input:focus 选择元素输入后具有焦点
:before p:before 在每个<p>元素之前插入内容
:after p:after 在每个<p>元素之后插入内容

案例11:自定义单选框

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <link rel="stylesheet" type="text/css" href="new_file.css"/>
  7. </head>
  8. <body>
  9. <!-- 自定义单选框 -->
  10. <div class="radio">
  11. <input type="radio" name="a"/>
  12. <div class="checked"></div>
  13. </div>
  14. <div class="radio">
  15. <input type="radio" name="a"/>
  16. <div class="checked"></div>
  17. </div>
  18. </body>
  19. </html>

css

  1. .radio{
  2. width: 50px;
  3. height: 50px;
  4. background-color: rgba(0,0,0,0.1);
  5. position: relative;
  6. display: inline-block;
  7. border-radius: 100px;
  8. box-shadow: 0 0 2px 1px rgba(255,0,0,0.5);
  9. }
  10. .radio input[type=radio]{
  11. width: 100%;
  12. height: 100%;
  13. margin: 0;
  14. opacity: 0;
  15. position: absolute;
  16. left: 0;
  17. top: 0;
  18. }
  19. .radio .checked{
  20. position: relative;
  21. }
  22. .radio .checked::before{
  23. content: "";
  24. position: absolute;
  25. width: 0;
  26. height: 0;
  27. border-right: 20px solid green;
  28. border-top: 20px solid transparent;
  29. right: 25px;
  30. top: 5px;
  31. display: none;
  32. }
  33. .radio .checked::after{
  34. content: "";
  35. position: absolute;
  36. width: 0;
  37. height: 0;
  38. border-left: 20px solid blue;
  39. border-bottom: 20px solid transparent;
  40. left: 25px;
  41. top: 25px;
  42. display: none;
  43. }
  44. .radio input[type=radio]:checked+.checked::before{
  45. display: block;
  46. }
  47. .radio input[type=radio]:checked+.checked::after{
  48. display: block;
  49. }

效果展示

案例12:提示框文字

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. #content{
  8. position: relative;
  9. width: 200px;
  10. }
  11. .cart{
  12. display: none;
  13. position: absolute;
  14. top: 0;
  15. left: 0;
  16. margin-top: -35px;
  17. padding: 5px 10px;
  18. height: 20px;
  19. background-color: black;
  20. border-radius: 5px;
  21. color: white;
  22. }
  23. .cart:after{
  24. content: "";
  25. border-width: 5px;
  26. border-style: solid;
  27. border-color: black transparent transparent transparent;
  28. position: absolute;
  29. bottom: -10px;
  30. left: 50%;
  31. margin-left: -5px;
  32. }
  33. #content:hover .cart{
  34. display: inline-block;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <br /><br /><br /><br />
  40. <div id="content">
  41. 请把鼠标移上来
  42. <div class="cart">
  43. 提示文字
  44. </div>
  45. </div>
  46. </body>
  47. </html>

效果展示

案例13:自定义列表

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <link rel="stylesheet" type="text/css" href="new_file.css"/>
  7. </head>
  8. <body>
  9. <ul>
  10. <li tip="提示1">自定义列表</li>
  11. <li tip="提示2">自定义列表</li>
  12. <li tip="提示3">自定义列表</li>
  13. <li tip="提示4">自定义列表</li>
  14. </ul>
  15. <ul>
  16. <li tip="提示1">自定义列表</li>
  17. <li tip="提示2">自定义列表</li>
  18. <li tip="提示3">自定义列表</li>
  19. <li tip="提示4">自定义列表</li>
  20. </ul>
  21. </body>
  22. </html>

css

  1. ul{
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. display: inline-block;
  6. /* 设置每个UL重新计数,默认起始值=0 */
  7. counter-reset: a 0;
  8. }
  9. ul li{
  10. position: relative;
  11. padding-left: 20px;
  12. /* 设置每个li自增 1 ,默认自增值=1 */
  13. counter-increment: a 1;
  14. }
  15. /* 自定义列表前面的图案 */
  16. ul li:before{
  17. /* 引用 li 中定义的自增变量 a 的值 */
  18. content: counter(a);
  19. width: 0;
  20. height: 0;
  21. position: absolute;
  22. border-left: 8px solid black;
  23. border-top: 8px solid transparent;
  24. border-bottom: 8px solid transparent;
  25. left: 0;
  26. top: 4px;
  27. line-height: 0px;
  28. }
  29. /* 自定义列表后面的鼠标悬停提示文字 */
  30. ul li:after{
  31. /* 引用 html标记上的 tip 属性值 */
  32. content: attr(tip);
  33. position: absolute;
  34. color: white;
  35. background-color: black;
  36. padding: 4px 8px;
  37. border-radius: 4px;
  38. font-size: 12px;
  39. display: none;
  40. }
  41. ul li:hover:after{
  42. display: block;
  43. left: 110%;
  44. white-space: nowrap;
  45. top: 0px;
  46. }

效果展示

属性选择器

选择器 描述
[attribute] 用于选取带有指定属性的元素。
[attribute=value] 用于选取带有指定属性和值的元素。
[attribute~=value] 用于选取属性值中包含指定词汇的元素。
[attribute l=value] 用于选取带有以指定值开头的属性值的元素,只能选择前缀,必须加:“-”。
[attribute^=value] 匹配属性值以指定值开头的每个元素。
[attribute$=value] 匹配属性值以指定值结尾的每个元素。
[attribute*=value] 匹配属性值中包含指定值的每个元素。

实例14:输入元素

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>后代选取器</title>
  6. <style type="text/css">
  7. form{
  8. border-radius: 5px;
  9. display: inline-block;
  10. padding: 5px;
  11. border: 1px solid rgba(0,0,0,0.3);
  12. box-shadow: 1px 3px 1px rgba(0,0,0,0.4),
  13. -1px 4px 1px rgba(0,0,0,0.3),
  14. 2px 5px 3px rgba(0,0,0,0.2),
  15. -2px 6px 3px rgba(0,0,0,0.1);
  16. }
  17. input {
  18. height: 30px;
  19. margin: 0;
  20. padding: 0px 10px;
  21. border: 1px solid rgba(0,0,0,0.4);
  22. border-radius: 5px;
  23. font-size: 18px;
  24. color: #DC143C;
  25. }
  26. input[type=button]{
  27. }
  28. input:focus{
  29. border: 1px solid rgba(255,0,0,1);
  30. outline: 0;
  31. }
  32. input:active{
  33. border: 1px solid rgba(0,0,255,1);
  34. outline: 0;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <form>
  40. <input type="text" placeholder="请输入用户名" />
  41. <input type="password" placeholder="请输入密码" />
  42. <input type="button" value="登陆" />
  43. </form>
  44. </body>
  45. </html>

效果展示

image-20210902173247813

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号