一、rem 基础

1. rem 单位

em :

相对于父元素的字体大小来说的

<div>
<p></p>
</div>
div {
font-size: 12px;
}
p {
width: 10em;
height: 10em;
background-color: pink;
}

rem:

相对于 html 元素字体大小来说的

    html {
font-size: 16px;
}
div {
font-size: 12px;
}
p {
/*width: 10em;*/
/*height: 10em;*/
width: 10rem;
height: 10rem;
background-color: pink;
}

总结:rem的优点就是可以通过修改html里面的文字大小来改变页面中元素的大小,页面中元素大小可以控制

二、媒体查询

2.1 什么是媒体查询

2.2 语法规范

1. mediatype 媒体类型

2. 关键字

3. 媒体特性



栗子:

<style>
/*这句话的意思就是:在我们屏幕上,并且 最大的宽度是 800像素 设置我们想要的样式*/
/*max-width 小于等于800*/
/*媒体查询可以根据不同的屏幕尺寸在改变不同的样式*/
@media screen and (max-width: 900px){
body{
background-color: pink;
}
}
@media screen and (max-width: 600px) {
body{
background-color: purple;
}
}
</style>



栗子2:根据页面宽度改变页面背景颜色



<style>
/*1. 小于 540 px 页面的背景变为蓝色*/
@media screen and (max-width: 539px){
body{
background-color: blue;
}
}
/*2. 540~970 页面的背景变成绿色*/
/*@media screen and (min-width: 540px) and (max-width: 969px) {*/
/* body {*/
/* background-color: green;*/
/* }*/
/*}*/
/*简写为:*/
@media screen and (min-width: 540px){
body{
background-color: green;
}
}
/*3.大于等于 970 页面背景变成红色 */
@media screen and (min-width:970px){
body{
background-color: red;
}
}
</style>

2.3 媒体查询 + rem 实现元素动态变化



栗子:

<style>
*{
margin: 0;
padding: 0;
}
@media screen and (min-width: 320px) {
html{
font-size: 50px;
}
} @media screen and (min-width: 640px){
html{
font-size: 100px;
}
}
.top {
height: 1rem;
font-size: 0.5rem;
background-color: deeppink;
color: #ffffff;
text-align: center;
line-height: 1rem;
}
</style>
<body>
<div class="top">购物车</div>
</body>

2.4 引入资源

1 .语法规范

栗子:

需求:当我们屏幕大于等于 640px 以上的,我们让 div 一行显示 2 个

当我们屏幕小于 640 我们让div一行显示一个

建议媒体查询最好的方法是从小到大

<link rel="stylesheet" href="style320.css" media="screen and (min-width:320px)">
<link rel="stylesheet" href="style640.css" media="screen and (min-width:640px)">
<body>
<div>1</div>
<div>2</div>
</body>

style320.css

div {
width: 100%;
height: 100px;
}
div:nth-child(1){
background-color: pink;
}
div:nth-child(2){
background-color: purple;
}

style640.css

div {
width: 50%;
height: 100px;
float: left;
}
div:nth-child(1){
background-color: pink;
}
div:nth-child(2){
background-color: purple;
}

总结:引入资源就是 针对不同的屏幕尺寸 调用不同的 CSS 文件

三、Less 基础

3.1 Less作用(维护 css 的弊端)

3.2 Less介绍

3.3 Less 使用

3.4 Less 变量



栗子:

//定义一个颜色为粉色的变量
@color:pink;
//定义一个 字体为141像素的变量
@font14:14px;
body {
background-color: @color;
}
div {
background-color: @color;
font-size: @font14;
}
a{
font-size: @font14;
}

注意:

错误的变量名 @1color @color~#

变量名区分大小写 @color 和 @Color 是两个不同的变量

3.5 Less 编译



vocode Less插件

Easy LESS 插件用来把 less 文件编译成 css 文件

  1. 安装完毕插件,重新下载下 vscode

  1. 只要保存一下 Less 文件,会自动生成CSS 文件

  1. 当修改 my.less 保存的时候 ,my.css 会自动的去修改

栗子:

my.less

//定义一个颜色为粉色的变量
@color:pink;
//定义一个 字体为24像素的变量
@font14:24px;
//错误的变量名 @1color @color~#
//变量名区分大小写 @color 和 @Color 是两个不同的变量
body {
background-color: @color;
}
div {
background-color: @color;
font-size: @font14;
}

21.less的使用.html

<link rel="stylesheet" href="my.css">
<body>
<div>谁还不是一个小仙女呢!</div>
</body>

3.6 Less 嵌套

  1. 选择器的嵌套

栗子:

less文件

.header {
width: 200px;
height: 200px;
background-color:pink;
// less嵌套 子元素的样式直接写到父元素里面即可
a {
color: red;
}
}

