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

css中rem+vw布局的原理,grid布局与仿img.baidu.com实例

程序员文章站 2022-03-13 12:39:47
...

rem+VW布局优势与技巧

  • 此布局优势:元素动态适配页面宽度
  • 通常将html元素的font-size设置为calc(100vw / 3.75)
  • 尽管vm可做到无比丝滑的动态设置元素大小,但为了防止过小,或过大,最好用媒体查询来约束

    grid布局优势

  • 1、固定和灵活的轨道尺寸;
  • 2、可以使用行号,名称或通过定位网格区域将项目放置在网格上的精确位置;
  • 3、可以将多个项目放入网格单元格或区域中,它们可以彼此部分重叠。

grid布局属性

grid布局容器属性

  • grid布局中,grid容器使用grid-template-rows属性来控制布局显示网络需要的行数;
  • grid布局中,grid容器使用grid-template-columns属性来控制布局显示网络需要的列数;
  • grid布局中,grid容器使用grid-auto-rows;属性来控制布局隐式网络的大小
  • grid布局中,grid容器使用gap属性来控制,项目间的间隙
  • grid布局中,grid容器使用place-content属性来控制,剩余空间的分配方式
  • grid布局中,grid容器使用place-items属性来控制,项目在”单元格”中的对齐方式,例如下列代码展示了一个简单的grid布局:
  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. <div class="item">item1</div>
  12. <div class="item">item2</div>
  13. <div class="item">item3</div>
  14. <div class="item">item4</div>
  15. <div class="item">item5</div>
  16. <div class="item">item6</div>
  17. <div class="item">item7</div>
  18. <div class="item">item8</div>
  19. <div class="item">item9</div>
  20. </div>
  21. <style>
  22. .container {
  23. width: 30em;
  24. height: 30em;
  25. display: grid;
  26. grid-template-columns: repeat(3,1fr);
  27. grid-template-rows: repeat(3,1fr);
  28. background-color: cornsilk;
  29. gap: 10px 10px;
  30. place-content: center;
  31. place-items: center;
  32. }
  33. .container .item {
  34. width: 5em;
  35. height: 5em;
  36. background-color: darkorchid;
  37. }
  38. </style>
  39. </body>
  40. </html>

