Blogger Information
Blog 30
fans 1
comment 0
visits 16065
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
css 媒体查询(响应式布局),元素定位与flex弹性盒子
moon
Original
462 people have browsed it

媒体查询

  • 布局前提: 在一个宽度受限, 而高度不受限的空间内进行布局
  • 以下代码展示大屏适配,浏览器在大于960px,481~959px,低于480px
    时自动适配,下列三个按钮得大小

    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. <button class="btn first">按钮1</button>
    11. <button class="btn sec">按钮2</button>
    12. <button class="btn three">按钮3</button>
    13. </body>
    14. <style>
    15. html {
    16. font-size: 10px;
    17. }
    18. .btn {
    19. background-color: blue;
    20. color: brown;
    21. border: none;
    22. outline: none;
    23. }
    24. .btn:hover {
    25. cursor: pointer;
    26. opacity: 0.8;
    27. transition: 0.3s;
    28. padding: 0.4rem 0.8rem;
    29. }
    30. .btn.first {
    31. font-size: 1rem;
    32. }
    33. .btn.sec {
    34. font-size: 1.5rem;
    35. }
    36. .btn.three {
    37. font-size: 2.2rem;
    38. }
    39. @media (min-width: 960px) {
    40. html {
    41. font-size: 40px;
    42. }
    43. }
    44. @media (min-width: 481px) and (max-width: 959px) {
    45. html {
    46. font-size: 20px;
    47. }
    48. }
    49. @media (max-width: 480px){
    50. html {
    51. font-size: 10px;
    52. }
    53. }
    54. </style>
    55. </html>

    元素定位与文档流

  • 文档流: 显示顺序与书写顺序一致
  • 静态定位:position: static
  • 相对定位:position: relative,相对定位元素仍然在文档流中,所占空间不释放,只有相对原位置进行了偏移
  • 绝对定位:position: absolute,绝对定位, 不再相对于自身, 而是相对于它的包含块,而能充当绝对定位包含块的元素, 必须是”定位元素”,定位元素: position 不能是 static 就可以了,如果绝对定位元素, 一直向上,找不到可定位的父级元素,就相对于body
  • 固定定位:position: fixed,是绝对定位的子集, 只不过他的定位包含块是固定的,永远是body
  • 以下示例展示固定定位的作用:
  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>Document</title>
  8. </head>
  9. <body>
  10. <header>
  11. <h1 class="title">固定定位演示</h1>
  12. <button onclick="document.querySelector('.modal').style.display='block'">登录</button>
  13. </header>
  14. <div class="modal">
  15. <div class="modal-bg" onclick="this.parentNode.style.display='none'"></div>
  16. <form action="" class="modal-form">
  17. <fieldset style="display: grid; gap: 1em">
  18. <legend>用户登录</legend>
  19. <input type="email" name="email" placeholder="user@email.com" />
  20. <input type="password" name="password" placeholder="不少于6位" />
  21. <button>登录</button>
  22. </fieldset>
  23. </form>
  24. </div>
  25. <style>
  26. * {
  27. margin: 0;
  28. padding: 0;
  29. box-sizing: border-box;
  30. }
  31. header {
  32. background-color: cyan;
  33. display: flex;
  34. padding: 0.5em 1em;
  35. }
  36. .title {
  37. font-weight: lighter;
  38. font-style: italic;
  39. color: white;
  40. text-shadow: 1px 1px 1px #555;
  41. letter-spacing: 1px;
  42. }
  43. header button {
  44. margin-left: auto;
  45. width: 5em;
  46. border: none;
  47. border-radius: 0.5em;
  48. }
  49. header button:hover {
  50. cursor: pointer;
  51. background-color: coral;
  52. color: white;
  53. box-shadow: 0 0 5px #fff;
  54. transition: 0.3s;
  55. }
  56. .modal .modal-form fieldset {
  57. background-color: lightcyan;
  58. border: none;
  59. padding: 2em;
  60. box-shadow: 0 0 5px #888;
  61. }
  62. .modal .modal-form fieldset legend {
  63. padding: 5px 1em;
  64. background-color: rebeccapurple;
  65. color: white;
  66. }
  67. .modal .modal-form {
  68. /* 固定定位 */
  69. position: fixed;
  70. /* 顶部要留出头部导航的位置 */
  71. top: 10em;
  72. /* 左右相等,将表单挤到正中间 */
  73. left: 20em;
  74. right: 20em;
  75. }
  76. /* 半透明的遮罩 */
  77. .modal .modal-bg {
  78. /* 固定布局,定位空间在整个屏幕 */
  79. position: fixed;
  80. /* 屏幕视口的四个顶点的坐标 */
  81. top: 0;
  82. left: 0;
  83. right: 0;
  84. bottom: 0;
  85. /* 背景半透明 */
  86. background-color: rgb(0, 0, 0, 0.5);
  87. }
  88. /* 初始化时将模态框弹层隐藏 */
  89. .modal {
  90. display: none;
  91. }
  92. </style>
  93. </body>
  94. </html>

flex弹性盒子

弹性盒子结构

  • flex弹性盒子由flex容器,与弹性项目组成,flex容器中的”子元素” 则成为” 弹性项目/flex项目”

    flex容器常用属性

  • 1.flex-direction:row(设定主轴方向为横向),flex-direction: column(设定主轴方向为纵向)
  • 2.place-content: 容器中的剩余空间在项目之间进行分配
    place-content: start;
    place-content: end;
    place-content: center;
    place-content: space-between;二端对齐
    place-content: space-around;分散对齐
    place-content: space-evenly;平均对齐
  • 3.place-items: 项目在交叉轴上的对齐方式
    place-items: stretch;
    place-items: start;
    place-items: end;
    place-items: center;

flex项目常用属性

  • 1.flex: 放大因子 收缩因子 计算宽度
  • 2.order:排序参数,值越小,越靠前
  • 3.以下代码实例演示,flex容器,以及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>Document</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <p class="item">item1</p>
  12. <p class="item">item2</p>
  13. <p class="item">item3</p>
  14. <p class="item">item4</p>
  15. <p class="item">item5</p>
  16. </div>
  17. </body>
  18. <style>
  19. .container {
  20. display: flex;
  21. flex-flow: row nowrap;
  22. height: 20em;
  23. border: 1px solid #000;
  24. place-content: center;
  25. }
  26. .item {
  27. width: 5em;
  28. border: darkgoldenrod;
  29. background-color: darkorange;
  30. margin: 20px;
  31. }
  32. .container :first-of-type {
  33. background-color: darkturquoise;
  34. order: 5;
  35. }
  36. .container :last-of-type {
  37. background-color: deeppink;
  38. order: -1;
  39. }
  40. </style>
  41. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post