结果图
- em是相对于父元素的字体大小来说的,rem(root em)是相对于html元素来说的,
- less编译器的使用 css样式写在less文件里 自动生成css文件 后续方便修改 和rem布局搭配使用
- rem实际开发啊适配方案1
元素大小的取值:(1)页面元素的rem值=页面元素之(px)/(屏幕宽度/划分的份数)
(2)屏幕宽度/划分的份数就是html font-size的大小
每个rem布局+less编译器使用的通用步骤
第一步
第二步
// 设置常见的屏幕尺寸 修改里面的html文字大小
// 一定要写到最上面,如果是pc端打开,就是显示这个
html {
font-size: 50px;
}
//我们此次定义划分的份数是15
// 把划分的数字定义成一个变量,以后要改的话就方便了
@no:15 ;
//320
@media screen and (min-width:320px) {
html{
// 运算符左右两侧必须加空格
font-size: (320px / @no);
}
}
// 360
@media screen and (min-width:360px) {
html{
font-size: (360px / @no);
}
}
// 375 iphone 678
@media screen and (min-width: 375px) {
html {
font-size: (375px / @no);
}
}
// 384
@media screen and (min-width: 384px) {
html {
font-size: (384px / @no);
}
// 400
@media screen and (min-width: 400px) {
html {
font-size: (400px / @no);
}
}
// 414
@media screen and (min-width: 414px) {
html {
font-size: (414px / @no);
}
}
// 424
@media screen and (min-width: 424px) {
html {
font-size: (424px / @no);
}
}
// 480
@media screen and (min-width: 480px) {
html {
font-size: (480px / @no);
}
}
// 540
@media screen and (min-width: 540px) {
html {
font-size: (540px / @no);
}
}
// 720
@media screen and (min-width: 720px) {
html {
font-size: (720px / @no);
}
}
// 750
@media screen and (min-width: 750px) {
html {
font-size: 750px / @no;
}
}
第三步
第四部 body样式
body{
min-width: 320px;
// 模仿苏宁页面,不写最大宽度
// 750/15
width: 15rem;
margin: 0 auto;
line-height: 1.5;
font-family: Arial, Helvetica;
background-color: #f2f2f2;
}
代码
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- 视口标签 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no,maximum-scale=1.0,minimum-scale=1.0">
<title>Document</title>
<!-- 移动端的初始化样式 -->
<link rel="stylesheet" href="css/normalize.css">
<!-- 引入常见屏幕尺寸写入公共样式,列表页详情页等等每个页面都要用 -->
<!-- 不需要引入common.css文件 -->
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<!-- 顶部搜索框 -->
<div class="search-content">
<a href="" class="classify"></a>
<div class="search">
<form action="">
<!-- -->
<input type="text" value="厨卫包暖气">
</form>
</div>
<a href="" class="login">登录</a>
</div>
<!-- banner部分 -->
<div class="banner">
<img src="upload/banner.gif" alt="">
</div>
<!-- 广告部分 -->
<div class="ad">
<a href="" id=""><img src="upload/ad1.gif" alt=""></a>
<a href="" id=""><img src="upload/ad2.gif" alt=""></a>
<a href="" id=""><img src="upload/ad3.gif" alt=""></a>
</div>
<!-- nav模块 -->
<nav>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
<a href="#">
<img src="upload/nav1.png" alt="">
<span>爆款手机</span>
</a>
</nav>
</body>
</html>
css
// 首页的样式less文件
@import "common";
// @import 导入的意思 可以把一个样式文件导入到另外一个样式文件里面
// link 是把一个 样式文件引入到 html页面里面
body{
min-width: 320px;
// 模仿苏宁页面,不写最大宽度
// 750/15
width: 15rem;
margin: 0 auto;
line-height: 1.5;
font-family: Arial, Helvetica;
background-color: #f2f2f2;
}
// 固定定位要有宽度,宽度和父盒子一样,可以根据屏幕自适应,高度也是
// 定义一个变量50修改起来方便
@baseFont:50;
.search-content{
// 子元素用了flex布局,行内元素块元素都默认一行显示
display: flex;
// 固定定位,相对于浏览器
position: fixed;
top: 0;
left:50%;
transform: translateX(-50%);
width: 15rem;
// 88px,页面元素算法:页面元素px/html字体大小
// 注意除法运算在less里面要用括号括起来
height: (88rem / @baseFont);
background-color: #ffc001;
// 子盒子写在父盒子里,是less编辑器的做法
.classify{
width: (44rem / @baseFont);
height: (70rem / @baseFont);
// margin: 11px 25px 7px 24px;转化成rem
margin: (11rem / @baseFont) (25rem / @baseFont) (7rem / @baseFont) (24rem / @baseFont);
// 背景图,盒子缩放自适应,图片缩放自适应
background:url(../images/classify.png) no-repeat;
//背景缩放,跟着父亲
background-size: (44rem / @baseFont) (70rem / @baseFont);
}
.search {
flex: 1;
input {
// 这个输入框不会盖住登录
box-sizing: border-box;
// 去掉默认蓝色边框
outline: none;
width: 100%;
// 去掉边框
border: 0;
height: (66rem / @baseFont);
// 高度的一半
border-radius:(33rem / @baseFont) ;
background-color: #fff2cc;
margin-top: (12rem / @baseFont);
padding-left: (55rem / @baseFont);
font-size: 25px;
color:#757575;
}
}
.login{
// 水平垂直居中
width: (75rem / @baseFont);
height: (70rem / @baseFont);
line-height:(70rem / @baseFont) ;
margin: (10rem / @baseFont);
// font-size: 25px;
font-size: (25rem / @baseFont);
text-emphasis: center;
color: #fff;
}
}
.banner{
width: (750rem / @baseFont);
height: (368rem / @baseFont);
// 图片根据屏幕大小自适应
img {
width: 100%;
height: 100%;
}
}
.ad{
// 子元素三张图片链接各占一份
display: flex;
a{
flex: 1;
img{
width: 100%;
height: 100%;
}
}
}
// nav
nav{
width:(750rem / @baseFont) ;
a{
float: left;
width:(150rem / @baseFont) ;
height:(140rem / @baseFont) ;
img{
// 转成块级元素,才可以左右居中对齐
display: block;
width:(82rem / @baseFont) ;
height:(82rem / @baseFont) ;
margin: (10rem / @baseFont) auto 0;
}
span {
font-size: (25rem / @baseFont);
color: #333;
}
}
}