目录
相关知识引入
-
像素
- 物理像素:真实存在,出场已设置,指的是屏幕的分辨率。
- 物理像素比:在电脑上进行前端设计时,1个px能显示在手机上的物理像素点个数。
例如:iPhone 8的物理像素比为2,即1px为手机上的两倍大小。
-
二倍图
- 引入原因:对于设计时的一张50*50(px)图片,在iPhone 8中打开会使其放大两倍,造成图片模糊。
- 解决方法:找一张100*100(px)的图片,将其设置大小为50*50(px),使其在手机端放大为原图。
-
视口 —— meta视口标签
1.语法:
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, maximum-scale=1.0, minimum-scale=1.0 ">
注意:其内容要写在<head>中!
2.属性解释:
(1)width: 设置的为视口的宽度,可更改device-width的特殊值;
(2)initial-scale: 表示初始缩放比;
(3)user-scalable: 用户是否可以缩放,取值:yes / no == 1 / 0 。
-
初始化及特殊样式
- 自定义样式前的清除:-webkit-appearance: none ;
- 禁止长按页面弹出菜单:-webkit-touch-callout: none ;
- 清除点击高亮:-webkit-tap-highlight-color:transparent ;
- 初始化:放最后了 — —
相关移动布局
-
流式布局
1.内含:相当于百分比布局,给页面设置百分比进行布局。
2.相关属性:
max-width: 最大宽度; min-width: 最小宽度;
-
flex弹性布局
—— ——任何盒子均可设置为弹性盒子
1.原理:通过给父元素设置属性 display:flex ,来控制子元素的位置及排列顺序。
注意:父元素为弹性盒子时,子元素的float、clear、vertical-align属性将失效。
2.常见的父项属性(前提要给父元素设置 display:flex )
- flex-direction:主轴方向,可取值:row(从左到右);column(从上到下);row / column -reverse(左右 / 上下颠倒)
- justify-content:主轴子元素排列方式,可取值:flex-start(从左到右) ;flex-end(从右到左) ;center(居中); space-around(平分剩余空间); space-between(两边贴边,再平分剩余空间)
- flex-wrap:子元素是否换行,可取值:nowarp(默认排在一条轴线上,不换行);warp(进行换行)
align-items:在单行情况下,设置侧轴子元素排列方式,可取值:stretch(拉伸);flex-start(从上到下);flex-end(从下到上);center(居中)
align-content:在多行情况下,设置侧轴子元素排列方式,可取值:flex-start(从上到下); flex-end(从下到上); center(居中); space-around(平分剩余空间); space-between(两边贴边,再平分剩余空间) ;stretch(拉伸)
flex-flow:复合属性;简写:flex-flow: flex-direction flex-wrap;
3.常见的子项属性
1.flex属性:定义子项目分配的剩余空间,用flex表示划分的份数。可取:个数或百分比
- 当flex分别取1,2,1 —— 形成三栏自适应布局;
- 当flex分别取50%,50%,50%,50% —— 形成多行自适应布局;
2.align-self属性:控制子项目在侧轴上的排列方式,可覆盖父元素 align-items 属性。
3.order属性:定义项目顺序,数值越小,排列越靠前,默认为0。
3.应用
1.使盒子居中:设置justify-content:center; align-items:center;
2.三栏布局:(1)3个div盒子,两边的定宽,中间设置flex:1;
(2)父项盒子设置display:flex;
3.形成图片下文字效果:(1)设置flex-direction:column;
(2)让侧轴居中对齐:align-items:center;
-
rem适配布局
1.rem单位
em:相对于父元素的字体大小 ;
rem:相对与HTML的字体大小 ;
2.媒体查询
1.意义:@media 可以根据不同屏幕尺寸设置不同样式 ;
2.语法 :@media 媒体类型 关键字 ( 媒体特性 ) {
Css-Code; }
3.规范:
(1)媒体类型:all(所有设备)、print(打印设备)、screen(智能屏)
(2)关键字:and(多个多媒体)、not(排除某个多媒体)、only(指定多媒体)
(3)媒体特性:要带括号,可取值:min-width、max-width、width。
4.注意:有层叠效果,写时通常按照从小到大来写。
5.引入资源方式:在<link>中判断不同尺寸,从而引入不同资源,语法如下
<link rel="stylesheet" media="mediatype and\not\only (media feature)" href="mystylesheet.css" >
3.Less语言
- 意义:其为CSS扩展语言,也为其预处理器,引入了变量、函数、运算等功能,扩展了CSS的动态特性。
- 建立:文件后缀名要改为Less
- 重点内容:
(1)Less变量
格式:@变量名:值;
要求:不包含特殊字符,且不以数字开头,大小写有区别。
(2)Less嵌套
- 子元素直接写在父元素{ }内部;
- 有伪类、伪元素选择器,在其前加&;
(3)Less运算
- 运算符中间以空格隔开,例如:1px + 5;
- 如果有两个不同的单位运算,结果取第一个值的;
- 除法要写( );
4.rem实际开发中的适配方案
1.根据不同宽度设置html的font-size大小 —— —— 媒体查询;
计算:屏幕宽度(width) / 划分份数(通常为15/10)= font-size(px)大小;
2.按照元素的宽高取值,换算成rem为单位的值;
计算:body=划分份数 * rem;
各元素=页面元素大小 / html 的 font-size ;
3.实现rem适配布局,达到等比例缩放效果 ———— rem + 媒体查询 + Less
-
vw与vh布局
1.引入:vw —— ——视口宽度单位;
vh —— ——视口高度单位;
2.计算结果:1vw = 1 / 100 视口宽度;
3.区分:
(1)百分比是相对父元素来说的,占起的百分比空间;
(2)vw 与 vh 是相对于视口来说的;
-
移动端初始化
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
/* Document
========================================================================== */
/**
* 1. Correct the line height in all browsers.
* 2. Prevent adjustments of font size after orientation changes in iOS.
*/
html {
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers.
*/
body {
margin: 0;
}
/**
* Render the `main` element consistently in IE.
*/
main {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
pre {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* Remove the gray background on active links in IE 10.
*/
a {
background-color: transparent;
}
/**
* 1. Remove the bottom border in Chrome 57-
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
*/
b,
strong {
font-weight: bolder;
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
code,
kbd,
samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font size in all browsers.
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/* Embedded content
========================================================================== */
/**
* Remove the border on images inside links in IE 10.
*/
img {
border-style: none;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers.
* 2. Remove the margin in Firefox and Safari.
*/
button,
input,
optgroup,
select,
textarea {
font-family: inherit; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
* Show the overflow in IE.
* 1. Show the overflow in Edge.
*/
button,
input { /* 1 */
overflow: visible;
}
/**
* Remove the inheritance of text transform in Edge, Firefox, and IE.
* 1. Remove the inheritance of text transform in Firefox.
*/
button,
select { /* 1 */
text-transform: none;
}
/**
* Correct the inability to style clickable types in iOS and Safari.
*/
button,
[type="button"],
[type="reset"],
[type="submit"] {
-webkit-appearance: button;
}
/**
* Remove the inner border and padding in Firefox.
*/
button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
* Restore the focus styles unset by the previous rule.
*/
button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
* Correct the padding in Firefox.
*/
fieldset {
padding: 0.35em 0.75em 0.625em;
}
/**
* 1. Correct the text wrapping in Edge and IE.
* 2. Correct the color inheritance from `fieldset` elements in IE.
* 3. Remove the padding so developers are not caught out when they zero out
* `fieldset` elements in all browsers.
*/
legend {
box-sizing: border-box; /* 1 */
color: inherit; /* 2 */
display: table; /* 1 */
max-width: 100%; /* 1 */
padding: 0; /* 3 */
white-space: normal; /* 1 */
}
/**
* Add the correct vertical alignment in Chrome, Firefox, and Opera.
*/
progress {
vertical-align: baseline;
}
/**
* Remove the default vertical scrollbar in IE 10+.
*/
textarea {
overflow: auto;
}
/**
* 1. Add the correct box sizing in IE 10.
* 2. Remove the padding in IE 10.
*/
[type="checkbox"],
[type="radio"] {
box-sizing: border-box; /* 1 */
padding: 0; /* 2 */
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
*/
[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
height: auto;
}
/**
* 1. Correct the odd appearance in Chrome and Safari.
* 2. Correct the outline style in Safari.
*/
[type="search"] {
-webkit-appearance: textfield; /* 1 */
outline-offset: -2px; /* 2 */
}
/**
* Remove the inner padding in Chrome and Safari on macOS.
*/
[type="search"]::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
* 1. Correct the inability to style clickable types in iOS and Safari.
* 2. Change font properties to `inherit` in Safari.
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/* Interactive
========================================================================== */
/*
* Add the correct display in Edge, IE 10+, and Firefox.
*/
details {
display: block;
}
/*
* Add the correct display in all browsers.
*/
summary {
display: list-item;
}
/* Misc
========================================================================== */
/**
* Add the correct display in IE 10+.
*/
template {
display: none;
}
/**
* Add the correct display in IE 10.
*/
[hidden] {
display: none;
}