曾经面试踩过的坑,都在这里了~ HTML/CSS

2018-11-04 15:14:43 +08:00
 southSu

前言

前段时间面试(包括阿里巴巴的电话面试),遇到过一些面试题,且面试中出现机率较高的提问 /笔试,有些答的不是很好挂掉了,今天终于有时间整理出来分享给大家,希望对大家面试有所帮助,都能轻松拿 offer。

主要分三部分:htmlcssjs; react/vue 等都归类于 js,内容来源于面试过程中遇到的、在复习过程中看到认为值得加深巩固群友交流分享的;如有理解的错误或不足之处,欢迎留言纠错、斧正,这里是@IT·平头哥联盟,我是首席填坑官苏南(South·Su) ^_^~

HTML

1、什么是盒子模型?

有些面试官会问你对盒子模型的理解,在我们平时看到的网页中,内部的每一个标签元素它都是有几个部分构成的:内容(content)、外边距(margin)、内边距(padding)、边框(border),四个部分组成,当你说完这些面试官是不会满意这个答案的,因为还有一个重点(IE 盒模型和标准盒模型的区别)——— IE 盒模型的 content 包括 border、padding

2、页面导入样式时有几种方法,它们之间有区别?

小结link页面被加载的时,link 会同时被加载,而@import引用的 CSS 会等到页面被加载完再加载,且 link 是XHTML标签,无兼容问题; link 支持动态 js 去控制 DOM 节点去改变样式,而 @import 不支持,

3、简单讲述一下块元素、内联元素、空元素有哪些,它们之间的区别?

小结:块元素总是独占一行,margin 对内联元素上下不起作用;

4、说说 cookies,sessionStorage、localStorage 你对它们的理解?

1)、如何让 cookie 浏览器关闭就失效?——不对 cookie 设置任何正、负或 0 时间的即可;

2)、sessionStorage 在浏览器多窗口之间 (同域)数据是否互通共享? ——不会,都是独立的,localStorage 会共享;

3)、能让 localStorage 也跟 cookie 一样设置过期时间?答案是可以的,在存储数据时,也存储一个时间戳,get 数据之前,先拿当前时间跟你之前存储的时间戳做比较 详细可看我之前写的另一篇分享(小程序项目总结 )。

5、简述一下你对 HTML 语义化的理解 ?

语义化是指根据内容的类型,选择合适的标签(代码语义化),即用正确的标签做正确的事情; html语义化让页面的内容结构化,结构更清晰,有助于浏览器、搜索引擎解析对内容的抓取; 语义化的 HTML 在没有CSS的情况下也能呈现较好的内容结构与代码结构; 搜索引擎的爬虫也依赖于 HTML 标记来确定上下文和各个关键字的权重,利于SEO;

CSS

1、position 的 static、relative、absolute、fixed 它们的区别?
2、如何让一个元素垂直 /水平(垂直水平)都居中,请列出你能想到的几种方式?
<div class="div-demo"></div>
<style>
	.div-demo{
		width:100px;
		height:100px;
		background-color:#06c;
		margin: auto;
		position:absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
	}
</style>
<style>
	.div-demo{
		width:100px;
		height:100px;
		background-color:#06c;
		margin: auto;
		position:absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
		-webkit-transform: translate(-50%,-50%);
	}
</style>
<body class="container">
	<div class="div-demo"></div>
	<style>

		html,body{
			height:100%;
		}
		.container{
			display: box;
			display: -webkit-box;
			display: flex;
			display: -webkit-flex;
			-webkit-box-pack: center;
			-webkit-justify-content: center;
			justify-content: center;
			-webkit-box-align: center;
			-webkit-align-items: center;
			align-items: center;
		}
		.div-demo{
			width:100px;
			height:100px;
			background-color:#06c;
		}
	</style>

</body>

3、项目中有用纯 CSS 样式写过 三角形 icon 吗?
<body class="container">
	<div class="div-angles"></div>
	<div class="div-angles right"></div>
	<div class="div-angles bottom"></div>
	<div class="div-angles left"></div>
	<style>

		html,body{
			height:100%;
		}
		.container{
			display: box;
			display: -webkit-box;
			display: flex;
			display: -webkit-flex;
			-webkit-box-pack: center;
			-webkit-justify-content: center;
			justify-content: center;
			-webkit-box-align: center;
			-webkit-align-items: center;
			align-items: center;
		}
		.div-angles{
			width: 0;
			height: 0;
			border-style: solid;
			border-width:30px;
			width:0px;
			height:0px;
			border-color: transparent transparent #06c transparent;
		}
		.right{
			border-color: transparent transparent transparent #06c ;
		}
		.bottom{
			border-color: #06c transparent transparent ;
		}
		.left{
			border-color: transparent #06c transparent transparent;
		}
	</style>

</body>

