Viewport
Viewport
viewport 是用户网页的可视区域。
1、语法
针对移动网页优化过的页面的viewport meta标签大致如下
<meta name="viewport" content="width=device-width, initial-scale=1.0">
- width:控制 viewport 的大小,可以指定的一个值,如 600,或者特殊的值,如 device-width
为设备的宽度(单位为缩放为 100% 时的 CSS 的像素)。 - height:和 width 相对应,指定高度。
- initial-scale:初始缩放比例,也即是当页面第一次 load 的时候缩放比例。
- maximum-scale:允许用户缩放到的最大比例。
- minimum-scale:允许用户缩放到的最小比例。
- user-scalable:用户是否可以手动缩放。
2、网格视图
大部分网页是基于网格设计的,这些网页是按列布局的
响应式网格视图通常是 12 列,宽度为100%,在浏览器窗口大小调整时会自动伸缩。
2.1 创建
前提:所有的HTML元素都有box-sizing 属性且设置为 border-box;确保边距和边框包含在元素的宽度和高度间。
* {
box-sizing: border-box;
}
完整代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<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>
3、媒体查询 @media
3.1、定义
媒体类型允许指定文件将如何在不同的媒体呈现
@media 针对不同的屏幕尺寸设置不同的样式
3.2、语法
@media mediatype and|not|only (media feature) {
CSS-Code;
}
<link rel="stylesheet" media="mediatype and|not|only (media feature)" href="mystylesheet.css">
示例
如果浏览器窗口小于500px,背景将变为浅蓝色
@media only screen and (max-width: 500px) {
body {
background-color: lightblue;
}
}
3.3、媒体类型
3.4、媒体功能
值 | 描述 |
---|---|
aspect-ratio | 定义输出设备中的页面可见区域宽度与高度的比率 |
color | 定义输出设备每一组彩色原件的个数。如果不是彩色设备,则值等于0 |
… | … |
3.5、示例
/* For width smaller than 400px: */
body {
background-image: url('img_smallflower.jpg');
}
/* For width 400px and larger: */
@media only screen and (min-width: 400px) { #检测浏览器宽度
body {
background-image: url('img_flowers.jpg');
}
}
/* 设备小于 400px: */
body {
background-image: url('img_smallflower.jpg');
}
/* 设备大于 400px (也等于): */
@media only screen and (min-device-width: 400px) {#检测设备宽度
body {
background-image: url('img_flowers.jpg');
}
}
4、图片
4.1、width
如果 width 属性设置为 100%,图片会根据上下范围实现响应式功能
img {
width: 100%;
height: auto;
}
注:以上实例中,图片会比它的原始图片大
4.2、max-width
max-width 属性设置为 100%, 图片永远不会大于其原始大小
img {
max-width: 100%;
height: auto;
}
4.3、背景图片
背景图片可以响应调整大小或缩放,设置background-size
contain
按比例自适应内容区域。
图片保持其比例不变(部分区域会留白)
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-repeat: no-repeat;
background-size: contain;
border: 1px solid red;
}
100% 100%
背景图片将延展覆盖整个区域(图片拉伸,充满整个区域)
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: 100% 100%;
border: 1px solid red;
}
cover
背景图像扩展至足够大,以使背景图像完全覆盖背景区域(保持了图片的比例但是图片部分内容无法显示在背景定位区域中)
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: cover;
border: 1px solid red;
}
5、视频
在网页中添加视屏
示例会根据div区域大小自动调整并占满整个div区域
video {
width: 100%;
height: auto;
}
5.1、width
width 属性设置为 100%,视频播放器会根据屏幕大小自动调整比例
video {
width: 100%;
height: auto;
}
5.2、max-width
max-width 属性设置为 100%, 视频播放器会根据屏幕自动调整比例,但不会超过其原始大小
video {
max-width: 100%;
height: auto;
}