博客列表 >CSS中的伪类选择器,box-sizing属性,常用单位,媒体查询

CSS中的伪类选择器,box-sizing属性,常用单位,媒体查询

书伟php由0到1
书伟php由0到1原创
2021年10月08日 12:49:25422浏览

伪类选择器

  • 示例代码1:

    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>伪类</title>
    8. </head>
    9. <body>
    10. <ul class="list">
    11. <li>item1</li>
    12. <li>item2</li>
    13. <li>item3</li>
    14. <li>item4</li>
    15. <p>item1</p>
    16. <p>item2</p>
    17. <p>item3</p>
    18. <p>item4</p>
    19. <p>item5</p>
    20. <ul>
    21. <li>item3-1</li>
    22. <li>item3-2</li>
    23. <li>item3-3</li>
    24. <li>item3-4</li>
    25. <li>item3-5</li>
    26. <li>item3-6</li>
    27. </ul>
    28. <li>item5</li>
    29. <li>item6</li>
    30. </ul>
    31. <style>
    32. /* :nth-of-type()分组匹配,在匹配之前将元素根据类型进行分组后再匹配 */
    33. /* 通过空格符号,选择当前父元素中的第2个子元素li,同时孙子元素的第2个也起 */
    34. .list :nth-of-type(2) {
    35. background-color: red;
    36. }
    37. /* 通过>符号,选择当前父元素中的第3个子元素li,仅第3个子元素起作用 */
    38. .list > :nth-of-type(3) {
    39. background-color: blue;
    40. }
    41. /* 通过>符号,选择当前父元素中的第4个子元素p,仅第4个子元素起作用 */
    42. .list > p:nth-of-type(4) {
    43. background-color: brown;
    44. }
    45. /* 通过*符号,选择当前父元素中的第5个子元素,所有第5个子元素起作用 */
    46. .list > *:nth-of-type(5) {
    47. background-color: chartreuse;
    48. }
    49. /* 去掉集合中的第6个li元素,not就是在一个集合中去掉某一些元素 */
    50. .list > :nth-of-type(6) :not(li:nth-of-type(6)) {
    51. background-color: cyan;
    52. }
    53. /* 选中第一个 */
    54. .list > :nth-of-type(1) {
    55. background-color: cyan;
    56. }
    57. .list > :first-of-type {
    58. background-color: darkgoldenrod;
    59. }
    60. /* 选中最后一个 */
    61. .list > :last-of-type {
    62. background-color: darkmagenta;
    63. }
    64. /* 倒数选择第2个 */
    65. .list > li:nth-last-of-type(2) {
    66. background-color: darkslateblue;
    67. }
    68. </style>
    69. </body>
    70. </html>
  • 输出效果1

  • 示例代码2

    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>伪类选择器的参数</title>
    8. </head>
    9. <body>
    10. <ul class="list">
    11. <li>item1</li>
    12. <li>item2</li>
    13. <li>item3</li>
    14. <li>item4</li>
    15. <li>item5</li>
    16. <li>item6</li>
    17. </ul>
    18. <style>
    19. /* :nth-of-type(参数) */
    20. /* 参数公式=an+b a,n,b的取值为[0,1,2,3……] */
    21. /* a:系数 n:计数器 b:偏移量 */
    22. /* 元素的有效编号必须从1开始,n是从0开始,b是从0开始 */
    23. /* :nth-of-type(3)===> : nth-of-type(0n+3) */
    24. .list :nth-of-type(0n + 3) {
    25. background-color: red;
    26. }
    27. /* 1n+b: 选择从第几个开始 */
    28. .list :nth-of-type(1n + 2) {
    29. background-color: salmon;
    30. }
    31. /* even:偶数 */
    32. .list :nth-of-type(2n) {
    33. background-color: slateblue;
    34. }
    35. .list :nth-of-type(even) {
    36. background-color: slateblue;
    37. }
    38. /* 奇数: */
    39. .list :nth-of-type(2n + 1) {
    40. background-color: teal;
    41. }
    42. .list :nth-of-type(odd) {
    43. background-color: teal;
    44. }
    45. /* 选择前三个 */
    46. .list > :nth-of-type(-n + 3) {
    47. background-color: yellow;
    48. }
    49. /* 选择后三个 */
    50. .list > :nth-last-of-type(-n + 3) {
    51. background-color: blue;
    52. }
    53. </style>
    54. </body>
    55. </html>
  • 输出效果2:

