Home > Web Front-end > JS Tutorial > body text

Summary of basic CSS interview questions

小云云
Release: 2018-02-23 10:11:13
Original
5170 people have browsed it

This article mainly shares 50 basic CSS interview questions with you, hoping to help everyone.

1 Introduce the standard CSS box model? How is it different from the box model of lower versions of IE?

Standard box model: width = content width (content) + border + padding + margin
Low version IE box model: width = content width (content+border+padding) + margin

2 box-sizing attribute?

Used to control the parsing mode of the element's box model, the default is content-box
context-box: W3C's standard box model, setting the height/width attribute of the element refers to the content part The height/width of
border-box: IE traditional box model. Setting the height/width attributes of an element refers to the height/width of the border + padding + content part

3 What are the CSS selectors? What properties can be inherited?

CSS selectors: id selector (#myid), class selector (.myclassname), tag selector (p, h1, p), adjacent selector (h1 + p), Child selector (ul > li), descendant selector (li a), wildcard selector (*), attribute selector (a[rel="external"]), pseudo-class selector (a:hover, li: nth-child)

Inheritable properties: font-size, font-family, color

Non-inheritable styles: border, padding, margin, width, height

Priority (proximity principle): !important > [ id > class > tag ]
!important is higher than inline priority

4 How is the CSS priority algorithm calculated?

Element selector: 1
class selector: 10
id selector: 100
Element tag: 1000

  1. ! The style declared by important has the highest priority. If there is a conflict, it will be calculated again.

  2. If the priorities are the same, the style that appears last is selected.

  3. The inherited style has the lowest priority.

5 What are the new pseudo-classes in CSS3?

p:first-of-type selects the first element belonging to its parent element
p:last-of-type selects the last element that belongs to its parent element
p:only-of-type selects the only element that belongs to its parent element
p:only-child selects the only element that belongs to its parent element Child element
p:nth-child(2) Selects the second child element belonging to its parent element
:enabled :disabled The disabled state of the form control.
:checked The radio button or check box is selected.

6 How to center p? How to center a floated element? How to center an absolutely positioned p?

p:

border: 1px solid red;
margin: 0 auto; 
height: 50px;
width: 80px;
Copy after login

Centering the top, bottom, left and right of floating elements:

border: 1px solid red;
float: left;
position: absolute;
width: 200px;
height: 100px;
left: 50%;
top: 50%;
margin: -50px 0 0 -100px;
Copy after login

Centering the left and right of absolute positioning:

border: 1px solid black;
position: absolute;
width: 200px;
height: 100px;
margin: 0 auto;
left: 0;
right: 0;
Copy after login

There are more elegant ones The way to center it is to use flexbox, I will sort it out later.

7 What are the values ​​of display? Explain their function?

inline (default)--inline
none--hide
block--block display
table--table display
list- item--item list
inline-block

8 The value of position?

static (default): Arrange according to the normal document flow;
relative (relative positioning): Do not break away from the document flow, refer to its own static position through top, bottom, left, right positioning;
absolute (absolute positioning): The reference to the nearest parent element that is not static is positioned through top, bottom, left, right;
fixed (fixed positioning): The fixed reference object is the visual window.

9 What are the new features of CSS3?

  1. RGBA and transparency

  2. background-image background-origin(content-box/padding-box/border-box) background -size background-repeat

  3. word-wrap (wrap long indivisible words) word-wrap: break-word

  4. text shadow :text-shadow: 5px 5px 5px #FF0000; (horizontal shadow, vertical shadow, blur distance, shadow color)

  5. font-face attribute: Define your own font

  6. Rounded corners (border radius): border-radius attribute is used to create rounded corners

  7. ##Border image: border-image: url(border.png) 30 30 round

  8. Box-shadow: box-shadow: 10px 10px 5px #888888

  9. Media query: define two sets of css, when the browser size Different attributes will be used when changing

10 Please explain the flexbox (flexible box layout model) of CSS3 and its applicable scenarios?

The purpose of this layout model is to provide a more efficient way to layout, align and allocate space for items in a container. In the traditional layout method, the block layout arranges the blocks in the vertical direction from top to bottom; while the inline layout arranges the blocks in the horizontal direction. Flexbox layout has no such inherent direction restrictions and can be freely manipulated by developers.

Trial scenario: Flexible layout is suitable for mobile front-end development and is also perfectly supported on Android and ios.

11 What is the principle of creating a triangle with pure CSS?

First, you need to set the width and height of the element to 0. Then set the border style.

width: 0;
height: 0;
border-top: 40px solid transparent;
border-left: 40px solid transparent;
border-right: 40px solid transparent;
border-bottom: 40px solid #ff0000;
Copy after login

12 How to design a screen-full layout of pin characters?

The first real pin character:

    ##三The height and width of the block are determined;
  1. The block above uses margin: 0 auto; centered;
  2. 下面两块用float或者inline-block不换行;

  3. 用margin调整位置使他们居中。

第二种全屏的品字布局:
上面的p设置成100%,下面的p分别宽50%,然后使用float或者inline使其不换行。

13 常见的兼容性问题?

  1. 不同浏览器的标签默认的margin和padding不一样。

    *{margin:0;padding:0;}

  2. IE6双边距bug:块属性标签float后,又有横行的margin情况下,在IE6显示margin比设置的大。hack:display:inline;将其转化为行内属性。

  3. 渐进识别的方式,从总体中逐渐排除局部。首先,巧妙的使用“9”这一标记,将IE浏览器从所有情况中分离出来。接着,再次使用“+”将IE8和IE7、IE6分离开来,这样IE8已经独立识别。

    {
    background-color:#f1ee18;/*所有识别*/
    .background-color:#00deff\9; /*IE6、7、8识别*/
    +background-color:#a200ff;/*IE6、7识别*/
    _background-color:#1e0bd1;/*IE6识别*/
    }
    Copy after login
  4. 设置较小高度标签(一般小于10px),在IE6,IE7中高度超出自己设置高度。hack:给超出高度的标签设置overflow:hidden;或者设置行高line-height 小于你设置的高度。

  5. IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性。解决方法:统一通过getAttribute()获取自定义属性。

  6. Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示,可通过加入 CSS 属性 -webkit-text-size-adjust: none; 解决。

  7. 超链接访问过后hover样式就不出现了,被点击访问过的超链接样式不再具有hover和active了。解决方法是改变CSS属性的排列顺序:L-V-H-A ( love hate ):  a:link {} a:visited {} a:hover {} a:active {}

14 为什么要初始化CSS样式

因为浏览器的兼容问题,不同浏览器对有些标签的默认值是不同的,如果没对CSS初始化往往会出现浏览器之间的页面显示差异。

15 absolute的containing block计算方式跟正常流有什么不同?

无论属于哪种,都要先找到其祖先元素中最近的 position 值不为 static 的元素,然后再判断:

  1. 若此元素为 inline 元素,则 containing block 为能够包含这个元素生成的第一个和最后一个 inline box 的 padding box (除 margin, border 外的区域) 的最小矩形;

  2. 否则,则由这个祖先元素的 padding box 构成。

如果都找不到,则为 initial containing block。

补充:

  1. static(默认的)/relative:简单说就是它的父元素的内容框(即去掉padding的部分)

  2. absolute: 向上找最近的定位为absolute/relative的元素

  3. fixed: 它的containing block一律为根元素(html/body)

16 CSS里的visibility属性有个collapse属性值?在不同浏览器下以后什么区别?

当一个元素的visibility属性被设置成collapse值后,对于一般的元素,它的表现跟hidden是一样的。

  1. chrome中,使用collapse值和使用hidden没有区别。

  2. firefox,opera和IE,使用collapse值和使用display:none没有什么区别。

17 display:none与visibility:hidden的区别?

display:none 不显示对应的元素,在文档布局中不再分配空间(回流+重绘)
visibility:hidden 隐藏对应元素,在文档布局中仍保留原来的空间(重绘)

18 position跟display、overflow、float这些特性相互叠加后会怎么样?

display属性规定元素应该生成的框的类型;position属性规定元素的定位类型;float属性是一种布局方式,定义元素在哪个方向浮动。
类似于优先级机制:position:absolute/fixed优先级最高,有他们在时,float不起作用,display值需要调整。float 或者absolute定位的元素,只能是块元素或表格。

19 对BFC规范(块级格式化上下文:block formatting context)的理解?

BFC规定了内部的Block Box如何布局。
定位方案:

  1. 内部的Box会在垂直方向上一个接一个放置。

  2. Box垂直方向的距离由margin决定,属于同一个BFC的两个相邻Box的margin会发生重叠。

  3. 每个元素的margin box 的左边,与包含块border box的左边相接触。

  4. BFC的区域不会与float box重叠。

  5. BFC是页面上的一个隔离的独立容器,容器里面的子元素不会影响到外面的元素。

  6. 计算BFC的高度时,浮动元素也会参与计算。

满足下列条件之一就可触发BFC

  1. 根元素,即html

  2. float的值不为none(默认)

  3. overflow的值不为visible(默认)

  4. display的值为inline-block、table-cell、table-caption

  5. position的值为absolute或fixed

20 为什么会出现浮动和什么时候需要清除浮动?清除浮动的方式?

浮动元素碰到包含它的边框或者浮动元素的边框停留。由于浮动元素不在文档流中,所以文档流的块框表现得就像浮动框不存在一样。浮动元素会漂浮在文档流的块框上。
浮动带来的问题:

  1. 父元素的高度无法被撑开,影响与父元素同级的元素

  2. 与浮动元素同级的非浮动元素(内联元素)会跟随其后

  3. 若非第一个元素浮动,则该元素之前的元素也需要浮动,否则会影响页面显示的结构。

清除浮动的方式:

  1. 父级p定义height

  2. 最后一个浮动元素后加空p标签 并添加样式clear:both。

  3. 包含浮动元素的父标签添加样式overflow为hidden或auto。

  4. 父级p定义zoom

21 上下margin重合的问题

在重合元素外包裹一层容器,并触发该容器生成一个BFC。
例子:

    

 .aside {             margin-bottom: 100px;               width: 100px;             height: 150px;             background: #f66;         }         .main {             margin-top: 100px;             height: 200px;             background: #fcc;         }          .text{             /*盒子main的外面包一个p,通过改变此p的属性使两个盒子分属于两个不同的BFC,以此来阻止margin重叠*/             overflow: hidden;  //此时已经触发了BFC属性。         }
Copy after login

22设置元素浮动后,该元素的display值是多少?

自动变成display:block

23 移动端的布局用过媒体查询吗?

通过媒体查询可以为不同大小和尺寸的媒体定义不同的css,适应相应的设备的显示。

  1. 里边

  2. CSS : @media only screen and (max-device-width:480px) {/css样式/}

24 使用 CSS 预处理器吗?
Less sass

25 CSS优化、提高性能的方法有哪些?

  1. 避免过度约束

  2. 避免后代选择符

  3. 避免链式选择符

  4. 使用紧凑的语法

  5. 避免不必要的命名空间

  6. 避免不必要的重复

  7. 最好使用表示语义的名字。一个好的类名应该是描述他是什么而不是像什么

  8. 避免!important,可以选择其他选择器

  9. 尽可能的精简规则,你可以合并不同类里的重复规则

26 浏览器是怎样解析CSS选择器的?

CSS选择器的解析是从右向左解析的。若从左向右的匹配,发现不符合规则,需要进行回溯,会损失很多性能。若从右向左匹配,先找到所有的最右节点,对于每一个节点,向上寻找其父节点直到找到根元素或满足条件的匹配规则,则结束这个分支的遍历。两种匹配规则的性能差别很大,是因为从右向左的匹配在第一步就筛选掉了大量的不符合条件的最右节点(叶子节点),而从左向右的匹配规则的性能都浪费在了失败的查找上面。
而在 CSS 解析完毕后,需要将解析的结果与 DOM Tree 的内容一起进行分析建立一棵 Render Tree,最终用来进行绘图。在建立 Render Tree 时(WebKit 中的「Attachment」过程),浏览器就要为每个 DOM Tree 中的元素根据 CSS 的解析结果(Style Rules)来确定生成怎样的 Render Tree。

27 在网页中的应该使用奇数还是偶数的字体?为什么呢?

使用偶数字体。偶数字号相对更容易和 web 设计的其他部分构成比例关系。Windows 自带的点阵宋体(中易宋体)从 Vista 开始只提供 12、14、16 px 这三个大小的点阵,而 13、15、17 px时用的是小一号的点。(即每个字占的空间大了 1 px,但点阵没变),于是略显稀疏。

28 margin和padding分别适合什么场景使用?

何时使用margin:

  1. 需要在border外侧添加空白

  2. 空白处不需要背景色

  3. 上下相连的两个盒子之间的空白,需要相互抵消时。

何时使用padding:

  1. 需要在border内侧添加空白

  2. 空白处需要背景颜色

  3. 上下相连的两个盒子的空白,希望为两者之和。

兼容性的问题:在IE5 IE6中,为float的盒子指定margin时,左侧的margin可能会变成两倍的宽度。通过改变padding或者指定盒子的display:inline解决。

29 元素竖向的百分比设定是相对于容器的高度吗?

当按百分比设定一个元素的宽度时,它是相对于父容器的宽度计算的,但是,对于一些表示竖向距离的属性,例如 padding-top , padding-bottom , margin-top , margin-bottom 等,当按百分比设定它们时,依据的也是父容器的宽度,而不是高度。

30 全屏滚动的原理是什么?用到了CSS的哪些属性?

  1. 原理:有点类似于轮播,整体的元素一直排列下去,假设有5个需要展示的全屏页面,那么高度是500%,只是展示100%,剩下的可以通过transform进行y轴定位,也可以通过margin-top实现

  2. overflow:hidden;transition:all 1000ms ease;

31 什么是响应式设计?响应式设计的基本原理是什么?如何兼容低版本的IE?

响应式网站设计(Responsive Web design)是一个网站能够兼容多个终端,而不是为每一个终端做一个特定的版本。
基本原理是通过媒体查询检测不同的设备屏幕尺寸做处理。
页面头部必须有meta声明的viewport。

Copy after login

32 视差滚动效果?

视差滚动(Parallax Scrolling)通过在网页向下滚动的时候,控制背景的移动速度比前景的移动速度慢来创建出令人惊叹的3D效果。

  1. CSS3实现
    优点:开发时间短、性能和开发效率比较好,缺点是不能兼容到低版本的浏览器

  2. jQuery实现
    通过控制不同层滚动速度,计算每一层的时间,控制滚动效果。
    优点:能兼容到各个版本的,效果可控性好
    缺点:开发起来对制作者要求高

  3. 插件实现方式
    例如:parallax-scrolling,兼容性十分好

33 ::before 和 :after中双冒号和单冒号有什么区别?解释一下这2个伪元素的作用

  1. 单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素。

  2. ::before就是以一个子元素的存在,定义在元素主体内容之前的一个伪元素。并不存在于dom之中,只存在在页面之中。

:before 和 :after 这两个伪元素,是在CSS2.1里新出现的。起初,伪元素的前缀使用的是单冒号语法,但随着Web的进化,在CSS3的规范里,伪元素的语法被修改成使用双冒号,成为::before  ::after

34 你对line-height是如何理解的?

行高是指一行文字的高度,具体说是两行文字间基线的距离。CSS中起高度作用的是height和line-height,没有定义height属性,最终其表现作用一定是line-height。
单行文本垂直居中:把line-height值设置为height一样大小的值可以实现单行文字的垂直居中,其实也可以把height删除。
多行文本垂直居中:需要设置display属性为inline-block。

35 怎么让Chrome支持小于12px 的文字?

p{font-size:10px;-webkit-transform:scale(0.8);} //0.8是缩放比例
Copy after login

36 让页面里的字体变清晰,变细用CSS怎么做?

-webkit-font-smoothing在window系统下没有起作用,但是在IOS设备上起作用-webkit-font-smoothing:antialiased是最佳的,灰度平滑。

37 position:fixed;在android下无效怎么处理?

Copy after login

38 如果需要手动写动画,你认为最小时间间隔是多久,为什么?
多数显示器默认频率是60Hz,即1秒刷新60次,所以理论上最小间隔为1/60*1000ms = 16.7ms。

39 li与li之间有看不见的空白间隔是什么原因引起的?有什么解决办法?

行框的排列会受到中间空白(回车空格)等的影响,因为空格也属于字符,这些空白也会被应用样式,占据空间,所以会有间隔,把字符大小设为0,就没有空格了。
解决方法:

  1. 可以将

  2. 代码全部写在一排

  3. 浮动li中float:left

  4. 在ul中用font-size:0(谷歌不支持);可以使用letter-space:-3px

40 display:inline-block 什么时候会显示间隙?

  1. 有空格时候会有间隙 解决:移除空格

  2. margin正值的时候 解决:margin使用负值

  3. 使用font-size时候 解决:font-size:0、letter-spacing、word-spacing

41 There is a height-adaptive p with two ps inside, one with a height of 100px, and I hope the other one fills the remaining height

Layer p uses position: relative; p with adaptive height requirements uses position: absolute; top: 100px; bottom: 0; left: 0

42 Explain the image formats of png, jpg, and gif. Decide when to use it. Have you ever learned about webp?

  1. png is Portable Network Graphics, which is a lossless data compression bitmap file format. The advantages are: high compression ratio and good color. Available in most places.

  2. jpg is a distortion compression method used for photos. It is a destructive compression that does a good job in smooth changes in tone and color. A format used to store and transmit photos on www.

  3. gif is a bitmap file format that reproduces true-color images with 8-bit color. Animation effects can be achieved.

  4. The webp format is an image format launched by Google in 2010. The compression rate is only 2/3 of jpg, and the size is 45% smaller than png. The disadvantages are that the compression takes longer and the compatibility is not good. Currently, Google and Opera support it.

#43 What is the difference between writing the style tag after the body and before the body?

The page loads from top to bottom. Of course, the style is loaded first.
is written after the body tag. Since the browser parses the HTML document line by line, parsing the style sheet written at the end (outline or written in the style tag) will cause the browser to stop the previous rendering and wait for loading. And if the style sheet is re-rendered after the parsing of the style sheet is completed, the FOUC phenomenon may occur under IE in Windows (that is, the page flickering problem caused by style failure)

44 The CSS attribute overflow attribute defines the content of the overflow element content area. How to deal with it?

When the parameter is scroll, a scroll bar will appear.
When the parameter is auto, a scroll bar will appear when the content of the child element is larger than the parent element.
When the parameter is visible, the overflow content appears outside the parent element.
When the parameter is hidden, overflow will be hidden.

45 Explain CSS Sprites

Include all images involved in a page into one large image, and then use CSS background-image, background- The combination of repeat and background-position performs background positioning. Using CSS Sprites can greatly reduce the http requests of web pages, thereby greatly improving the performance of the page; CSS Sprites can reduce the bytes of images.

Related recommendations:

javascript two-dimensional array interview questions

The difference between the PC version and the mobile version of HTML5 interview questions

10 High Frequency HTML5 Interview Questions


The above is the detailed content of Summary of basic CSS interview questions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!