当前位置:   article > 正文

CSS3常用伪类与伪类元素详解_input的css 伪lei属性

input的css 伪lei属性

1.伪类选择器---根据元素的状态指定样式

1.1.常规选择器 :hover

  1. <style>
  2. div {
  3. margin: 100px auto;
  4. width: 200px;
  5. height: 200px;
  6. background-color: antiquewhite;
  7. }
  8. div:hover {
  9. /* 当鼠标在div元素上时颜色改变 */
  10. background-color: aqua;
  11. }
  12. </style>

1.2. 激活状态 :active

鼠标按下时触发.抬起时还原

  1. <style>
  2. button {
  3. width: 100px;
  4. height: 40px;
  5. background-color: antiquewhite;
  6. }
  7. button:active {
  8. /* 当点击button元素上时颜色改变,鼠标抬起时,样式还原*/
  9. background-color: aqua;
  10. }
  11. </style>

1.3. 聚焦状态 :focus

一般用于输入框获取焦点时触发

  1. <style>
  2. inputn {
  3. width: 100px;
  4. height: 40px;
  5. }
  6. input:focus {
  7. /* 当输入框切换为输入状态时,输入框底色改变 */
  8. background-color: aqua;
  9. }
  10. </style>

1.4.选择同类元素的第一个元素 :first-child

  1. //CSS
  2. div:first-child {
  3. color: aqua;
  4. }
  5. //HTML
  6. <div>A</div>
  7. <div>B</div>
  8. <div>C</div>

1.5.选择同类元素的最后一个元素 :last-child

  1. //CSS
  2. div:last-child {
  3. color: aqua;
  4. }
  5. //HTML
  6. <div>A</div>
  7. <div>B</div>
  8. <div>C</div>

1.6.选择同类元素的第n个元素 :nth-child(n)

  1. //CSS
  2. div:nth-child(2) {
  3. color: aqua;
  4. }
  5. //HTML
  6. <div>A</div>
  7. <div>B</div>
  8. <div>C</div>

 

2.伪元素

2.1在元素前添加前缀元素 ::before

  1. //CSS
  2. div {
  3. margin: 100px auto;
  4. width: 150px;
  5. height: 40px;
  6. text-align: center;
  7. line-height: 40px;
  8. background-color: orange;
  9. }
  10. div::before {
  11. //content是必须的,不然没有作用
  12. content: '2';
  13. }
  14. //HTML
  15. <div>=1+1</div>

 2.2在元素后添加后缀元素 ::after

  1. //CSS
  2. div {
  3. margin: 100px auto;
  4. width: 150px;
  5. height: 40px;
  6. text-align: center;
  7. line-height: 40px;
  8. background-color: orange;
  9. }
  10. div::after {
  11. //content是必须的,不然没有作用
  12. content: '2';
  13. }
  14. //HTML
  15. <div>1+1=</div>

2.3首个字符 ::first-letter

  1. //CSS
  2. p::first-letter {
  3. font-size: 50px;
  4. }
  5. //HTML
  6. <p>
  7. abc <br>
  8. 123 <br>
  9. xyz
  10. </p>

 

 伪类选择器和伪类元素比较常用的就在上面,还有一些不常用的,大家在学习中慢慢积累.

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/707636
推荐阅读
相关标签
  

闽ICP备14008679号