防img.baidu.com

  • 示例代码如下
  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. <main>
  12. <div class="header">
  13. <div class="top_left">
  14. <a href="http://news.baidu.com/" name="i_news">新闻</a>
  15. <a href="https://www.hao123.com/" name="i_hao123">hao123</a>
  16. <a href="http://map.baidu.com/" name="i_map">地图</a>
  17. <a href="https://haokan.baidu.com/?sfrom=baidu-top/" name="i_video">好看</a>
  18. <a href="http://tieba.baidu.com/" name="i_tieba">贴吧</a>
  19. <a href="https://xueshu.baidu.com/" name="i_xueshu">学术</a>
  20. </div>
  21. <div class="top_right">
  22. <a id="new-userinfo-baiduIndex" href="https://www.baidu.com">百度首页</a>
  23. <a id="new-userinfo-baiduIndex" href="https://www.baidu.com">收藏本页</a>
  24. <a id="new-userinfo-offical" target="_blank" href="https://image.baidu.com/img/image/imageplus/index.html?fr=image">官方合作</a>
  25. <button>登录</button>
  26. </div>
  27. </div>
  28. <div class="dingwei">
  29. <img src="./img/myimg1.png" alt="" class="baidu"></img>
  30. <input class="sousuo" type="text"></input>
  31. <button class="serch">百度一下</button>
  32. <div class="tip">
  33. <label for="">热门搜索:</label>
  34. <label for="">壁纸</label>
  35. <label for="">头像</label>
  36. <label for="">手抄报</label>
  37. <label for="">高清美图</label>
  38. </div>
  39. </div>
  40. </main>
  41. <footer>
  42. 精美合辑
  43. <div class="container">
  44. <div class="first">
  45. <label for="">城市建筑摄影专题</label>
  46. </div>
  47. <div class="second">
  48. <label for="">渐变风格插画</label>
  49. </div>
  50. <div class="three">
  51. <label for="">皮影</label>
  52. </div>
  53. <div class="fourth">
  54. <label for="">宠物图片</label>
  55. </div>
  56. <div class="five">
  57. <label for="">航拍地球系列</label>
  58. </div>
  59. <div class="six">
  60. <label for="">查看更多合辑</label>
  61. </div>
  62. </div>
  63. </footer>
  64. </div>
  65. <style>
  66. .container {
  67. width: 100%;
  68. height: 32rem;
  69. display: grid;
  70. grid-template-columns: repeat(3,1fr);
  71. grid-template-rows: 3em minmax(calc(100vh - 21em - 0.6em), 1fr) 18em;
  72. background-color: white;
  73. }
  74. main {
  75. grid-row-end: span 2;
  76. grid-column-end: span 3;
  77. background-image:url(https://img6.bdstatic.com/img/image/pcindex/sunjunpchuazhoutu.JPG);
  78. width: 100%;
  79. height: 100%;
  80. box-sizing: cover;
  81. overflow: hidden;
  82. zoom: 1;
  83. background-position: center center;
  84. background-color: yellowgreen;
  85. background-size: 100%;
  86. }
  87. main .header{
  88. background-color: rgba(0,0,0,0.3);
  89. height: 3em;
  90. display: flex;
  91. place-content: space-between;
  92. place-items: center center;
  93. }
  94. main .dingwei{
  95. position: absolute;
  96. top:25%;
  97. left: 30%;
  98. }
  99. main .dingwei .baidu{
  100. position: absolute;
  101. left:150px;
  102. height: 76px;
  103. width: 244px;
  104. }
  105. main .dingwei .sousuo{
  106. position:absolute;
  107. height: 40px;
  108. width: 496px;
  109. font: 16px/18px arial;
  110. outline: 0;
  111. border: 0;
  112. top:100px;
  113. left: 10px;
  114. }
  115. main .dingwei .tip{
  116. width: 400px;
  117. color: white;
  118. display: flex;
  119. position:absolute;
  120. top:150px;
  121. left: 100px;
  122. place-content: space-between;
  123. }
  124. main .dingwei .serch {
  125. position: absolute;
  126. top: 99px;
  127. left: 511px;
  128. cursor: pointer;
  129. width: 108px;
  130. height: 44px;
  131. line-height: 45px;
  132. padding: 0;
  133. background: 0 0;
  134. background-color: #E4E4E5;
  135. border-radius: 0 10px 10px 0;
  136. font-family: Arial;
  137. font-size: 17px;
  138. color: #222;
  139. box-shadow: none;
  140. font-weight: 400;
  141. border: 0;
  142. outline: 0;
  143. }
  144. .top_right button{
  145. background-color: blue;
  146. color: white;
  147. }
  148. footer {
  149. grid-column-end: span 3;
  150. background-color: white;
  151. margin: 15px;
  152. }
  153. footer .container{
  154. display: grid;
  155. grid-template-rows: repeat(2,1fr);
  156. grid-template-columns: repeat(3,1fr);
  157. background-color: white;
  158. }
  159. footer .container .first{
  160. width: 80%;
  161. height: 80%;
  162. box-sizing: cover;
  163. background-repeat: no-repeat;
  164. background-position: center center;
  165. background-image: url(https://t7.baidu.com/it/u=1595072465,3644073269&amp;fm=193&amp;f=GIF);
  166. border-radius: 12px;
  167. background-color: #E3D3D5;
  168. margin: 15px;
  169. background-size: 100%;
  170. }
  171. footer .container .second{
  172. width: 80%;
  173. height: 80%;
  174. border-radius: 12px;
  175. box-sizing: cover;
  176. background-repeat: no-repeat;
  177. background-position: center center;
  178. background-image: url(https://t7.baidu.com/it/u=1819248061,230866778&amp;fm=193&amp;f=GIF);
  179. background-color: #005E9C;
  180. margin: 15px;
  181. background-size: 100%;
  182. }
  183. footer .container .three{
  184. width: 80%;
  185. height: 80%;
  186. border-radius: 12px;
  187. box-sizing: cover;
  188. background-repeat: no-repeat;
  189. background-position: center center;
  190. background-image: url(https://t7.baidu.com/it/u=2168645659,3174029352&amp;fm=193&amp;f=GIF);
  191. background-color: #B0A290;
  192. margin: 15px;
  193. background-size: 100%;
  194. }
  195. footer .container .fourth{
  196. width: 80%;
  197. height: 80%;
  198. border-radius: 12px;
  199. box-sizing: cover;
  200. background-repeat: no-repeat;
  201. background-position: center center;
  202. background-image: url(https://t7.baidu.com/it/u=4162611394,4275913936&amp;fm=193&amp;f=GIF);
  203. background-color: #AD8151;
  204. margin: 15px;
  205. background-size: 100%;
  206. }
  207. footer .container .five{
  208. width: 80%;
  209. height: 80%;
  210. border-radius: 12px;
  211. box-sizing: cover;
  212. background-repeat: no-repeat;
  213. background-position: center center;
  214. background-image: url(https://t7.baidu.com/it/u=2621658848,3952322712&amp;fm=193&amp;f=GIF);
  215. background-color: #AD8151;
  216. margin: 15px;
  217. background-size: 100%;
  218. }
  219. footer .container .six{
  220. width: 80%;
  221. height: 80%;
  222. border-radius: 12px;
  223. box-sizing: cover;
  224. background-repeat: no-repeat;
  225. background-position: center center;
  226. background-image: url(https://emoji.cdn.bcebos.com/yunque/shouyebeijingtu.png);
  227. background-color: #AD8151;
  228. margin: 15px;
  229. background-size: 100%;
  230. }
  231. footer .container:nth-of-type(n) label
  232. {
  233. position: relative;
  234. top: 160px;
  235. color: white;
  236. padding: 0 16px;
  237. height: 46px;
  238. line-height: 46px;
  239. overflow: hidden;
  240. font-size: 14px;
  241. white-space: nowrap;
  242. text-overflow: ellipsis;
  243. }
  244. .top_left a,.top_right a{
  245. display: inline-block;
  246. height: 20px;
  247. font:400;
  248. color: white;
  249. text-decoration: none;
  250. margin: 0 0 0 24px;
  251. }
  252. </style>
  253. </body>
  254. </html>

执行代码结构如下:
css中rem+vw布局的原理,grid布局与仿img.baidu.com实例