经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS3 » 查看文章
CSS3选择器新增问题的实现_css3_CSS
来源:jb51  时间:2021/1/25 11:27:58  对本文有异议

基本选择器扩展

1.子元素选择器

:tomato: #wrap > .inner {color: pink;} 也可称为直接后代选择器,此类选择器只能匹配到直接后代,不能匹配到深层次的后代元素 总结:>作用于元素的第一代后代,空格作用于元素的所有后代

2. 相邻兄弟选择器

:tomato: #wrap #first + .inner {color: #f00;}它只会匹配紧跟着的兄弟元素

3. 通用兄弟选择器

:tomato: #wrap #first ~ div { border: 1px solid;}它会匹配第二个元素,条件是它必须跟(不一定是紧跟)在第一个元素之后,且他们都有一个共同的父元素所有的兄弟元素

4. 选择器分组

:tomato: h1,h2,h3{color: pink;} 此处的逗号我们称之为结合符

属性选择器

1. 子串值属性选择器

:tomato: [attr|=val] : 选择attr属性的值是val(包括val)或以val-开头的元素。

:tomato: [attr^=val] : 选择attr属性的值以val开头(包括val)的元素。

:tomato: [attr$=val] : 选择attr属性的值以val结尾(包括val)的元素。

:tomato: [attr*=val] : 选择attr属性的值中包含字符串val的元素 少数浏览器支持子串选择元素

2. 存在和值属性选择器

:tomato: [attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何。[attr=val]:该选择器仅选择 attr 属性被赋值为 val 的所有元素。

:tomato: [attr~=val]:表示带有以 attr 命名的属性的元素,并且该属性是一个以空格作为分隔的值列表,其中至少一个值为val。 比如位于被空格分隔的多个类(class)中的一个类。比如name="atguigu_llc atguigu"

:tomato: [name =val]:该选择器仅选择 name 属性被赋值为 val 的所有元素。

伪类与伪元素选择器

1. 链接伪类

:tomato: :link 表示作为超链接,并指向一个未访问的地址的所有锚

:tomato: :visited 表示作为超链接,并指向一个已访问的地址的所有锚

:tomato: :target 代表一个特殊的元素,它的id是URI的片段标识符

:exclamation: 注意:link,:visited,:target是作用于链接元素的!前两者只能在a标签上起作用

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. div{
  6. width: 300px;
  7. height: 200px;
  8. line-height: 200px;
  9. background: palegreen;
  10. color: hotpink;
  11. text-align: center;
  12. display: none;
  13. }
  14. a:visited{
  15. color:orange;
  16. }
  17. :target{
  18. display: block;
  19. }

2. 动态伪类

:tomato: :hover表示悬浮到元素上

:tomato: :active表示匹配被用户激活的元素(点击按住时)

:tomato:由于a标签的:link和:visited可以覆盖了所有a标签的状态,所以当:link,:visited,:hover,:active同时出现在a标签身上时 :link和:visited不能放在最后!!!

:tomato: 隐私与:visited选择器只有下列的属性才能被应用到已访问链接:color、background-color、border-color

:exclamation: 注意:hover,:active基本可以作用于所有的元素!

  1. <style type="text/css">
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. }
  6. a{
  7. text-decoration: none;
  8. color: black;
  9. display: block;
  10. }
  11. a:hover{
  12. font-size:24px;
  13. /*color: red;*/
  14. }
  15. a:link{
  16. font-size:48px;
  17. /*color: pink;*/
  18. }
  19. a:visited{
  20. /*font-size:96px;*/
  21. /*color: deeppink;*/
  22. }
  23. </style>

3. 表单相关伪类

①:disable 匹配被禁用的表单

②:checked 匹配被选中的表单

③:focus 匹配获焦的表单

④:enabled匹配可编辑的表单

