CSS面试题

207 阅读3分钟

清浮动的方法:

1、空标签清浮动,在塌陷的元素里添加一个块元素作为他的最后一个子元素,写上样式:
<div class="clearfix"></div>
.clearfix{
clear:both;
height:0;
overflow:hidden;
}
2、br清浮动(不常用,不规范):
<br clear="both" />
3、伪类清浮动:
.clearfix:after{
content:"";
display:block;
clear:both;
height:0;
overflow:hidden;
visibility:hidden;
}
.clearfix{
zoom:1;
}

4、给父元素定高

BFC:

作用:1、包含浮动元素
2、不被浮动元素覆盖
3、阻止父子元素的margin传递,解决margin-top和margin-bottom的bug

触发BFC的条件:
1、float的值不为none
2、overflow的值不为visible
3、position的值不为static或者relative
4、display的值为table-cell、table-caption和inline-block之一
(1、3就是元素脱离文档流)

display:none 和visibilty:hidden的区别:

display:none;是元素彻底消失了
visibility:hidden;只是将元素内容隐藏了,但是元素的实际位置依然存在

position的值relative和absolute定位原点是:

relative:相对定位,参照物是其自身
absolute:绝对定位,参照物为属性不为static的定父级,如果没有,就是document
fixed:固定定位,参照物是可视区窗口

有多少种方法可以使内联元素支持宽高:

1、display:block;
2、display:inline-block;
3、浮动
4、绝对定位
5、固定定位

如何让div水平垂直居中:

方法一:(知道宽高):
div{
width: 200px;
height: 200px;
background: red;
position: absolute;
left: 50%; // right:50%;
top: 50%; // bottom:50%;
margin-left: -100px; // margin-right: -100px;
margin-top: -100px; // margin-bottom: -100px;
}

方法二:(不知道宽高):
div{
width: 200px;
height: 200px;
background: red;
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}

编写一个布局,页面最小宽度300px,左边自适应35% 右边65%:

<style>
body{
margin: 0;
padding: 0;
}
.box{
min-width: 300px;
height: 100vh;
background: red;
}
.left{
float: left;
width: 35%;
height: 100%;
background: yellow;
}
.right{
float: left;
width: 65%;
height: 100%;
background: pink;
}
</style>
<body>
<div class="box">
<div class="left"></div>
<div class="right"></div>
</div>
</body>

透明度兼容:

opacity:0~1;
filter:alpha(opacity="0~100");

表单控件类型:

输入型控件:text,password
选择型控件:radio,checkbox
按钮型控件:button,submit,reset
下拉列表:select
文本域:textarea

提示信息标签:

label:
用法:1、<label for="txt"></label>
<input type="text" id="txt" />
2、<label><input type="text" id="txt" /></label>

五大浏览器内核及其代表作品:

1、Trident,IE,遨游,世界之窗,360浏览器,此内核只能应用于windows平台,是不开源的
2、Gecko,代表作是火狐浏览器,它的最大优势是跨平台,能在多种操作系统上运行
3、Webkit,代表作是Chrome,Safari,是开源项目
4、Presto:代表作是Opera,是公认的渲染最快的引擎
5、Blink,是谷歌和Opera Software开发的浏览器排版引擎

CSS Bug:

CSS样式在不同浏览器下显示不一致的问题

CSS Hack:

是一种兼容CSS样式在不同浏览器下不能正常显示的技巧方法

Filter:

表示过滤器的意思,本质上来说就是一种用来过滤不同浏览器的hack类型

常见的过滤器有哪些:
1、下划线过滤器,只有IE6及以下的浏览器才能识别
2、*过滤器,ie7及以下浏览器可以识别
3、\0过滤器只有ie8及以上浏览器才能识别
4、\9过滤器只有ie版本浏览器才能识别
5、!important过滤器,权重最高,但ie6不能识别,但能识别!important前面的声明