box-sizing属性

  • box-sizing:来指定内容区的边界在哪里
    box-sizing: content-box
  • 宽度和高度分别应用到元素的内容框,在宽度和高度之外绘制元素的内边距和边框。
    box-sizing: border-box
  • 通过收缩原来内容区的大小,来保证盒子在页面中的空间不变
  • 示例代码
    1. <div class="box"></div>
    2. <style>
    3. .box {
    4. width: 200px;
    5. height: 200px;
    6. border: 10px solid currentColor;
    7. background-color: royalblue;
    8. border: 10px dashed currentColor;
    9. background-clip: content-box;
    10. /* 内边距 位于边框与内容区之间的区域,呼吸区*/
    11. padding: 20px;
    12. /* box-sizing来指定内容区的边界在哪里 */
    13. /* 默认height、width就是内容区大小 */
    14. /* content-box:在宽度和高度之外绘制元素的内边距和边框 */
    15. box-sizing: content-box;
    16. /* 通过收缩原来内容区的大小,来保证盒子在页面中的空间不变 */
    17. box-sizing: border-box;
    18. height: 200px;
    19. width: 200px;
    20. /* 外边距 */
    21. margin: 20px;
    22. /* 盒模型的四个方向是可以独立设置的 */
    23. /* padding,border,margin */
    24. /* 四个方向是用时钟来表示的,上,右,下,左。 */
    25. }
  • 输出效果

常用单位

  • css中常用单位分为绝对单位,相对单位。
  • 绝对定位 :px即像素,与设置相关,一英寸有96px
  • 相对定位:
    1、em ,rem ,与字号相关,其中rem主要用于布局。
    2、vw ,vh , 可视窗口大小

媒体查询移动优先与PC优先的区别使用

  • 通过@media 查询设置,可以针对不同的媒体类型定义不同的样式。
  • 媒体查询有移动优先和PC优先两种方式 根据页面的宽度确定,常用的是 min-widthmax-width

  • 移动优先是由小屏到大屏,min-width可以理解为“大于等于多少范围”

  • PC优先是由大屏到小屏,max-width可以理解为“小于等于多少范围”

使用PC优先的方式,注意最大尺寸的设置min-width,以免超过最大尺寸时,自动设置为默认样式。

  • 示例代码:

    1. <button class="btn small">小按钮</button>
    2. <button class="btn middle">中按钮</button>
    3. <button class="btn large">大按钮</button>
    4. <style>
    5. html {
    6. font-size: 10px;
    7. }
    8. .btn {
    9. background-color: cyan;
    10. color: red;
    11. border: none;
    12. outline: none;
    13. }
    14. /* 悬浮鼠标光标小手 */
    15. .btn:hover {
    16. cursor: pointer;
    17. opacity: 0.9;
    18. transition: 0.3s;
    19. }
    20. /* 小按钮 */
    21. .btn.small {
    22. font-size: 1.2rem;
    23. padding: 0.5rem 0.9rem;
    24. }
    25. /* 中按钮 */
    26. .btn.middle {
    27. font-size: 1.6rem;
    28. padding: 0.4rem 0.8rem;
    29. }
    30. /* 大按钮 */
    31. .btn.large {
    32. font-size: 1.8rem;
    33. padding: 0.4rem 0.8rem;
    34. }
    35. /* 移动优先,从最小的屏幕开始进行媒体查询 */
    36. @media (min-width: 480px) {
    37. html {
    38. font-size: 12px;
    39. }
    40. }
    41. @media (min-width: 640px) {
    42. html {
    43. font-size: 14px;
    44. }
    45. }
    46. @media (min-width: 720px) {
    47. html {
    48. font-size: 16px;
    49. }
    50. }
    51. /* 桌面优先/PC优先, 由大屏到小屏 */
    52. @media (max-width: 720px) {
    53. html {
    54. font-size: 16px;
    55. }
    56. }
    57. @media (max-width: 640px) {
    58. html {
    59. font-size: 14px;
    60. }
    61. }
    62. @media (max-width: 480px) {
    63. html {
    64. font-size: 12px;
    65. }
    66. }
    67. @media (min-width: 720px) {
    68. html {
    69. font-size: 16px;
    70. }
    71. }
    72. </style>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议