当前位置:网站首页>关于Web响应式设计
关于Web响应式设计
2022-07-21 00:01:00 【InfoQ】
前言
再到现在,手机和电脑都需要跑不同尺寸的网页,这会出现了 “自适应布局”,解决了开发者设计者的困惑,开发者不需要单独去维护两套站点的代码和样式。 开发者需要写几种不同尺寸下的布局(媒体查询 )效果。 相比之前的 固定布局 , 液体布局好多了,不过还不是很完美。 后来,又有了 “响应式布局”,完美得解决了之前的问题, 自适应布局 是 媒体布局 + 固定布局 混搭, 而 响应式布局是媒体布局 + 流动布局的混搭。
<meta name="viewport" content="width=device-width, initial-scale=1">
媒体布局
查询条件
@media type and (feature)
all
应用于所有设备
print
应用于打印机和打印预览
screen
应用于电脑,平板
all
and
: 合并查询条件
only
: 仅在某个type
下显示
or
: 满足多种条件时使用,用逗号分隔开
not
:不能应用于单独的媒体查询否定条件中,它只能应用于整个查询。
@media (min-width: 700px) and (max-width: 1000px)
{
...
}
@media only screen and (min-width: 1024px){
···
}
@media (max-width:500px) , (orientation: landscape) {
···
}
@media not screen and (min-width:800px) and (max-width:1200px),print and (color) {
···
}
根据浏览器窗口模式 调整样式
orientation
@media all and (orientation: landscape) {
// Styles for landscape mode.
}
@media all and (orientation: portrait) {
// Styles for portrait mode.
}
根据视口大小调整样式
min-width
@media (min-width: 400px) {
// Styles for viewports wider than 400 pixels.
}
max-width
@media (max-width: 400px) {
// Styles for viewports narrower than 400 pixels.
}
@media (min-width: 600px) and (max-width: 1000px) {
}
px
em
常见PC端适配代码
/* 适配各种屏幕尺寸 */
@media (min-width: 1024px){
body,html{font-size: 18px}
}
@media (min-width: 1100px) {
body,html{font-size: 20px}
}
@media (min-width: 1280px) {
body,html{font-size: 22px;}
}
@media (min-width: 1366px) {
body,html{font-size: 24px;}
}
@media (min-width: 1440px) {
body,html{font-size: 25px;}
}
@media (min-width: 1680px) {
body,html{font-size: 28px;}
}
@media (min-width: 1920px) {
body,html{font-size: 33px;}
}
响应式图像
max-inline-size
img,
video,
iframe {
max-inline-size: 100%;
block-size: auto;
}

图像加载提示
img
loading
loading=lazy
图像延迟加载,只有鼠标滚动到该图片所在位置才会显示。
loading=eager
默认,图像立即加载。
<img
src="xxx.jpg"
alt=""
loading="eager"
>
图像解码
img
decoding
decoding="async"
<img
src="xxx.jpg"
alt=""
loading="eager"
decoding="async"
>
响应式图像
max-inline-size:100`` 让图片完美的显示在浏览器中,但有时可能需要根据不同尺寸显示不同宽度的图片效果,就可以使用
<img
src="small-image.png"
srcset="small-image.png 300w,
medium-image.png 600w,
large-image.png 1200w"
>
图像格式
srcset
picture
picture
img
source
source
srcset
source
src
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt=""
decoding="async">
</picture>
网站主题化
提供黑暗模式
prefers-color-scheme
light
dark
@media (prefers-color-scheme: dark) {
body{
color: red;
font-size: 22px;
}
}
@media (prefers-color-scheme: light) {
body{
color: rgb(17, 61, 183);
font-size: 33px;
}
}
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="theme-color" content="#ffffff" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#000000" media="(prefers-color-scheme: dark)">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img{
max-inline-size: 100%;
}
@media (prefers-color-scheme: dark) {
body{
color: red;
font-size: 22px;
}
}
@media (prefers-color-scheme: light) {
body{
color: rgb(17, 61, 183);
font-size: 33px;
}
}
</style>
</head>
表单主题化
[accent-color](https://web.dev/accent-color/)
html {
color-scheme: light;
}
@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
accent-color: pink;
}
}
head
<meta name="supported-color-schemes" content="light dark">
边栏推荐
- C#(三十五)之在滚动窗口中绘图
- MySQL explain execution plan analysis
- 【2023提前批 之 面经】~ 联发科
- Particle system in Niagara - ue5
- ICML 2022 | 教程效度,可靠性和意义:可复现机器学习的统计方法教程
- SAP Fiori topic 2: using webide to build Fiori with navigation bar
- YOLOX----人间疑惑!
- 解决MySQL主键id从1开始自增 ,id不连续的问题
- YII框架安装步骤(yii框架版本1.1.20,时间是2018/11)
- 动手学moveit2|介绍和安装
猜你喜欢
Particle system in Niagara - ue5
DeFi 对经济模式带来的改变和存在的局限
[CS231N]Notes_1-Introduction
从一线开发到技术总监,你就差一个赶鸭子上架
PHP FPM custom ZABBIX monitoring
Kubernetes apiserver, etcd, controller manager, scheduler high availability principle
Mutex mutex of thread C (43) is mutually exclusive
Jmeter-正则、xpath、JSON
php-fpm自定义zabbix监控
C#(三十八)之StreamWriter StreamWriter使用方法及与FileStream类的区别
随机推荐
Module learning (V) - matrix keyboard
Thread of C (41)
Swift 中风味各异的依赖注入
Yarn run dev cannot load file c:\program files\nodejs\node_ global\yarn. PS1, because script execution is prohibited in this system
夏日大作战!卧兔网络带你看出海达人营销创意!
XFS 打开电子商务新方式
C#(四十四)之线程死锁
[CS231N]Notes_1-Introduction
免费、强大的开源笔记软件Joplin综合评测 —印象笔记的开源替代
Jianghu nickname of Chinese Universities
C#(三十七)之基于流的文件操作(FileStream)
IP day 11 notes
力扣139题:单词拆分
ROS2入门教程 | 动作(Action)通信与自定义接口
新版的动手学ROS2发布啦
Hands on moveit2 | introduction and installation
被鱼粉直呼小鱼牛逼的代码,来看ROS2如何进行点云PCL处理(订阅、转换、保存)
ip6tables v1.8.7 (legacy): Couldn‘t load match `pkttype‘:No such file or directory
[featured] the structure of MySQL B-tree and b+tree?
[MySQL practice] multi table linkage update data in SQL