隐藏

用CSS实现响应式布局

发布:2021/4/8 9:26:09作者:管理员 来源:本站 浏览次数:1109

响应式网页看起来高大上,但实际上,不用JS只用CSS也能实现响应式网站的布局

要用到的就是CSS中的媒体查询下面来简单介绍一下怎么运用

使用@media 的三种方式

第一: 直接在CSS文件中使用

@media 类型 and (条件1) and (条件二)

{

css样式

}

@media screen and (max-width:980px ) {

body{

background-color: red;

}

}

第二:使用@import导入

@import url("css/moxie.css") all and (max-width:980px);

第三,也是最常用的:使用link连接,media属性用于设置查询方式

<link rel="stylesheet" type="text/css" href="css/moxie.css" media=“all and (max-width=980px)”/>

我们只需用到width衍生出的max-width这个属性,定义输出设备中的页面可见区域宽度来控制该改变的样式即可。

下面是一个简单响应式的布局的html代码

 
	
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>响应式布局</title>
  6. <meta name="viewport"content="width=device-width,initial-scale=1.0,maximum-scale=1,user-scalable=no" />
  7. <meta name="format-detection" content="telephone=no,email=no"/>
  8. <link rel="stylesheet" type="text/css" href="css/mo2.css"/>
  9. </head>
  10. <body>
  11. <div>
  12. <header id="head">
  13. <ul>
  14. <li>header1</li>
  15. <li>header2</li>
  16. <li>header2</li>
  17. <li>header2</li>
  18. <li>header2</li>
  19. </ul>
  20. <div>icon</div>
  21. </header>
  22. <section id="main">
  23. <div class="left">
  24. left
  25. </div>
  26. <div class="center">
  27. center
  28. </div>
  29. <div class="right">
  30. right
  31. </div>
  32. </section>
  33. <footer id="foot">
  34. footer
  35. </footer>
  36. </div>
  37. </body>
  38. </html>

下边是CSS样式

 
	
  1. *{
  2. margin: 0px;
  3. padding: 0px;
  4. font-family: "微软雅黑";
  5. }
  6. #head,
  7. #foot,
  8. #main
  9. {
  10. height: 100px;
  11. width: 1200px;
  12. /*width: 85%;*/
  13. background-color: goldenrod;
  14. text-align: center;
  15. font-size: 48px;
  16. line-height: 100px;
  17. margin: 0 auto;
  18. }
  19. #head div{
  20. display: none;
  21. font-size: 20px;
  22. height: 30px;
  23. width: 100px;
  24. background-color: green;
  25. float: right;
  26. line-height: 30px;
  27. margin-top: 35px;
  28. }
  29. #head ul{
  30. width: 80%;
  31. }
  32. #head ul li{
  33. width: 20%;
  34. float: left;
  35. text-align: center;
  36. list-style: none;font-size: 20px;
  37. }
  38. #main{
  39. height: auto;
  40. margin: 10px auto;
  41. overflow: hidden;
  42. }
  43. .left,
  44. .center,
  45. .right{
  46. height: 600px;
  47. line-height: 600px;
  48. float: left;
  49. width: 20%;
  50. background-color: red
  51. }
  52. .center{
  53. width: 60%;
  54. border-left: 10px solid #FFF;
  55. border-right: 10px solid #FFF;
  56. box-sizing: border-box;
  57. }
  58. @media only screen and (max-width: 1200px) {
  59. #head,
  60. #foot,
  61. #main{
  62. width: 100%;
  63. }
  64. }
  65. @media only screen and (max-width: 980px) {
  66. .right{
  67. display: none;
  68. }
  69. .left{
  70. width: 30%;
  71. }
  72. .center{
  73. width: 70%;
  74. border-right: hidden;
  75. }
  76. }
  77. @media only screen and (max-width: 640px) {
  78. .left,
  79. .center,
  80. .right{
  81. width: 100%;
  82. display: block;
  83. height: 200px;
  84. line-height: 200px;
  85. }
  86. .center{
  87. border: hidden;
  88. border-top: 10px solid #FFFFFF;
  89. border-bottom: 10px solid #FFFFFF;
  90. height: 600px;
  91. line-height: 600px;
  92. }
  93. #head ul{
  94. display: none;
  95. }
  96. #head div{
  97. display: block;
  98. }
  99. }

窗口大于1200px时显示的样子

 

 窗口小于1200大于980时,只会被压缩,并不会发生其他变化


当大于640小于980时,右侧栏隐藏


当小于640时,导航栏折叠,body三部分竖直排列显示,若窗口持续缩小,不在发生变化,区域被压缩


好了,布局就这么简单,细节的把握还靠不断地练习。