响应式 Web 设计基础 ref
三个主要idea:
- flexible grids for layout
- flexible media for images and videos
- CSS media queries to create diffrent styles for different screen widths
设置响应式的页面
<meta name="viewport" content-width="device-width">
指示浏览器设置其视口 (viewport) 宽度为设备 (device) 宽度.
媒体查询 (Media Queries) w3 ref
使用 Media Queries 的策略
- Adjust columns - 多columns desktop/lanscape tablet; 单column手机
- Flexible width -
max-width: 960px
for desktop;width: 100%
for 手机 - Tighten up white space - 在手机上, 调整headline, graphics 和其他元素的附加的空白, 以免浪费小屏上紧张的空间量.
- Adjust font sizes - 手机上缩小字体, 节省空间.
- Changing navigation menus - Collapse the menu …
- Hide content on handled devices 都是为了少空间; 缺点, 可能会遗漏重要内容.
- Use background images - 可以根据不同的screen width来加载不同大小的图片.
设置断点 (Breakpoints)
Media queries 让你设置breakpoint来调整styles.
桌面优先 vs. 移动端优先
- 桌面优先
- 移动端优先
html 中创建媒体查询
|
|
样式表中创建媒体查询
Use the
@import
directive123@import url(css/base.css); /* no media query, applies to all */@import url(css/medium.css) (min-width:481px) and (max-width:768px);@import url(css/small.css) (max-width:480px);Embed the media query in the style sheet
12345678@media (max-width: 480px) {body {/* style properties go here */}.style1 {/* style properties go here */}}
基本的样式表结构
|
|
移动端优先
|
|
弹性网格
例子: two-column design
|
|
|
|
HTML 源码顺序很重要
Desktop => phone: Float columns side by side => top to bottom
Rest the Box Model
为国避免 Float drops,
|
|
将固定宽度转为弹性网格
element width / container width = result
流式图片
尽管flexible columns会隨屏幕变窄, 但图片通常不会.
两步设置流式图片
最大宽度
1img { max-width: 100%; }删去
<img>
标签的height
andwidth
属性1<img src="bunny.jpg" width="320" height="200" alt="bunny">into:
1<img src="bunny.jpg" alt="bunny">
上述方法假设, 让所有images 填满列宽的.
或许, 你想让图片比列小一些:
|
|
流式图片的缺点
在小屏上仅缩小尺寸, 而不是用更小的图片代替, 不省流量.
解决方法1: responsive-images-in-practice
解决方法2: Adaptive images
视频 和 Flash
|
|
videos using iframe
: Responsive-Design-Create-Fluid-YouTube-amp-Vimeo-Content
Embed Responsively service: embedresponsively