css伪类伪元素小结

区分

伪元素在CSS3之前就已经存在,只是没有伪元素的说法,都是归纳为伪类,所有很多人分不清楚伪类和伪元素。比如常用的:before:after,它们是伪类还是伪元素?其实在CSS3之前被称为伪类,直到CSS3才正式区分出来叫伪元素

那如何区分伪元素和伪类,记住两点:

1. 伪类表示被选择元素的某种状态,例如:hover

2. 伪元素表示的是被选择元素的某个部分,这个部分看起来像一个独立的元素,但是是"假元素",只存在于css中,所以叫"伪"的元素,例如:before:after

核心区别在于,是否创造了“新的元素”

3、常见的一些应用

1)包括before、after、first-letter、first-line ,分别表示元素内容的前面、元素内容的后面以及第一个文字和第一行文字

<style>
  .wrap{
    height: 100px;
    width: 500px;
    border: 1px rgb(5, 248, 114) solid;
    padding: 10px;
    display: flex;
    flex-direction: column;
  }
   .box1:before{
    content: "我是前面的before";
    color: rgb(177, 81, 13);
  }
  .box1:after{
      content: "我是后面的after";
      color: rgb(243, 29, 72);
  }
  .box1{
    background: rgb(221, 132, 214);
    background-clip: content-box;
  }
  .box2:first-letter{
    font-size: 3em;
    font-weight: bold;
  }
  .box2:first-line{
    color: rgb(101, 245, 6);
}

</style>
</head>
  <body>
    <div class="wrap">
      <div class="box1">
        周末了,记得学习啊
      </div>
      <div class="box2">
        复习一下css3
      </div>
    </div>
  </body>
<script>
</script>
</html>

展现效果:

2) css3之后新增的伪元素

附上源码

<style>
  .wrap{
    height: auto;
    width: 500px;
    border: 1px rgb(5, 248, 114) solid;
    padding: 10px;
    display: flex;
    flex-direction: column;
  }
   .box1:before{
    content: "我是前面的before";
    color: rgb(177, 81, 13);
  }
  .box1:after{
      content: "我是后面的after";
      color: rgb(243, 29, 72);
  }
  .box1{
    background: rgb(221, 132, 214);
    background-clip: content-box;
  }
  .box2:first-letter{
    font-size: 3em;
    font-weight: bold;
  }
  .box2:first-line{
    color: rgb(101, 245, 6);
  }
  .box3::selection{
    color:rgb(177, 81, 13);
  }
  .box4 input::placeholder{
    color: #f00;
  }
  .box5{
    display: flex;
    flex-direction: row;
  }

  a{
    text-decoration: none;
    padding: 10px;
    border-radius: 5px;	
  }
  a.link:link{
    background-color: rgb(203, 218, 151);
  }
  a.hover:visited{
  background-color: rgb(221, 51, 142);
  }
  a.active:active{
    font-size: 2em;
  }
  a.focus:focus{
    background-color: rgb(247, 142, 4);
  }

</style>
</head>
  <body>
    <div class="wrap">
      <div class="box1">
        周末了,记得学习啊
      </div>
      <div class="box2">
        复习一下css3
      </div>
      <div class="box3">
        这个是选中的内容的部分
      </div>
      <div class="box4">
        <input type="text" placeholder="北京今天迎来了初雪啊">
      </div>
      <div class="box5">
        <a class="link" href="#">正常链接</a>
        <a class="visited" href="#">点击过后</a>
        <a class="active" href="#">被激活时</a>
        <a class="focus" href="#">获得焦点时</a>
      </div>
    </div>

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值