CSS 1.css介绍 css指的是层叠样式表(cascading style sheets) 官方文档:https://www.w3school.com.cn/css/index.asp为什么需要c
顺晟科技
2022-09-13 13:10:19
201
<style>
html *{
padding:0;
margin:0;
}
.layout article div{
min-height:200px
}
.layout.float .left{
float:left;
width: 300px;
background: red;
}
.layout.float .right{
float:right;
width: 300px;
background: yellow;
}
.layout.float .center{
background: green
}
</style>
</head>
<body>
<section class="layout float">
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动解决方案</h1>
1.这是三栏布局的中间部分
</div>
</article>
</section>
</body>
<style>
html *{
padding:0;
margin:0;
}
.layout .left-right-center{
position: relative;
}
article div{
min-height:200px;
}
.layout.absolute .left{
position: absolute;
left:0;
width:300px;
background: red;
}
.layout.absolute .center{
position: absolute;
left:300px;
right:300px;
background: yellow;
}
.layout.absolute .right{
position: absolute;
right:0;
width:300px;
background: blue;
}
</style>
</head>
<body>
<section class="layout absolute">
<article class="left-right-center">
<div class="left"></div>
<div class="center">
<h1>浮动解决方案</h1>
1.这是三栏布局的中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
<style>
html *{
padding:0;
margin:0;
}
.layout article div{
min-height:200px
}
.layout .flexbox{
display: flex;
}
.layout .flexbox .left{
width:300px;
background: red;
}
.layout .flexbox .center{
flex:1;
background:yellow;
}
.layout .flexbox .right{
width:300px;
background: blue;
}
</style>
</head>
<body>
<section class="layout ">
<article class="flexbox">
<div class="left"></div>
<div class="center">
<h1>浮动解决方案</h1>
1.这是三栏布局的中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
<style>
html *{
padding:0;
margin:0;
}
.layout article div{
height:200px;
}
.layout .left-center-right{
width:100%;
display: table;
}
.layout .left-center-right>div{
display: table-cell;
}
.layout.table .left{
width:300px;
background: red;
}
.layout.table .center{
background: yellow;
}
.layout.table .right{
width:300px;
background: blue;
}
</style>
</head>
<body>
<section class="layout table">
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>浮动解决方案</h1>
1.这是三栏布局的中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
<style>
html *{
padding:0;
margin:0;
}
.layout article div{
min-height:200px
}
.layout.grid .left-center-right{
display: grid;
width:100%;
grid-template-rows: 200px;
grid-template-columns: 300px auto 300px;
}
.layout.grid .left{
background: red;
}
.layout.grid .center{
background: yellow;
}
.layout.grid .right{
background: blue;
}
</style>
</head>
<body>
<section class="layout grid">
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>浮动解决方案</h1>
1.这是三栏布局的中间部分
</div>
<div class="right"></div>
</article>
</section>
</body>
延伸:
float
布局兼容性比较好,脱离文档流,但是父级需要清浮动,兼容性好
absolute
布局简单,兼容性好,脱离文档流
flex
布局的优点 布局简单,灵活,移动端友好,缺点是ie8以下的浏览器不兼容
tabe
兼容性好,可以兼容ie8,但是是table标签的不正规使用
gird
兼容性不太良好,可以使用在非常多的使用场景
如上
浮动不能使用,定位不能使用,grid不能使用
table和flex可以使用
题目:谈谈你对CSS盒模型的认识
标准模型
IE盒模型
CSS盒模型:
标准盒子模型:宽度= 内容宽度(content) + padding + boder + margin
IE盒模型:宽度 = 内容宽度(content+padding+ border) +margin
用来控制元素的盒子模型的解析模式,默认为content-box
content-box:w3c的标准盒子模型,设置元素的height/width属性指的是content部分的高/宽
border-box: IE传统盒子模型。设置元素的height/width属性指的是content+padding+border部分的宽和高
css如何设置这两种模型
box-sizing:content-box //标准盒模型
box-sizing:border-box //IE盒模型
JS如何设置盒模型对应的宽和高
dom.style.width/height //不会获取外嵌样式宽和高,只能在页面标签上直接添加
dom.currentStyle.width/height //只支持IE
window.getComputedStyle(dom).width/height //支持IE,火狐,chrome
dom.getBoundingCliendRect().width/height //content+padding+border
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双边距问题</title>
<style>
html *{
margin:0;
padding:0;
}
#sec{
background: #f00;
}
.child{
height:100px;
margin-top:10px;
background: yellow;
}
#bro1{
height:200px;
margin-bottom:20px;
background: blue;
}
#bro2{
height:200px;
margin-top:10px;
background: gray;
}
.empty{
margin-top:20px;
margin-bottom:10px;
}
</style>
</head>
<body>
<!--父子双边距 此时父元素sec的高度仍为100px 不会加上子元素的margin-top-->
<section id="sec">
<article class="child"></article>
</section>
<br>
<br>
<br>
<!--兄弟双边距 此时兄弟节点之间的margin为两者margin的最大值 即20px-->
<section id="bro1"></section>
<section id="bro2"></section>
<br>
<br>
<br>
<!--空元素 此时margin取margin-top和margin-bottom的最大值 即20px-->
<div class="empty"></div>
</body>
</html>
定义:
BFC是块级格式化上下文,是用于布局块级盒子的一块渲染区域,BFC是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面元素,外面的元素也不会影响到子元素
BFC规则
如何创建BFC
css选择器:
可继承属性:font-size,font-famliy,color
不可继承的样式:boder,padding,margin,width,height
元素选择器: 1
class选择器:10
id选择器:100
元素标签:1000
CSS选择器的解析是从右到左解析的。若从左向右的匹配,发现不符合规则,则需要进行回溯,会损失很多性能。若从右向左匹配,先找到所有的最右节点,对于每一个节点,向上寻找父节点直到找到根元素或满足条件的匹配规则,则结束这个分支的遍历。两种匹配规则的性能差别很大,是因为从右向左的匹配在第一步就筛选了大量的不符合条件的最右节点,而从左往右的匹配规则的性能都浪费在了失败的查找上面。
而在CSS解析完毕后,需要将解析的结果与DOM tree的内容一起进行分析建立一颗Render Tree,最终用来进行绘画。在建立Render Tree时,浏览器就要为每个DOM tree中的元素根据CSS的解析结果来确定生成怎样的Render Tree
::before 和 :after中双冒号和单冒号有什么区别?解释一下
行高是指一行文字的高度,具体说是两行文字间的基线的距离。CSS中起高度作用的是height和line-height,没有定义height属性,最终其表现作用一定是line-height
单行文本垂直居中:把line-height值设置为height一样的值可以实现单行文字的垂直居中,其实也可以把height删除。
多行文本垂直居中:需要设置display为inline-block
浮动元素碰到包含它的边框或者浮动元素的边框停留。由于浮动元素不在文档流中,所以文档流的块框表现就像浮动框不存在一样。浮动元素会漂浮在文档流的块框上。
浮动带来的问题
清浮动的方式:
09
2022-11
09
2022-11
09
2022-11
09
2022-11
19
2022-10
19
2022-10