欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CSS媒体查询,模态框,Flex常用属性

程序员文章站 2022-03-27 08:27:26
...

媒体查询

响应式布局:媒体查询。
主要是查询设备:屏幕,打印机类。
布局一般是宽度受限,高度不受限;也可以高度和宽度都受限,叫单屏布局;不可用高度和宽度都不受限。
实例说明媒体查询

  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. <!-- 创建3个btn实例,第一个类叫基本样式类,第二个叫修饰类-->
  11. <button class="btn small">按钮1</button>
  12. <button class="btn middle">按钮2</button
  13. ><button class="btn large">按钮3</button>
  14. <!-- 创建样式 -->
  15. <style>
  16. /* 初始化box */
  17. * {
  18. margin: 0;
  19. padding: 0;
  20. box-sizing: border-box;
  21. }
  22. /* 创建html的font-size大小 */
  23. html {
  24. font-size: 10px;
  25. }
  26. /* 创建按钮基本样式 */
  27. .btn {
  28. background-color: seagreen;
  29. color: aliceblue;
  30. border: none;
  31. outline: none;
  32. margin: 3px;
  33. }
  34. /* 创建鼠标悬停效果 */
  35. .btn:hover {
  36. cursor: pointer; /*指针设置为小手*/
  37. background-color: orangered;
  38. opacity: 0.8; /*透明度*/
  39. transition: 0.2s; /*过渡效果*/
  40. padding: 0.4rem 0.8rem; /*设置内边距*/
  41. }
  42. /* 分别设置3个btn的fontsize,用相对单位rem表示,只要动态调整rem大小就可以实现动态调整按钮大小的功能 */
  43. .btn.small {
  44. font-size: 1.2rem;
  45. }
  46. .btn.middle {
  47. font-size: 1.6rem;
  48. }
  49. .btn.large {
  50. font-size: 1.8rem;
  51. }
  52. /* 媒体查询 ,移动优先,从最小屏幕开始匹配,已ipone11为例,iphone11的屏幕大小为375*812px */
  53. /* 查询当屏幕小于375px时 */
  54. /*此处min-width写成了min-width,检查了好久没发现,以后还要细心一些*/
  55. @media (max-width: 374px) {
  56. html {
  57. font-size: 12px;
  58. }
  59. }
  60. @media (min-width: 375px) and (max-width: 414px) {
  61. html {
  62. font-size: 14px;
  63. }
  64. }
  65. @media (min-width: 415px) {
  66. html {
  67. font-size: 16px;
  68. }
  69. }
  70. /* PC优先,从最大屏幕开始适配 */
  71. @media (min-width: 600px) {
  72. html {
  73. font-size: 20px;
  74. }
  75. }
  76. @media (min-width: 501px) and(max-width:599px) {
  77. html {
  78. font-size: 16px;
  79. }
  80. }
  81. @media (max-width: 500px) {
  82. html {
  83. font-size: 10px;
  84. }
  85. }
  86. </style>
  87. </body>
  88. </html>

CSS媒体查询,模态框,Flex常用属性
CSS媒体查询,模态框,Flex常用属性

