响应式设计

1、Viewport:

viewport 是用户网页的可视区域。

一个常用的针对移动网页优化过的页面的 viewport meta 标签大致如下:

<meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;" />

2、网格视图:

响应式网格视图通常是 12 列,宽度为100%,在浏览器窗口大小调整时会自动伸缩。

创建响应式网格视图:

首先确保所有的 HTML 元素都有 box-sizing 属性且设置为 border-box:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
    box-sizing: border-box;
}
.header {
    border: 1px solid red;
    padding: 15px;
}
.menu {
    width: 25%;
    float: left;
    padding: 15px;
    border: 1px solid red;
}
.main {
    width: 75%;
    float: left;
    padding: 15px;
    border: 1px solid red;
}
</style>
</head>
<body>

<div class="header">
<h1>Chania</h1>
</div>

<div class="menu">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>

<div class="main">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
<p>Resize the browser window to see how the content respond to the resizing.</p>
</div>

</body>
</html>




如果去掉 box-sizing: border-box;的话,效果如下:



每一行使用 <div> 包裹。所有列数加起来应为 12:

<div class="row">
  <div class="col-3">...</div>
  <div class="col-9">...</div>
</div>

3、媒体查询:

如果浏览器窗口小于500px,背景将变为浅蓝色:

@media only screen and (max-width: 500px) {
    body {
        background-color: lightblue;
    }
}

为移动端优先设计,添加断点:

/* For mobile phones: */
[class*="col-"] {
    width: 100%;
}
@media only screen and (min-width: 600px) {
    /* For tablets: */
    .col-m-1 {width: 8.33%;}
    .col-m-2 {width: 16.66%;}
    .col-m-3 {width: 25%;}
    .col-m-4 {width: 33.33%;}
    .col-m-5 {width: 41.66%;}
    .col-m-6 {width: 50%;}
    .col-m-7 {width: 58.33%;}
    .col-m-8 {width: 66.66%;}
    .col-m-9 {width: 75%;}
    .col-m-10 {width: 83.33%;}
    .col-m-11 {width: 91.66%;}
    .col-m-12 {width: 100%;}
}
@media only screen and (min-width: 768px) {
    /* For desktop: */
    .col-1 {width: 8.33%;}
    .col-2 {width: 16.66%;}
    .col-3 {width: 25%;}
    .col-4 {width: 33.33%;}
    .col-5 {width: 41.66%;}
    .col-6 {width: 50%;}
    .col-7 {width: 58.33%;}
    .col-8 {width: 66.66%;}
    .col-9 {width: 75%;}
    .col-10 {width: 83.33%;}
    .col-11 {width: 91.66%;}
    .col-12 {width: 100%;}
}

一个实例:

<div class="row">
<div class="col-3 col-m-3">...</div>
<div class="col-6 col-m-9">...</div>
<div class="col-3 col-m-12">...</div>
</div>

其中,针对桌面设备(col),为3-6-3的比例设计

针对平板设备(col-m),为3-9-12的比例设计



4、响应式图片:

/* For width smaller than 400px: */
body {
    background-image: url('img_smallflower.jpg'); 
}

/* For width 400px and larger: */
@media only screen and (min-device-width: 400px) {
    body { 
        background-image: url('img_flowers.jpg'); 
    }
}


背景图片:( 如果 background-size 属性设置为 "100% 100%" ,背景图片将延展覆盖整个区域

div {
    width: 100%;
    height: 400px;
    background-image: url('img_flowers.jpg');
    background-size: 100% 100%;
    border: 1px solid red;
}





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
H5响应式设计是指在网页开发中,通过使用HTML5技术和CSS3媒体查询等方法,使网页能够根据不同的设备和屏幕大小做出相应的布局和样式适配,从而让用户在不同的设备上都能够有良好的浏览体验。 H5响应式设计的作业可以包括以下几个方面: 1. 设计稿分析和页面规划:在开始编写响应式网页之前,首先需要对设计稿进行分析和页面规划。根据设计稿的布局和样式要求,确定响应式网页需要适配的设备和屏幕大小范围。 2. 媒体查询的使用:响应式设计离不开媒体查询。在CSS中使用媒体查询可以根据不同的设备和屏幕大小设置不同的样式。通过设置不同的CSS规则,可以使网页在不同的设备上显示不同的布局和样式。 3. 弹性网格布局:在响应式设计中,弹性网格布局是非常重要的一部分。通过使用弹性网格布局,可以使网页的布局在不同设备上具有灵活性和自适应性。 4. 图片和媒体的适配:在响应式设计中,还需要考虑图片和媒体的适配。可以通过设置图片的最大宽度或使用CSS的background-size属性来实现图片的自适应。 5. 网页速度优化:在响应式设计中,网页的加载速度也是一个重要的考虑因素。可以通过优化图片大小、合并和压缩CSS和JavaScript文件等方法来提高网页的加载速度。 总之,H5响应式设计作业涵盖了设计稿分析、页面规划、媒体查询的使用、弹性网格布局、图片和媒体的适配以及网页速度优化等方面,旨在让学生掌握响应式设计的原理和技巧,从而能够开发出良好的响应式网页。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值