第八天C3选择器 定位 - 图1

css3选择器

  • nth-child:选中所有儿子元素中的第几个
  • nth-of-type:选中儿子元素中特定类型中的第几个
    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. /*
    10. 指的是所有子元素中的第几个
    11. 选中第几个元素:跟普通数字 */
    12. .main>li:nth-child(2){
    13. background: darkcyan;
    14. }
    15. /* 跟奇偶数
    16. 奇数:odd
    17. 偶数:even */
    18. .main>li:nth-child(odd){
    19. background: darkgreen;
    20. }
    21. .main>li:nth-child(even){
    22. background: darkmagenta;
    23. }
    24. /* 跟自己设计的:3n 3n+1 3n+2 */
    25. .main>li:nth-child(3n){
    26. background: yellow;
    27. }
    28. .main>li:nth-child(3n+1){
    29. background: red;
    30. }
    31. /* 快速选中第一个 */
    32. .main>li:first-child{
    33. background: darkseagreen;
    34. }
    35. /* 快速选中最后一个 */
    36. .main>li:last-child{
    37. background: darkseagreen;
    38. }
    39. </style>
    40. </head>
    41. <body>
    42. <ul class="main">
    43. <li>1</li>
    44. <li>2</li>
    45. <li>3</li>
    46. <li>4</li>
    47. <li>5</li>
    48. <li>6</li>
    49. <li>7</li>
    50. <li>8</li>
    51. </ul>
    52. </body>
    53. </html>
<!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>
        /* 
         特定类型中的第几个
        */
        /* p标签类型中的第一个 */
        .main>p:nth-of-type(1){
            background: red;
        }
        /*p标签类型中的奇数项  */
        .main>p:nth-of-type(odd){
            background: red;
        }
        /*p标签类型中的偶数项  */
        .main>p:nth-of-type(even){
            background: yellow;
        }
        /* 快速选中p标签第一个 */
        .main>p:first-of-type{
            background:green;
        }
        /* 快速选中p标签最后一个 */
        .main>p:last-of-type{
            background: gray;
        }
    </style>
</head>
<body>
    <div class="main">
        <p>p1</p>
        <h3>h3</h3>
        <p>p2</p>
        <p>p3</p>
        <p>p1</p>
        <h3>h3</h3>
        <p>p2</p>
        <p>p3</p>
    </div>
</body>
</html>

定位:position

名称 绝对定位 相对定位 固定定位 静态定位
代码 position:absolute position:relative position:fixed position:static







特色
是否占位置 否 是
是否脱离文档流 是 否
层级 高于普通文档流


参
照
物
别人:祖先级元素
多个参照物:就近原则
没有参照物:body
参照物本身已经有定位,就不用再写定位
自身 整个窗口
宽
度
用百分比表示时,指的是参照物的百分比
行内元素 写宽高起作用
块级元素 由自身内容决定
  • 静态定位:position:static
  • 相对定位:position:relative
    • 1.不脱离普通文档流
    • 2.相对定位的元素层级高于普通文档流
    • 3.给绝对定位的元素做参照物
    • 4.相对自身原来的位置
  • 绝对定位: position: absolute
    • 1.参照物是别人,必须是祖先级元素
    • 2.如果有多个参照物,浏览器按照就近原则“就近原则”.如果都没有设置参照物,参照物就是body
    • 3.在设置参照物的时候,如果参照物本身已经是绝对定位或固定定位,就不用再写position:relative;
    • 4.绝对定位的元素不占位置,脱离普通文档流;
    • 5.层级高于普通文档流
    • 6.给绝对定位的元素,宽度用百分比表示的时候,指的是参照物的百分比
    • 7.行内元素,写宽高本来不起作用,一旦设置了定位,写宽高也可以起作用
    • 8.块级元素,不写宽度,会自动撑满父级,但是一旦给它设置了定位,它的宽度就是由自身内容决定的
  • 固定定位:position: fixed
    • 固定定位的参照物是相对于整个窗口的

      对于定位的元素,可以通过z-index 来控制它的层级关系,z-index这个值越大,就越靠上

面试题:如何让一个元素在整个屏幕或者是一个盒子中水平垂直居中

  • 固定定位或者绝对定位;

    • left:50%;top:50%;
    • margin-left:负的盒子宽的一半;margin-top:负的盒子高度的一半;
      <style>
      .box{
         width:200px;
         height:200px;
         background:green;
         position: absolute;
         left:50%;
         top:50%;
         margin-left:-100px;
         margin-top:-100px;
      }
      </style>
      

      精灵图

  • 原理:通过改变background-position

    在写精灵图的时候,background-position的x轴,y轴的值一定的负的

变成圆

  • border-radius: 50%
    .box{
          width:300px;
          height:300px;
          background:gold;
          border-radius: 50%;
      }