4、什么是外边距合并,项目中是否有遇到过?
5、:before 和 :after 两伪元素,平时都是使用双冒号还是单冒号?有什么区别?以及它们的作用:
6、Chrome、Safari 等浏览器,当表单提交用户选择记住密码后,下次自动填充表单的背景变成黄色,影响了视觉体验是否可以修改?
input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill {
  background-color: #fff;//设置成元素原本的颜色
  background-image: none;
  color: rgb(0, 0, 0);
}
//方法 2:由(licongwen )补充
input:-webkit-autofill {
    -webkit-box-shadow: 0px 0 3px 100px #ccc inset; //背景色
}
7、浏览器的最小字体为 12px,如果还想再小,该怎么做?
8、移动端的边框 0.5px,你有几种方式实现?

贴上 3、5 两方案代码,也是目前公司一直在用的(预处理 SCSS):

//3、css3 的 background-image 本文由 @IT·平头哥联盟-首席填坑官∙苏南分享
@mixin border($top:1, $right:1, $bottom:1, $left:1, $color:#ebebf0) {
  background-image:linear-gradient(180deg, $color, $color 50%, transparent 50%), 
                  linear-gradient(90deg, $color, $color 50%, transparent 50%), 
                  linear-gradient(0deg, $color, $color 50%, transparent 50%),
                  linear-gradient(90deg, $color, $color 50%, transparent 50%);
  background-size: 100% $top + px, $right + px 100%, 100% $bottom + px, $left + px 100%;
  background-repeat: no-repeat;
  background-position: top, right top, bottom, left top ;
}

@mixin borderTop($top:1, $color:#ebebf0) {
  @include border($top, 0, 0, 0, $color);
}
@mixin borderRight($right:1, $color:#ebebf0) {
  @include border(0, $right, 0, 0, $color);
}
@mixin borderBottom($bottom:1, $color:#ebebf0) {
  @include border(0, 0, $bottom, 0, $color);
}
@mixin borderLeft($left:1, $color:#ebebf0) {
  @include border(0, 0, 0, $left, $color);
}
@mixin borderColor($color:#ebebf0) {
  @include border(1, 1, 1, 1, $color);
}

//5、css3 的 transform:scale  本文由 @IT·平头哥联盟-首席填坑官∙苏南分享
@mixin borderRadius($width:1,$style:solid,$color:#ebebf0,$radius:2px) {
  position:relative;
    &:after{
       left:0px;
       top:0px;
       right:-100%;
       bottom:-100%;
       border-radius:$radius;
       border-style: $style;
       border-color: $color;
       border-width: $width+ px;
       position:absolute;
       display:block;
       transform:scale(0.5);
       -webkit-transform:scale(0.5);
       transform-origin:0 0;
       -webkit-transform-origin:0 0;
       content:'';
    }
}
8、display:none 与 visibility:hidden 两者的区别?
9、CSS 样式优先级排序如何计算的?
10、li 与 li 之间有看不见的空白间隔是什么原因引起的?你是怎么解决的?

抱歉 Javascript 部分,在下一篇,因为 V2EX 提示文章主题超长了,请见谅!

文本将持续更新,整理收集自己 /群友的面经分享给大家,如果觉得不错那就请关注下方的 公众号吧,您的支持是我最大的动力。

作者:苏南 - 首席填坑官
原文链接: https://blog.csdn.net/weixin_43254766/article/details/83119712
交流群:912594095,公众号:honeyBadger8
本文原创,著作权归作者所有。商业转载请联系@IT·平头哥联盟获得授权,非商业转载请注明原链接及出处。

2312 次点击
所在节点    分享创造
6 条回复
southSu
2018-11-04 15:19:18 +08:00
当你累的时候,别抱怨,
因为前方就是灿烂的曙光。
当你觉得辛苦的时候,别想着放弃,
因为比你优秀的人比你还努力。
当你觉得痛苦的时候,别想着逃避,
因为即使逃到地狱也是痛苦的…
正能量的人生,才是灿烂的人生。
每天都是全新的开始!——下午好,我是苏南,周末愉快,感谢您的阅读,愿你安好!
silencefent
2018-11-04 16:52:29 +08:00
谢谢,我在苏北
qianmeng
2018-11-05 21:40:57 +08:00
不错,我竟然能看懂大部分
southSu
2018-11-05 23:11:43 +08:00
@silencefent
不那么好过的日子里,
要学会每天给自己找一个开心的理由,
哪怕只是阳光很暖,电量很满!—晚上好,我是苏南,感谢您的支持,晚安!
southSu
2018-11-05 23:12:55 +08:00
@qianmeng
最美好的生活方式
奔跑在理想的路上,
回头有一路的故事,
低头有坚定的脚步,
抬头有清晰的远方!—晚上好,我是苏南,感谢您的支持,能看懂说明你很优秀,晚安!
erwin985211
2021-06-10 14:26:43 +08:00
挖坟,刚被几道 css 面试题问到自闭

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/504320

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX