css响应式布局

“自适应网页设计”到底是怎么做到的?其实并不难。

一、viewport

首先,在网页代码的头部,加入一行viewport元标签

<meta name="viewport" content="width=device-width,initial-scale=1.0,
maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />

viewport是网页默认的宽度和高度,上面这行代码的意思是,网页宽度默认等于屏幕宽度(width=device-width),原始缩放比例(initial-scale=1)为1.0,即网页初始大小占屏幕面积的100%。

二、流动布局(fluid grid)

“流动布局”的含义是,各个区块的位置都是浮动的,不是固定不变的。

.main{float: right; 70%; height:300px ;background-color: azure}
.leftBar{float: left; 25%; height:300px; background-color: burlywood}

float的好处是,如果宽度太小,放不下两个元素,后面的元素会自动滚动到前面元素的下方,不会在水平方向overflow(溢出),避免了水平滚动条的出现。(这里使用了百分比,不会出现这个情况,但如果width固定就会出现)

.main{float: right; 700px; height:300px ;background-color: azure}
.leftBar{float: left; 250px; height:300px; background-color: burlywood}

另外,绝对定位(position: absolute)的使用,也要非常小心。

三、媒体监听

选择加载CSS

“自适应网页设计”的核心,就是CSS3引入的Media Query模块。
它的意思就是,自动探测屏幕宽度,然后加载相应的CSS文件。

<link rel="stylesheet" type="text/css" media="screen and (max-device- 400px)" href="tinyScreen.css"/>

上面的代码意思是,如果屏幕宽度小于400像素(max-device- 400px),就加载tinyScreen.css文件。

<link rel="stylesheet" type="text/css" 
media="screen and (min- 400px) and (max-device- 600px)" href="smallScreen.css"/>

如果屏幕宽度在400像素到600像素之间,则加载smallScreen.css文件。

除了用html标签加载CSS文件,还可以在现有CSS文件中加载。
@import url("tinyScreen.css") screen and (max-device- 400px);

CSS的@media规则

同一个CSS文件中,也可以根据不同的屏幕分辨率,选择应用不同的CSS规则。

@media screen and (max-device- 400px) 
{
  .column {float: none; auto;} 
  #sidebar {display: none}
}

上面的代码意思是,如果屏幕宽度小于400像素,则column块取消浮动(float:none)、宽度自动调节(auto),sidebar块不显示(display:none)。

示例如下

<style type="text/css">
  /*屏幕宽度大于900的时候*/
  *
  {
      padding:0px;
      margin:0px;
      font-family:"微软雅黑";
  }
  #header
  {
      height:100px;
      border:solid 1px red;
      margin:0px auto;
  }
  #main
  {
      margin:10px auto;
      height:400px;
  }
  #footer
  {
      margin:0px auto;
      height:100px;
      border:solid 1px red;
  }
  @media screen and (min-900px)
  {
      #header,#footer
      {
          800px;
      }
      #main
      {
          800px;
          height:400px;;
      }
      #main-left
      {
          200px;
          height:400px;
          border:solid 1px red;
          float:left;
      }
      #main-center
      {
          394px;
          height:400px;
          border:solid 1px red;
          float:left;
      }
      #main-right
      {
          200px;
          height:400px;
          border:solid 1px red;
          float:left;
      }
  }
  @media screen and (min-600px) and (max-900px)
  {
      #header,#footer
      {
          600px;
      }
      #main
      {
          600px;
          height:400px;;
      }
      #main-left
      {
          200px;
          height:400px;
          border:solid 1px red;
          float:left;
      }
      #main-center
      {
          396px;
          height:400px;
          border:solid 1px red;
          float:left;
      }
      #main-right
      {
          display:none;
      }
  }
  @media screen and (max-600px)
  {
      #header,#footer
      {
          300px;
      }
      #main
      {
          300px;
          height:400px;;
      }
      #main-left
      {
          display:none;
      }
      #main-center
      {
          300px;
          height:400px;
          border:solid 1px red;
      }
      #main-right
      {
          display:none;
      }
  }
  </style>
  </head>
  <body>
  <div id="header">头部</div>
  <div id="main">
    <div id="main-left">主题-左边</div>
    <div id="main-center">主题-中间</div>
    <div id="main-right">主题-右边</div>
  </div>
  <div id="footer"></div>
  </body>
原文地址:https://www.cnblogs.com/HelloJC/p/11158178.html