百分比布局是一种等比例缩放的布局方式,也是移动Web开发中比较常见的布局方式。在CSS代码中需要使用百分比来设置盒子的宽高。
例如,把盒子的宽度设置成百分比,网页就会根据浏览器的宽度和屏幕的大小来自动调整显示效果。
而且一般的情况下,我们都会用 max-width 和 min-width 做相关的宽度限制以防止过度的拉伸。以下是子元素设置百分比的参照值:
子元素 | 参照值 |
---|---|
width/height | 基于子元素的直接父元素,width相对于父元素的width,height相对于父元素的height |
top/bottom 和 left/right | 相对于直接非static定位的父元素的height/width |
padding/margin | 不论是垂直方向或者是水平方向,都相对于直接父亲元素的width,与父元素的height无关。 |
border-radius | 相对于自身的宽度 |
原理简单,兼容性好,且掌握好参照值之后,在一定范围内基本不会出现适配的问题
弹性盒布局是 CSS3 中的一种新布局模式,可以轻松地创建响应式网站布局。弹性盒布局为盒模块增加了灵活性,可以让我们告别浮动(float),完美地实现垂直居中。目前它得到几乎所有主流浏览器的支持。
弹性布局,用来为盒装模型提供最大的灵活性,任何一个容器都可以指定为flex布局。采用flex布局的元素,被称为容器,它的所有子元素会自动成为容器成员,称为flex项目
对于低端老年机,不支持 flex 属性的不太友好
Rem 适配方案一般采取媒体查询来实现响应式布局设计。
Rem适配方案使用媒体查询可以根据不同的设备按比例设置页面的字体大小,然后在页面中使用rem单位,可以通过修改html里面的文字大小来改变页面中的元素的大小从而进行整体控制。
rem是 CSS 3 中新增的一种相对长度单位。当使用 rem 单位时,根节点 <html>
的字体大小(font-size)决定了rem的尺寸。
rem 是相对长度单位,rem方案中的样式设计为相对于根元素 font-size 计算值的倍数。根据 屏幕宽度 设置 html 标签的 font-size,在布局时使用 rem 单位布局,达到自适应的目的,是 弹性布局 的一种实现方式。
通过获取屏幕实际宽度动态的设置 html 的字体大小,1rem = html 的字体大小像素(font-size)。
(function (doc, win) {
let docEl = doc.documentElement, // 获取html
resizeEvt = "orientationchange" in window ? "orientationchange" : "resize",
width = 750, // 设计稿宽,用时只需要修改这一处
recalc = function () {
const clientWidth = docEl.clientWidth; // 获取设备尺寸
if (!clientWidth) return; // 如果没有值,回去
if (clientWidth > width) {
// 如果超过设计稿宽度,就给一个固定值
docEl.style.fontSize = "100px";
docEl.style.width = width + "px";
docEl.style.margin = "0 auto";
} else {
docEl.style.fontSize = 100 * (clientWidth / width) + "px";
docEl.style.width = "";
docEl.style.margin = "";
}
};
if (!doc.addEventListener) return; // 如果没有这个方法,中断函数
win.addEventListener(resizeEvt, recalc, false); // 改变大小时调整一下
doc.addEventListener("DOMContentLoaded", recalc, false); // 加载完成时调整
})(document, window);
// 使用时:1rem = 设计稿的宽度 / 100
<script>
(function (doc, win) {
let docEl = doc.documentElement, // 获取html
resizeEvt = "orientationchange" in window ? "orientationchange" : "resize",
width = 750, // 设计稿宽,用时只需要修改这一处
recalc = function () {
const clientWidth = docEl.clientWidth; // 获取设备尺寸
if (!clientWidth) return; // 如果没有值,回去
if (clientWidth > width) {
// 如果超过设计稿宽度,就给一个固定值
docEl.style.fontSize = "100px";
} else {
docEl.style.fontSize = 100 * (clientWidth / width) + "px";
}
};
if (!doc.addEventListener) return; // 如果没有这个方法,回去
win.addEventListener(resizeEvt, recalc, false); // 改变大小时调整一下
doc.addEventListener("DOMContentLoaded", recalc, false); // 加载完成时调整
})(document, window);
// 使用时:1rem = 设计稿的宽度 / 100
</script>
<script>!function(e,t){var n=t.documentElement,d=e.devicePixelRatio||1;function i(){var e=n.clientWidth/3.75;n.style.fontSize=e+"px"}if(function e(){t.body?t.body.style.fontSize="16px":t.addEventListener("DOMContentLoaded",e)}(),i(),e.addEventListener("resize",i),e.addEventListener("pageshow",function(e){e.persisted&&i()}),2<=d){var o=t.createElement("body"),a=t.createElement("div");a.style.border=".5px solid transparent",o.appendChild(a),n.appendChild(o),1===a.offsetHeight&&n.classList.add("hairlines"),n.removeChild(o)}}(window,document)</script>
vh,vw 主要是 CSS3 中新出的为了进行移动适配的长度单位:主要是相对于视口viewport的百分比。
视口是浏览器中用于呈现网页的区域,移动端的视口通常指的是布局视口。
计算方式实际上与百分基本一致,不过多了一个优势,就是参照系没有百分比那么复杂
100vw = 750px(设计图的像素)
0.13333333333333333333vw = 1px
/* 设计图中一个 48px*35px 大小的元素 */
box1{
width:48px;
height:35px;
}
/* 用 vh/vw 布局 */
box1{
width:6.4vw;
height:4.667vw;
}
html{
font-size:5.3333vw
}
.box1{
width:1.2rem;
height:0.875rem;
}
html{
font-size:13.333vw
}
.box1{
width:0.48rem;
height:0.35rem;
}
html {
font-size: 50px
}
body {
font-size: 24px
}
@media screen and (min-width:320px) {
html {
font-size: 21.33px
}
body {
font-size: 12px
}
}
@media screen and (min-width:360px) {
html {
font-size: 24px
}
body {
font-size: 12px
}
}
@media screen and (min-width:375px) {
html {
font-size: 25px
}
body {
font-size: 12px
}
}
@media screen and (min-width:384px) {
html {
font-size: 25.6px
}
body {
font-size: 14px
}
}
@media screen and (min-width:400px) {
html {
font-size: 26.67px
}
body {
font-size: 14px
}
}
@media screen and (min-width:414px) {
html {
font-size: 27.6px
}
body {
font-size: 14px
}
}
@media screen and (min-width:424px) {
html {
font-size: 28.27px
}
body {
font-size: 14px
}
}
@media screen and (min-width:480px) {
html {
font-size: 32px
}
body {
font-size: 15.36px
}
}
@media screen and (min-width:540px) {
html {
font-size: 36px
}
body {
font-size: 17.28px
}
}
@media screen and (min-width:720px) {
html {
font-size: 48px
}
body {
font-size: 23.04px
}
}
@media screen and (min-width:750px) {
html {
font-size: 50px
}
body {
font-size: 24px
}
}