实操练习1

  1. <head>
  2. <meta charset="UTF-8">
  3. <title></title>
  4. <style type="text/css">
  5. input:enabled{
  6. background: skyblue;
  7. }
  8. input:disabled{
  9. background: deeppink;
  10. }
  11. input:checked{
  12. width: 200px;
  13. height: 200px;
  14. }
  15. input:focus{
  16. background: pink;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <input type="text" />
  22. <input type="text" disabled="disabled" />
  23. <input type="checkbox" />
  24. <input type="text" />
  25. </body>

实操练习2 自定义单选按钮

  1. <head>
  2. <meta charset="UTF-8">
  3. <title></title>
  4. <style type="text/css">
  5. *{
  6. margin: 0;
  7. padding:0;
  8. }
  9. label{
  10. position: relative;
  11. float:left; /*转换为块级元素撑开大小*/
  12. width:100px;
  13. height:100px;
  14. border:2px solid;
  15. border-radius: 50%;
  16. overflow:hidden;
  17. }
  18. label > span{
  19. position: absolute;
  20. left:0;
  21. top:0;
  22. bottom:0;
  23. right:0;
  24. }
  25. input{
  26. position: absolute;
  27. left:-50px;
  28. top:-50px;
  29. }
  30. input:checked + span{
  31. background:pink;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <label>
  37. <input type="radio" name="rongtuowulian" />
  38. <span></span>
  39. </label>
  40. <label>
  41. <input type="radio" name="rongtuowulian" />
  42. <span></span>
  43. </label>
  44. <label>
  45. <input type="radio" name="rongtuowulian" />
  46. <span></span>
  47. </label>
  48. </body>

4. 伪元素

:tomato: 概念:伪元素表示页面中一些特殊的并不真实存在的元素(特殊的位置)

:tomato: 语法使用 ::开头

:tomato: 类别:①::first-letter ②::first-line ③::selection ④::before ⑤::after 注意:④和⑤必须结合content属性来使用

:tomato: 代码示例:

  1. <head>
  2. <meta charset="utf-8">
  3. <meta name="viewport" content="width=device-width,initial-scale=1.0" />
  4. <meta http-equiv="X-UA-Compatible"content="ie=edge" />
  5. <title></title>
  6. <style type="text/css">
  7. p:first-letter{
  8. color: #008000;
  9. font-size: 2.5rem;
  10. }
  11. p:first-line{
  12. color: aqua;
  13. }
  14. p::selection{
  15. color:red;
  16. }
  17. p:before{
  18. content: ''I will love you forever';
  19. color:blue;
  20. }
  21. p:after{
  22. content: 'sure are you';
  23. color:green;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div>Hello are you okay</div>
  29. <p> 我还是一个段落 我是一个很多很多解放碑还不错保持经济刺激
  30. 哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈
  31. </p>
  32. </body>

5. 结构性伪类(重点)

:tomato: index的值从1开始计数!!!!index可以为变量n(只能是n)index可以为even odd

:tomato: #wrap ele:nth-child(index)表示匹配#wrap中第index的子元素,这些伪类都是根据所有的子元素进行排序,这个子元素必须是ele

:tomato: #wrap ele:nth-of-type(index)表示匹配#wrap中第index的ele子元素

除此之外:nth-child和:nth-of-type有一个很重要的区别!!nth-of-type以元素为中心,在同一类型中排序,nth-child (相对于:first-child:last-child 或者 :nth-child(1):nth-last-child(1))

/* even表示偶数

odd表示奇数

first-of-type:在p类型中排

first-child:所有元素排

*/

:tomato::nth-child(index)系列

:first-child

:last-child

:nth-last-child(index)

:tomato::nth-of-type(index)系列

  1. :first-of-type
  2. :last-of-type
  3. :nth-last-type(index)
  4. :only-of-type(相对于:first-of-type:last-of-type
  5. 或者 :nth-of-type(1):nth-last-of-type(1))
  6. :not
  7. :empty(内容必须是空的,有空格都不行,有attr没关系)

代码实例

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. #wrap li:nth-of-type(1){
  6. color: pink;
  7. }
  8. p:only-of-type{
  9. border: 1px solid;
  10. }

6. 伪元素选择器

::after

::before

::firstLetter

::firstLine

::selection

  1. #wrap::before{
  2. content:"";
  3. display:block;
  4. width:200px;
  5. height:200px;
  6. background: #00FFFF;
  7. }
  8. #wrap::after{
  9. content:"";
  10. display:block;
  11. width:200px;
  12. height:200px;
  13. background: #0000FF;
  14. }

到此这篇关于CSS3选择器新增问题的实现的文章就介绍到这了,更多相关CSS3选择器新增内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号