伪对象选择器

和伪类选择器类似

placeholder 是在表单中提示输入内容的占位符

input::placeholder{}修改Input标签中的默认提示文字 只会修改提示文字的样式 部分CSS属性可能不能使用

selection 指在浏览器中被选定的部分 比如用户要复制一段文字时先进行选定文字,这时候可以对已经选定的文字来进行修改

p:selection{}选定的是在网页中被用户选择的P标签的内容 可以是动态变化的

浏览器前缀的问题

由于以上两个属性在很多浏览器中都还只是在测试阶段,所以单纯的输入属性值可能样式是无法显示的,这时候为了获得浏览器的支持 需要在选择器之前写浏览器前缀

  • 不同的浏览器有不同的前缀
    • -webkit- 谷歌
    • -ms- IE
    • -moz- 火狐
    • -o- opera
1
2
3
 input::-webkit-input-placeholder {
        color:orange;
      }

before和after

before和after分别是设置在对象的前后发生的内容 我理解成是分别在元素的内部最前面和最后面生成的内容,

  • 必须有属性content属性才能工作,content可以设置成空的字符串
  • 默认是行内块元素可以在浏览器的elements选项里看到生成的元素
  • div的样式会影响before和after的样式 div::before{content:"before the div";}在div内容最前面before内容是before the div。

用before和after做菜刀和滑板

里面用了border-radius属性 设置边角的圆角大小 可以用百分比 或者px

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .knife {
      width: 200px;
      height: 50px;
      margin: 0 auto;
      background-color: lightgray;
      position: relative;
      border-bottom-left-radius: 50px;
      border-bottom-right-radius: 5px;
    }
    
    .knife::after {
      content: '';
      /**/
      position: absolute;
      right: -100px;
      width: 100px;
      height: 15px;
      background-color: firebrick;
      color: white;
      font-size: 10px;
      text-align: center;
    }

    
    .skate-board {
      width: 200px;
      height: 10px;
      margin: 100px auto;
      background-color: yellowgreen;
      position: relative;
    }
    
    /* 前轮 */
    .skate-board::before {
      /* 必须 */
      content: '';

      /* 定位之后 变为块了 才能够设置大小 */
      position: absolute;

      width: 30px;
      height: 30px;
      top: 10px;
      left: 0px;
      background-color: black;

      /*  */
      /*border-radius: 10px;*/
      /* 对于 宽高一样的元素 */
      border-radius: 50%;
    }
    
    /* 后轮 */
    .skate-board::after {
      /* 必须 */
      content: '';

      /* 定位之后 变为块了 才能够设置大小 */
      position: absolute;

      width: 30px;
      height: 30px;
      top: 10px;
      right: 0px;
      background-color: black;
      border-radius: 50%;
      
    }
  </style>
</head>

<body>
  <div class='knife'></div>
  <div class='skate-board'></div>
</body>

</html>