目录
三、CSS display的block、inline和inline-block的区别
五、CSS display: none;与visibility: hidden;的区别
| 选择器 | 格式 | 优先级权重 |
|---|---|---|
| id选择器 | #id | 100 |
| 类选择器 | .box | 10 |
| 属性选择器 | input[type="text"] | 10 |
| 伪类选择器 | p:first-child | 10 |
| 标签选择器 | div | 1 |
| 伪元素选择器 | p::after | 1 |
| 相邻兄弟选择器 | h1+p | 0 |
| 通用兄弟选择器 | h1~p | 0 |
| 子代选择器 | ul>li | 0 |
| 后代选择器 | li a | 0 |
| 通配符选择器 | * | 0 |
优先级
!important > 内联样式 > id选择器 > 类、伪类、属性选择器 > 标签、伪元素选择器
权重:
!important: 10000
内联样式: 1000
id选择器: 100
类、伪类、属性选择器: 10
标签、伪元素选择器: 1
相邻兄弟选择器(+)、通用兄弟选择器(~)、子选择器(>)、后代选择器、通配符选择器(*): 0
| 属性值 | 作用 |
|---|---|
| none | 元素不显示,并且会从文档流中移除。 |
| block | 块元素。默认宽度为父元素宽度,可设置宽高,换行显示。 |
| inline | 行内元素。默认宽度为内容宽度,不可设置宽高,同行显示。 |
| inline-block | 行内块元素。默认宽度为内容宽度,可设置宽高,同行显示。 |
| list-item | 像块元素一样显示,并添加样式列表标记。 |
| table | 元素作为块级表格来显示。 |
| inherit | 规定应该从父元素继承display属性的值。 |
(1)在渲染树中:
(2)是否继承属性
(3)修改常规文档流中元素的display通常会造成文档的重排,但是修改visibility属性只会造成本元素的重绘。
(4)如果使用读屏器,设置display: none;的内容不会被读取,设置的visibility: hidden;内容会被读取。
CSS 中的盒子模型包括 IE 盒子模型和标准的 W3C 盒子模型