被编译成css文件如下

.header {
width: 200px;
height: 200px;
background-color: pink;
}
.header a {
color: red;
}
  1. 交集、伪类、伪元素选择器的嵌套

伪类栗子:

less文件

.header {
width: 200px;
height: 200px;
background-color:pink;
a {
color: red;
&:hover {
color: blue
}
}
}

被编译成css文件如下

.header {
width: 200px;
height: 200px;
background-color: pink;
}
.header a {
color: red;
}
.header a:hover {
color: blue;
}

伪元素栗子:

less 文件

.nav {
.logo{
color: red;
}
&::before{
content: "";
}
}

被编译成css 文件如下

.nav .logo {
color: red;
}
.nav::before {
content: "";
}

3.7 Less 运算



栗子1:

less文件

@border:5px + 5;
div {
width: 200px - 50;
height: 200px * 2;
border:@border solid red
}
img {
width:82 / 50rem;
height: 82 / 50rem;
}

css 文件

div {
width: 150px;
height: 400px;
border: 10px solid red;
}
img {
width: 1.64rem;
height: 1.64rem;
}

栗子2:

less文件

@baseFont:50px;
html{
font-size: @baseFont;
}
img {
width: 82rem / @baseFont;
height: 82rem / @baseFont;
}

css 文件

html {
font-size: 50px;
}
img {
width: 1.64rem;
height: 1.64rem;
}

总结:

1.运算符的左右两侧必须用空格隔开

2. 两个数参与运算,如果只有一个数有单位,则最后的结果就以这个单位为准

3. 两个数参与运算,如果两个数都有单位,并且单位不一致,则最后的结果以第一个数的单位为准

注意:

颜色也可以参与运算,比如

less文件

background-color:#666 - #222

css 文件:

background-color:#444

四、 rem 适配方案



4.1 rem 实际开发适配方案

4.2 rem 适配方案技术使用(市场主流)

4.3 rem 实际开发适配方案一

rem + 媒体查询 + less

1. 设计稿常见尺寸宽度

2. 动态设置html 标签 font-size 大小

栗子:

<style>
*{
margin: 0;
padding: 0;
}
@media screen and (min-width:320px){
html{
font-size: 22px;
}
}
@media screen and (min-width:750px){
html{
font-size: 50px;
}
}
div {
width: 2rem;
height: 2rem;
background-color:pink;
}
</style>
</head>
<body>
<div></div>
</body>

大屏下宽和高是 100px * 100px的

小屏下宽和高是 44px * 44px的

达到等比例缩放的效果

3. 元素大小取值的计算方法

总结:

原型大小的盒子 就是页面元素值: 100px * 100px

屏幕宽度:750px

划分的份数:15等份

屏幕宽度 / 划分的份数 就是 html font-size 的大小 = 750 / 15 = 50

页面元素的 rem值 : 100 / 50 = 2rem

五、苏宁首页案例制作

https://www.cnblogs.com/Chinatsu/p/14130701.html

六、rem 适配方案二

6.1 简洁高效的 rem 适配方案 flexible.js

6.2 使用 rem 适配方案二制作苏宁移动端首页

https://www.cnblogs.com/Chinatsu/p/14130714.html

最新文章

  1. jQuery弹出窗口浏览图片
  2. 鼠标模拟点击a标签
  3. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873
  4. Unreal Engine 虚幻引擎宣布对开发者免费
  5. 博客转移到 海胖网 http://haipz.com/ 希望你能支持我们!
  6. SpringMVC(四)
  7. Boot Petalinux Project Using a remote system
  8. cf B Three matrices
  9. python之6-4装饰器.md
  10. 基于visual Studio2013解决面试题之1007鸡蛋和篮子
  11. C++习题 对象转换
  12. soapUI的安装及破解
  13. vs2017使用问题
  14. 用Photoshop扭曲滤镜工具打造旋转的光束效果
  15. osds have slow requests
  16. How do I fix a “Unknown configuration key `foreign-architecture&#39; found in your `dpkg&#39; configuration files.” error?
  17. Ignite初探
  18. Webpack + React 开发 01 HelloWorld
  19. C++学习之路(十一):C++的初始化列表
  20. [转贴]SSL工作原理

热门文章

  1. Python基础指面向对象:2、动静态方法
  2. Cesium-03:洪水淹没
  3. Git同步操作
  4. 《浅谈亚 log 数据结构在 OI 中的应用》阅读随笔
  5. 【实时数仓】Day05-ClickHouse:入门、安装、数据类型、表引擎、SQL操作、副本、分片集群
  6. org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents
  7. layui table 表头抖动
  8. Linux通过脚本实现多台主机统一部署
  9. JavaScript入门⑧-事件总结大全
  10. 命令指定IP端口号