使用 media 实现响应式布局

前端之家收集整理的这篇文章主要介绍了使用 media 实现响应式布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近工作有一个需求是将一个界面改为响应式布局,由于UI还没有给设计,于是自己先查了一下资料做了一个demo。其实实现响应式布局的方式有很多,利用media实现就是其中一种,但是他也有一些缺点,比如说要对特别的屏幕单独定制样式代码。在我的代码里面我把屏幕分为了三种,代表为iPhone、iPad、PC三种,分别对应着三种不同的样式。

目前可以实现:

  • 根据界面大小自动调整布局
  • 界面宽度小到一定程度时会隐藏header,将其放到侧拉栏中

效果图如下(代码会在下面全部放上来):

media使用

原理

media简单来说就是一种查询工具,加入说你想知道打开你网页的屏幕宽度是768px的时候才使用这个样式,这个时候你就可以这样写:

  1. @media screen and (max-width:768px){
  2. body{
  3. background-color: black
  4. }
  5. }

这个代码效果就是当前界面的宽度小于768px的时候,将网页背景变成黑色。screen是用于电脑屏幕、平板电脑、智能手机等。对于@media的更多媒体类型如下:

描述
all 用于所有设备
print 用于打印机或打印预览
screen 用于电脑屏幕、平板电脑、智能手机等
speech 用于屏幕阅读器等发声设备

在做响应式布局的时候我主要用到max-widthmin-width两种属性min-width的作用于max-width的作用相反。

应用

  1. <link rel="stylesheet" href="./index.css">
  2. <link rel="stylesheet" href="./index_ipad.css" media="screen and (max-width:1200px)">
  3. <link rel="stylesheet" href="./index_mobile.css" media="screen and (max-width:768px)">

由我的代码可以得知我将页面分为三种大小,分别为(1200,+∞),(768,1200),(0,768),这个分类我是参照bootstrap来分的。

首先引入index.css,这也是你的电脑打开时的默认样式,当你的电脑宽度逐渐减小时,就会开始应用index_ipad.css这个样式文件,在这个文件中并不是将index.css的样式代码全部重写了一遍,而是把需要更改样式的代码做了编写。

举个例子,比如说我index.css中有四个方块,默认布局是float布局,全部排在一行,但是当页面宽度变为ipad大小是页面方块就会变成两行,原理是改一下方块的宽度。具体实现代码如下:

  1. /* index.css */
  2. .board {
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. height: 200px;
  7. float: left;
  8. width: 25%;
  9. color: white
  10. }
  11. .first {
  12. background-color: #F44336
  13. }
  14. .second {
  15. background-color: #E91E63
  16. }
  17. .third {
  18. background-color: #9C27B0
  19. }
  20. .fourth {
  21. background-color: #009688
  22. }
  1. /* index_ipad.css */
  2. .first,.second,.third,.fourth {
  3. width: 50%;
  4. }

侧拉栏

侧拉栏的原理其实并不难,就是先写一个div,保持与header元素相同,然后再设置其left属性,使其隐藏,通过js操作其left,将其显示出来。

  1. <div class="nav">
  2. <ul>
  3. <li>
  4. <a>第一个</a>
  5. </li>
  6. <li>
  7. <a>第二个</a>
  8. </li>
  9. <li>
  10. <a>第三个</a>
  11. </li>
  12. </ul>
  13. </div>
  1. .nav {
  2. position: absolute;
  3. z-index: 11;
  4. left: -10rem;
  5. top: 0;
  6. width: 10rem;
  7. height: 100%;
  8. background: #607D8B;
  9. }
  1. window.onload = function() {
  2. let btn = document.getElementsByClassName('menu')[0]
  3. let nav = document.getElementsByClassName('nav')[0]
  4. // 改变侧拉栏状态
  5. btn.addEventListener('click',function() {
  6. nav.style.left = nav.style.left == '-10rem' || nav.style.left.length == 0 ? 0 : '-10rem';
  7. },false);
  8. }

全部代码

  1. <!-- index.html -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <Meta charset="UTF-8">
  6. <Meta name="viewport" content="width=device-width,initial-scale=1.0">
  7. <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>响应式布局</title>
  9. <link rel="stylesheet" href="./index.css">
  10. <link rel="stylesheet" href="./index_ipad.css" media="screen and (max-width:1200px)">
  11. <link rel="stylesheet" href="./index_mobile.css" media="screen and (max-width:768px)">
  12. <script src="./index.js"></script>
  13. </head>
  14. <body>
  15. <div class="nav">
  16. <ul>
  17. <li>
  18. <a>第一个</a>
  19. </li>
  20. <li>
  21. <a>第二个</a>
  22. </li>
  23. <li>
  24. <a>第三个</a>
  25. </li>
  26. </ul>
  27. </div>
  28. <nav>
  29. <img src="./img/菜单.png" alt="菜单" class="menu">
  30. <a href="#">第一个</a>
  31. <a href="#">第二个</a>
  32. <a href="#">第三个</a>
  33. </nav>
  34. <div>
  35. <div class="board first">
  36. 第一个
  37. </div>
  38. <div class="board second">
  39. 第二个
  40. </div>
  41. <div class="board third">
  42. 第三个
  43. </div>
  44. <div class="board fourth">
  45. 第四个
  46. </div>
  47. </div>
  48. </body>
  49. </html>
  1. /* index.css */
  2. .board {
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. height: 200px;
  7. float: left;
  8. width: 25%;
  9. color: white
  10. }
  11. .first {
  12. background-color: #F44336
  13. }
  14. .second {
  15. background-color: #E91E63
  16. }
  17. .third {
  18. background-color: #9C27B0
  19. }
  20. .fourth {
  21. background-color: #009688
  22. }
  23. nav {
  24. background-color: #607D8B;
  25. text-align: right;
  26. height: 5vh;
  27. display: flex;
  28. align-items: center;
  29. justify-content: right
  30. }
  31. a {
  32. text-decoration-line: none;
  33. color: white;
  34. margin-right: 3%
  35. }
  36. .menu {
  37. width: 1.5rem;
  38. margin-left: 0px;
  39. display: none;
  40. cursor: pointer;
  41. }
  42. ul,li {
  43. list-style: none;
  44. padding: 0;
  45. margin: 0;
  46. }
  47. .nav {
  48. position: absolute;
  49. z-index: 11;
  50. left: -10rem;
  51. top: 0;
  52. width: 10rem;
  53. height: 100%;
  54. background: #607D8B;
  55. }
  56. .nav {
  57. transition: left linear .1s;
  58. }
  59. .nav a {
  60. display: block;
  61. padding: 1em 0;
  62. border-bottom: 1px solid #888;
  63. font-size: 16px;
  64. color: #eee;
  65. text-align: center;
  66. }
  67. .nav li {
  68. cursor: pointer;
  69. }
  1. /* index_mobile.css */
  2. .first,.fourth {
  3. float: none;
  4. width: 100%;
  5. }
  6. .menu {
  7. display: block;
  8. margin-right: 2%;
  9. }
  10. a {
  11. display: none
  12. }
  1. /* index_ipad.css */
  2. .first,.fourth {
  3. width: 50%;
  4. }
  5. .menu {
  6. display: block;
  7. margin-right: 2%;
  8. }
  9. a {
  10. display: none
  11. }
  1. //index.js
  2. window.onload = function() {
  3. let btn = document.getElementsByClassName('menu')[0]
  4. let nav = document.getElementsByClassName('nav')[0]
  5. btn.addEventListener('click',false);
  6. }

猜你在找的CSS相关文章