美文网首页
Introduction to CSS(4)- Responsi

Introduction to CSS(4)- Responsi

作者: sunboximeng | 来源:发表于2018-04-21 08:11 被阅读7次

What is a Responsive Web Site?
Site's layout adapts to the size of the device.

以前的网页分为手机版和电脑版,就是为了解决小屏幕浏览体验差的问题。但是随着4G的普及,移动端设备大量访问互联网,屏幕尺寸又非常多,一个一个去适配非常麻烦。于是就产生了Responsive Web Site。

Content is like water.png

实现 Responsive design 的方式:Media query(实现屏幕大小判断) + Proportional-based Grids(利用相对值可以按照屏幕大小缩放元素) + Float(使得盒子可以并排)。

浏览器开发者模式下,有一个手机图标,可以用来查看网页在不同屏幕大小下的效果。

Media query

CSS 中的 if 语句。

p { color: blue; } /* base styles */
...
@media (min-width: 1200px) { /* large */
...
}
@media (min-width: 950px) and (max-width: 1199px) { /* middle */
...
}
@media (min-width: 950px) and (max-width: 1199px) { /* small */
...
}

Proportional-based Grids

图片1.png

HTML:

<body>
<h1>Responsive Layout</h1>

<div class="row">
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 1</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 2</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 3</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 4</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 5</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 6</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 7</p></div>
  <div class="col-lg-3 col-md-6 col-sm-12"><p>Item 8</p></div>
</div>

</body>

CSS: 大屏4列,中屏2列,小屏1列。

h1 {
  margin-bottom: 15px;
}
p {
  border: 1px solid black;
  background-color: #A52A2A;
  width: 90%;
  height: 150px;

  /* 自动调整margin */
  margin-right: auto;
  margin-left: auto;

  font-family: Helvetica;
  color: white;
}
/* Simple Responsive Framework. */
.row {
  width: 100%;
}
/********** Large devices only **********/
/* middle、small device 的CSS代码都差不多,省略了 */
@media (min-width: 1200px) {
  .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12 {
    float: left;
    border: 1px solid green;
  }
  .col-lg-1 {
  /* 小数点保留两位才精确 */
    width: 8.33%;
  }
  .col-lg-2 {
    width: 16.66%;
  }
  .col-lg-3 {
    width: 25%;
  }
  .col-lg-4 {
    width: 33.33%;
  }
  .col-lg-5 {
    width: 41.66%;
  }
  .col-lg-6 {
    width: 50%;
  }
  .col-lg-7 {
    width: 58.33%;
  }
  .col-lg-8 {
    width: 66.66%;
  }
  .col-lg-9 {
    width: 74.99%;
  }
  .col-lg-10 {
    width: 83.33%;
  }
  .col-lg-11 {
    width: 91.66%;
  }
  .col-lg-12 {
    width: 100%;
  }
}
图片2.png 图片3.png 图片4.png

手机端的一个小问题:手机会自动缩放页面。
How do we tell the phone's browser that this is actually a responsive website and you don't need to try to zoom out, just stay at the regular zoom level?
在head标签里面加一个meta标签:

<meta name="viewport" content="width=device-width, initial-scale=1">

相关文章

网友评论

      本文标题:Introduction to CSS(4)- Responsi

      本文链接:https://www.haomeiwen.com/subject/jbmtlftx.html