模态框

  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. <header>
  11. <h2 class="title">模态框实例演示</h2>
  12. <button onclick="document.querySelector('.modal').style.display='block'">
  13. 登录
  14. </button>
  15. </header>
  16. <!-- 模态框 -->
  17. <div class="modal">
  18. <!-- 遮罩背景 -->
  19. <div
  20. class="modal_bg"
  21. onclick="this.parentNode.style.display='none'"
  22. ></div>
  23. <!-- 登录表单 -->
  24. <form action="" class="modal_form">
  25. <fieldset style="display: grid; gap: 1em">
  26. <legend>用户登录</legend>
  27. <p>
  28. <input type="email" name="email" placeholder="username@email.com" />
  29. </p>
  30. <p>
  31. <input type="passwd" name="passwd" placeholder="******" />
  32. </p>
  33. <p>
  34. <button onclick="">提交</button>
  35. </p>
  36. </form>
  37. </fieldset>
  38. </div>
  39. </body>
  40. <style>
  41. /* 初始化 */
  42. * {
  43. padding: 0;
  44. margin: 0;
  45. box-sizing: border-box;
  46. }
  47. /* 设置header样式 */
  48. header {
  49. background-color: seagreen;
  50. color: seashell;
  51. display: flex;
  52. padding: 0.2em 1em;
  53. }
  54. /* 设置header标题样式 */
  55. header .title {
  56. font-style: italic;
  57. font-weight: lighter;
  58. color: seashell;
  59. letter-spacing: 2px;
  60. text-shadow: 3px 3px 3px #555; /* 文本阴影 */
  61. }
  62. header button {
  63. margin-left: auto;
  64. border: none;
  65. outline: none;
  66. background-color: white;
  67. width: 5rem;
  68. padding: auto;
  69. border-radius: 0.4rem; /* 设置按钮边角圆滑 */
  70. }
  71. header button:hover {
  72. background-color: tomato;
  73. color: white;
  74. cursor: pointer;
  75. box-shadow: 2px 3px 5px #555;
  76. transition: 0.3s; /* 设置渐变 */
  77. }
  78. /* 打开首页,点击登录前隐藏表单 */
  79. .modal {
  80. display: none;
  81. }
  82. /* 设置模态框表单样式 */
  83. .modal .modal_form fieldset {
  84. background-color: lightgreen;
  85. border: none;
  86. padding: 2em;
  87. box-shadow: 0 0 5px #555;
  88. width: 20rem;
  89. }
  90. .modal .modal_form fieldset legend {
  91. background-color: lightcyan;
  92. border: none;
  93. padding: 2em;
  94. box-shadow: 0 0 5px #888;
  95. height: 0.5em;
  96. width: 10em;
  97. }
  98. /* 模态框定位 */
  99. .modal .modal_form {
  100. position: fixed;
  101. top: 10em;
  102. left: 20em;
  103. right: 20em;
  104. }
  105. /* 半透明遮罩定位 */
  106. .modal .modal_bg {
  107. position: fixed;
  108. top: 0;
  109. left: 0;
  110. right: 0;
  111. bottom: 0;
  112. background-color: rgba(0, 0, 0, 0.5); /* 四个参数分别是红、绿、蓝、透明度 */
  113. }
  114. </style>
  115. </html>

CSS媒体查询,模态框,Flex常用属性

flex布局常用属性

flex布局常用的三个属性

  1. flex-flow:设置主轴方向row/column,设置是否换行wrap/nowrap
  2. plac-content:设置项目在主轴的排列及空间分配,值有:start,end,center,space-between(两端对齐),space-around(分散对齐),space-evenly(平均对齐)。
  3. plac-item:项目在文档交叉轴的对齐方式,值有:start,end,center,stretch(拉伸对齐)。

    1. <!DOCTYPE html>
    2. <html lang="zh-CN">
    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>flex布局术语,容器,项目</title>
    8. </head>
    9. <body>
    10. <div class="container">
    11. <div class="item">item1</div>
    12. <div class="item">item2</div>
    13. <div class="item">item3</div>
    14. </div>
    15. <style>
    16. * {
    17. margin: 0;
    18. padding: 0;
    19. box-sizing: border-box;
    20. }
    21. .container {
    22. /* flex容器 */
    23. display: flex;
    24. height: 20em;
    25. border: 1px solid #000;
    26. }
    27. .container > .item {
    28. /* flex容器中的"子元素" 则成为" 弹性项目/flex项目" */
    29. width: 10em;
    30. /* height: 10em; */
    31. padding: 1em;
    32. background-color: lightcyan;
    33. border: 1px solid #000;
    34. }
    35. .container {
    36. /* 用在容器中的属性 */
    37. /* flex-direction: row; */
    38. /* flex-direction: column; */
    39. /* flex-wrap: wrap; */
    40. /* 1. 主轴方向,是否换行? */
    41. flex-flow: row nowrap;
    42. flex-flow:
    43. /* flex-flow: row wrap; */
    44. /* 2. place-content: 容器中的剩余空间在项目之间进行分配 */
    45. place-content: start;
    46. place-content: end;
    47. place-content: center;
    48. /* 二端对齐 */
    49. place-content: space-between;
    50. /* 分散对齐 */
    51. place-content: space-around;
    52. /* 平均对齐 */
    53. place-content: space-evenly;
    54. /* 3. 项目在交叉轴上的对齐 */
    55. place-items: stretch;
    56. place-items: start;
    57. place-items: end;
    58. place-items: center;
    59. /* flex容器上只需要记住三个属性就可以了 */
    60. /* 1. flex-flow: 主轴方向, 换行 */
    61. /* 2. place-content: 项目在主轴上的排列与空间分配 */
    62. /* 2. place-items: 项目在交叉轴上的对齐方式 */
    63. }
    64. </style>
    65. </body>
    66. </html>