盒模型是由四个部分组成:margin、border、padding和content。
标准盒模型和IE盒模型的区别在于设置width和height时,所对应的范围不同。
可以通过修改元素的border-sizing属性来改变元素的盒类型
CSS3选择器
CSS3边框与圆角
CSS3背景与渐变
新的背景属性:background-clip、background-origin、background-size
渐变:线性渐变(background-image: linear-gradient(direction, color-stop1, color-stop2, ...);)、径向渐变(background-image: radial-gradient(shape size at position, start-color, ..., last-color);)
CSS3文本效果
新的文本属性:text-shadow、box-shadow、text-overflow、word-wrap、word-break
CSS3转换
CSS3过渡
过渡属性:transition、transition-property、transition-duration、transition-timing-function、 transition-delay
CSS3动画
动画属性:@keyframes、animation、animation-name、animation-duration、
animation-timing-function、animation-fill-mode、animation-delay、
animation-iteration-count、animation-direction、animation-play-state
CSS3多列
多列属性:column-count、column-gap、column-rule-style、column-rule-width、
column-rule-color、column-rule、column-span、column-width
- overflow: hidden; // 溢出隐藏
- text-overflow: ellipsis; // 溢出用省略号显示
- white-space: nowrap; // 规定段落中的文本不进行换行
- overflow: hidden; // 溢出隐藏
- text-overflow: ellipsis; // 溢出用省略号显示
-
- // 注意:下面三个属性是CSS3的属性,不是所有浏览器都可以兼容,所以要在前面加上-webkit-来兼容部分浏览器
- display: -webkit-box; // 作为弹性伸缩盒子模型显示
- -webkit-box-orient: vertical; // 设置伸缩盒子的子元素排列方式: 从上到下垂直排列
- -webkit-line-clamp: 3; // 显示的行数
两栏布局:左边一栏宽度固定,右边一栏宽度自适应。
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- height: 100px;
- }
-
- .left {
- float: left;
- width: 200px;
- background: pink;
- }
-
- .right {
- margin-left: 200px;
- width: auto;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .left {
- width: 100px;
- height: 200px;
- float: left;
- background: pink;
- }
-
- .right {
- height: 200px;
- overflow: hidden;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- display: flex;
- height: 100px;
- }
-
- .left {
- width: 200px;
- background: pink;
- }
-
- .right {
- flex: 1;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- position: relative;
- height: 100px;
- }
-
- .left {
- width: 200px;
- height: 100px;
- position: absolute;
- background: pink;
- }
-
- .right {
- margin-left: 200px;
- height: 100px;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- position: relative;
- height: 100px;
- }
-
- .left {
- width: 200px;
- background: pink;
- }
-
- .right {
- position: absolute;
- top: 0;
- bottom: 0;
- right: 0;
- left: 200px;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
三栏布局:左右两栏宽度固定,中间自适应布局。
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- position: relative;
- height: 100px;
- }
-
- .left {
- width: 200px;
- height: 100px;
- position: absolute;
- top: 0;
- left: 0;
- background: pink;
- }
-
- .right {
- width: 200px;
- height: 100px;
- position: absolute;
- top: 0;
- right: 0;
- background: blanchedalmond;
- }
-
- .center {
- margin-left: 200px;
- margin-right: 200px;
- height: 100px;
- background-color: honeydew;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="center">
- center
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- display: flex;
- height: 100px;
- }
-
- .left {
- width: 200px;
- background: pink;
- }
-
- .right {
- width: 200px;
- background: blanchedalmond;
- }
-
- .center {
- flex: 1;
- background-color: honeydew;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="center">
- center
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- height: 100px;
- }
-
- .left {
- float: left;
- width: 200px;
- height: 100px;
- background: pink;
- }
-
- .right {
- float: right;
- width: 200px;
- height: 100px;
- background: blanchedalmond;
- }
-
- .center {
- margin-left: 200px;
- margin-right: 200px;
- height: 100px;
- background-color: honeydew;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- <div class="center">
- center
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- padding-left: 200px;
- padding-right: 200px;
- height: 100px;
- }
-
- .left {
- float: left;
- margin-left: -100%;
- position: relative;
- left: -200px;
- width: 200px;
- height: 100px;
- background: pink;
- }
-
- .right {
- float: left;
- margin-left: -200px;
- position: relative;
- left: 200px;
- width: 200px;
- height: 100px;
- background: blanchedalmond;
- }
-
- .center {
- float: left;
- width: 100%;
- height: 100px;
- background-color: honeydew;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="center">
- center
- div>
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .left {
- float: left;
- margin-left: -100%;
- width: 200px;
- height: 100px;
- background: pink;
- }
-
- .right {
- float: left;
- margin-left: -200px;
- width: 200px;
- height: 100px;
- background: blanchedalmond;
- }
-
- .center {
- float: left;
- width: 100%;
- height: 100px;
- background-color: honeydew;
- }
-
- .center-main {
- margin-left: 200px;
- margin-right: 200px;
- height: 100px;
- background-color: cadetblue;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="center">
- <div class="center-main">centerdiv>
- div>
- <div class="left">
- left
- div>
- <div class="right">
- right
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- width: 100%;
- height: 100vh;
- position: relative;
- background-color: pink;
- }
-
- .content {
- width: 500px;
- height: 500px;
- position: absolute;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="content">contentdiv>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- width: 100%;
- height: 100vh;
- position: relative;
- background-color: pink;
- }
-
- .content {
- width: 500px;
- height: 500px;
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- margin: auto;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="content">contentdiv>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- width: 100%;
- height: 100vh;
- position: relative;
- background-color: pink;
- }
-
- .content {
- width: 500px;
- height: 500px;
- position: absolute;
- top: 50%;
- left: 50%;
- margin-top: -250px; /* 自身高度的一半 */
- margin-left: -250px; /* 自身宽度的一半 */
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="content">contentdiv>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- width: 100%;
- height: 100vh;
- display: flex;
- align-items: center;
- justify-content: center;
- background-color: pink;
- }
-
- .content {
- width: 500px;
- height: 500px;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="content">contentdiv>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .outer {
- width: 100%;
- height: 100vh;
- display: flex;
- background-color: pink;
- }
-
- .content {
- width: 500px;
- height: 500px;
- margin: auto;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="outer">
- <div class="content">contentdiv>
- div>
- div>
- body>
-
- html>
Flex是FlexibleBox的缩写,意为“弹式布局”,用来为盒装模型提供最大的灵活性。
任何一个容器都可以指定为Flex布局。行内元素也可以使用Flex布局。
注意:设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。
采用Flex布局的元素,称为Flex容器(flex container),简称“容器”。
它的所有子元素自动成为容器成员,成为Flex项目(flex item),简称“项目”。
容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis),项目默认沿水平主轴排列。
设置在容器上的属性:
设置在项目上的属性:
Flex布局是CSS3新增的一种布局方式,可以通过将一个元素的display属性值设置为flex从而使它成为一个flex容器,它的所有子元素都会成为它的项目。一个容器默认有两条轴:一个是水平的主轴,一个是与主轴垂直的交叉轴。可以使用flex-direction来指定主轴的方向。可以使用justify-content来指定元素在主轴上的排列方式,使用align-items来指定元素在交叉轴上的排列方式。还可以使用flex-wrap来规定当一行排列不下时的换行方式。对于容器中的项目,可以使用order属性来指定项目的排列顺序,还可以使用flex-grow来指定当排列空间有剩余的时候,项目的放大比例,还可以使用flex-shrink来指定当排列空间不足时,项目的缩小比例。
浮动的定义:非IE浏览器下,容器不设高度且子元素浮动时,容器高度不能被内容撑开。 此时,内容会溢出到容器外面而影响布局。这种现象被称为浮动(溢出)。
浮动的工作原理:
浮动元素可以左右移动,直到遇到另一个浮动元素或者遇到它外边缘的包含框。浮动框不属于文档流中的普通流,当元素浮动之后,不会影响块级元素的布局,只会影响内联元素布局。此时文档流中的普通流就会表现得该浮动框不存在一样的布局模式。当包含框的高度小于浮动框的时候,此时就会出现“高度塌陷”。
浮动元素引起的原因?
清除浮动的方式:
父级盒子是无法确认高度的,需要有内容将高度撑起来,设置固定高度无法实现页面自适应,所以不推荐。
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .parent {
- width: 100%;
- height: 200px;
- background-color: pink;
- }
-
- .child {
- float: left;
- width: 200px;
- height: 200px;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="parent">
- <div class="child">contentdiv>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .parent {
- width: 100%;
- background-color: pink;
- }
-
- .child {
- float: left;
- width: 200px;
- height: 200px;
- background: blanchedalmond;
- }
- .clear {
- clear: both;
- }
- style>
- head>
-
- <body>
- <div class="parent">
- <div class="child">contentdiv>
- <div class="clear">div>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .parent {
- width: 100%;
- background-color: pink;
- overflow: auto; /* overflow: hidden; 二者都可解决盒子塌陷 */
- }
-
- .child {
- float: left;
- width: 200px;
- height: 200px;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="parent">
- <div class="child">contentdiv>
- div>
- div>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .parent {
- width: 100%;
- background-color: pink;
- zoom: 1;
- }
-
- .parent:after {
- /* clear 属性只有块级元素才有效的,而::after 等伪元素默认都是内联水平,这就是借助伪元素清除浮动影响时需要设置 display 属性值的原因。*/
- display: block;
- content: '';
- clear: both;
- }
-
- .child {
- float: left;
- width: 200px;
- height: 200px;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="parent">
- <div class="child">contentdiv>
- div>
- div>
- body>
-
- html>
相关概念:
- Box:Box是CSS布局的对象和基本单位,一个页面是由很多个Box组成的,这个Box就是我们所说的盒模型。
- Formatting context:块级上下文格式化,它是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用。
创建BFC的条件:
BFC的特点:
BFC的作用:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .left {
- width: 300px;
- height: 100vh;
- float: left;
- background-color: pink;
- }
-
- .right {
- height: 100vh;
- overflow: hidden;
- background: blanchedalmond;
- }
- style>
- head>
-
- <body>
- <div class="left">leftdiv>
- <div class="right">rightdiv>
- body>
-
- html>
| 属性值 | 概述 |
|---|---|
| static | 默认值。没有定位,元素出现在正常的文档流中,会忽略top、bottom、left、right或者z-index声明,块级元素从上往下纵向排列,行级元素从左向右排列。 |
| absolute | 生成绝对定位的元素,相对于static定位以外的第一个父元素进行定位。元素的位置通过left、top、right、bottom属性进行规定。可通过z-index进行层次分级。 |
| relative | 生成相对定位的元素,相对于其原来的位置进行定位。元素的位置通过left、top、right、bottom属性进行规定。 |
| fixed | 生成绝对定位的元素。指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变,比如回到顶部的按钮一般都是用此定位方式。 |
| inherit | 继承父元素的position值。 |
absolute的定位方式:
元素的定位为absolute设置了top、left,浏览器会根据什么去确定它的纵向和横向的偏移量?
浏览器会递归查找该元素的所有父元素,如果找到了一个设置了position: relative/absolute/fixed;的元素,就以该元素为基准定位。如果没有找到,就以浏览器边界定位。


relative的定位方式:
元素的定位永远是相对于元素自身位置的,和其他元素没关系,也不会影响其他元素。

fixed的定位方式:
元素的定位是相对于window(或者iframe)边界的,和其他元素没有关系。但是它具有破坏性,会导致其他元素位置的变化。

CSS 绘制三角形主要用到的是 border 属性,也就是边框。平时在给盒子设置边框时,往往都设置很窄,就可能误以为边框是由矩形组成的。实际上,border 属性是由三角形组成的。
代码如下:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .box{
- width: 0;
- height: 0;
- border: 100px solid;
- border-color: orange blue red green;
- }
- style>
- head>
-
- <body>
- <div class="box">
- div>
- body>
-
- html>
效果图:
CSS 实现三角形的原则:通过上下左右边框来控制三角形的方向,用边框的宽度比来控制三角形的角度。
代码如下:
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- <style>
- .box{
- width: 0;
- height: 0;
- border-top: 50px solid red;
- border-right: 20px solid transparent;
- border-left: 50px solid transparent;
- }
- style>
- head>
-
- <body>
- <div class="box">
- div>
- body>
-
- html>
效果图:

1px 问题指的是:在一些Retina屏幕的机型上,移动端页面的 1px 会变得很粗,呈现出不止 1px 的效果。
原因:CSS 中的 1px 并不能和移动设备上的 1px 划等号。
它们之间的比例关系有一个专门的属性来描述:
window.devicePixelRatio = 设备的物理像素 / CSS像素
解决1px问题: