当前位置:网站首页>响应式布局与px/em/rem的比对
响应式布局与px/em/rem的比对
2022-07-31 00:38:00 【百里狂生】
概要
文章目录
1. 响应式布局
1.1 自适应的目的
自适应的目的是在不同分辨率的不同设备上显示相同的画面
手机屏幕比较小,宽度通常在600px以下,PC端一般在1000px以上,部分高配的笔记本在2000px也有,同样的页面要显示在不同的设备上,还要呈现出满意的效果,不是一件容易的事情。
响应式的概念应该是覆盖了自适应,但是包括的东西太多了。
响应式布局可以根据屏幕的大小自动的调整页面的展现方式,以及布局
1.2 响应式布局的一些技术记录
- 1、允许网页的宽度自动的调整
- 2、尽量少使用绝对的宽度,多点百分比
- 3、相对大小的字体:字体不要使用px写死,最好使用相对大小的em,或者高清方案rem,这个不限制与字体,别的属性也可以这么设置。
- 4、流式布局,float等float的好处是,如果宽度太小,放不下两个元素,后面的元素会自动滚动到前面元素的下方,不会在水平方向overflow(溢出),避免了水平滚动条的出现。
- 5、选择加载css
<!-- 这个意思是如果屏幕宽度小于400px就加载rinyScreen.css文件 -->
<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 400px)" href="tinyScreen.css">
2. px / em / rem 的比对
2.1 px
px 是你屏幕设备物理上能显示的最小的一个点,这个点不是固定宽度的,不同设备上的长宽比例有可能会不同
div {
font-size: 16px;
width: 50px;
height: 50px;
background-color: #f8f8f8;
}
<div>物理像素点文本大小</div>
2.1.1 px 的兼容性
兼容所有浏览器
2.2 em
em 相对于父元素的font-size大小的相对单位
所有现代浏览器下默认字体尺寸是16px,这时1em = 16px,然后你人为把body里面定义font-size: 12px;(把浏览器默认16px改小),此时1em = 12px;
如果0.8em实际上就等于12px*0.8,
em的用处是你要整个网页字体统一变大变小,你只要改body里面的font-size的值就行了。
另外:
em 会继承父元素的字体大小
.parent {
font-size: 10px;
background-color: #f8f8f8;
}
.parent-2 {
font-size: 1em; /* font-size: 10px */
width: 1em; /* width: 10px */
height: 1em; /* height: 10px */
background-color: #777;
}
.child {
font-size: 5em; /* font-size: 50px */
width: 5em; /* width: 50px */
height: 5em; /* height: 50px */
background-color: #52ccba;
}
<div class="parent">
<div class="parent-2">
父元素文本大小1em
<div class="child">子元素文本大小5em</div>
</div>
</div>
2.2.1 em 的兼容性
2.3 rem
相对于HTML文档的font-size大小的相对单位
rem是相对于html元素的font-size大小的相对单位,所以在使用rem 的时候,只需要设置html元素的font-size大小即可,而且html的font-size是rem 的基准。
html {
font-size: 10px;
}
.rem-1 {
width: 1rem; /* width: 10px */
height: 1rem; /* height: 10px */
font-size: 1rem; /* font-size: 10px */
background-color: #f8f8f8;
}
.rem-2 {
width: 5rem; /* width: 50px */
height: 5rem; /* height: 50px */
font-size: 5rem; /* font-size: 50px */
background-color: #52ccba;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="uft-8">
<title>rem</title>
</head>
<body>
<div class="rem-1">文本大小为1rem</div>
<div class="rem-2">文本大小为5rem</div>
</body>
</html>
2.3.1 rem 的兼容性
边栏推荐
- 【深入浅出玩转FPGA学习15----------时序分析基础】
- (五)fastai应用
- Jetpack Compose learning (8) - State and remeber
- Common network status codes
- mysql索引失效的常见9种原因详解
- 如何在WordPress网站上添加导航菜单
- Adding, deleting, modifying and checking the foundation of MySQL
- ELK deployment script---pro test available
- 不用Swagger,那我用啥?
- (5) fastai application
猜你喜欢
Restricted character bypass
【唐宇迪 深度学习-3D点云实战系列】学习笔记
正则表达式密码策略与正则回溯机制绕过
【ABAP】MFBF过账到质量检验库存类型Demo
Meeting OA project pending meeting, all meeting functions
WEB安全基础 - - -漏洞扫描器
C语言力扣第48题之旋转图像。辅助数组
MySQL table design for message queue to store message data
How to ensure the consistency of database and cache data?
从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
随机推荐
.NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
MySQL master-slave replication and read-write separation script - pro test available
pytorch双线性插值
MySQL筑基篇之增删改查
Shell编程之条件语句
ELK deployment script---pro test available
(五)fastai应用
registers (assembly language)
ELK部署脚本---亲测可用
MySQL notes under
MySQL数据库进阶篇
Xss target drone training [success when pop-up window is realized]
MySQL数据库(基础)
SWM32 Series Tutorial 6 - Systick and PWM
MySQL数据库约束,表的设计
【愚公系列】2022年07月 Go教学课程 016-运算符之逻辑运算符和其他运算符
Gabor filter study notes
什么是Promise?Promise的原理是什么?Promise怎么用?
Restricted character bypass
埃拉托斯特尼筛法