2019 CSS classic interview questions

coldplay.xixi
Release: 2020-08-03 17:06:15
forward
8503 people have browsed it

2019 CSS classic interview questions

These are some knowledge points that I have summarized during my study. In this article, I will share it with you in the form of interview questions. I hope it will be helpful to everyone. Please help. This article is quite long. If you read it carefully and read it over and over, I believe it will be helpful to your studies or interviews. I also hope that everyone will criticize and correct me!

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

The box model is the actual place occupied by the element in the web page. There are two types: Standard box model and IE box model

Standard (W3C) box model: Content content padding border border margin

The width and height refer to the width and height of the content

Low version IE box model: Content (content padding border) border margin,

The width and height refer to the width and height of the content padding border part

Special recommendation: Summary of CSS interview questions in 2020 (latest)

Expansion of the question:

How to set these two CSS A model?

box-sizing : content-box  
box-sizing : border-box复制代码
Copy after login

How to set the width and height corresponding to the box model in JS?

dom.style.width/height;//设置获取的是内联样式
dom.currentStyle.width/height;//只有IE支持
window.getComputedStyle(dom).width/height;//兼容性好
dom.getBoundingClientRect().width/height;//适用场所:计算一个元素的绝对位置复制代码
Copy after login

Example question (explaining margin overlap based on the box model)?

This example is the overlap of parent and child margins, as well as the overlap of margins of sibling elements

<style>        
    html *{            
        padding: 0;            
        margin: 0;        
    }        
    #sec{            
        background: #f00;            
        overflow: hidden; //创建了一个BFC,块级格式化上下文   
    }        
    .child{            
        height: 100px;            
        margin-top: 10px;            
        background: yellow;        
    }    
</style>
<section id="sec">        
    <article class="child"></article>    
</section>复制代码
Copy after login

BFC (margin overlap solution)?

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, set the height/width attribute of the element to refer to Is the height/width of the content part
border-box: IE traditional box model. Setting the height/width attribute of an element refers to the height/width of the content border padding part

3. Do you understand the BFC specification (block formatting context)?

(A concept in the W3C CSS 2.1 specification. It is an independent container that determines how an element positions its content, as well as its relationship and interaction with other elements.)

a The page is composed of many Boxes. The type of element and the display attribute determine the type of the Box.

Different types of Box will participate in different Formatting Context (a container that determines how to render the document), so the elements inside the Box will be rendered in different ways, that is to say, the elements inside the BFC and the elements outside are different. will affect each other.

BFC specifies how the internal Block Box is laid out.

Positioning scheme:

  1. The internal Boxes will be placed one after another in the vertical direction.
  2. The vertical distance of Box is determined by margin. The margins of two adjacent Boxes belonging to the same BFC will overlap.
  3. The left side of each element's margin box touches the left side of the containing block's border box.
  4. The BFC area will not overlap with the float box.
  5. BFC is an isolated independent container on the page. The sub-elements inside the container will not affect the elements outside.
  6. When calculating the height of BFC, floating elements will also participate in the calculation.

BFC can be triggered if one of the following conditions is met

  1. root element, that is, the value of html
  2. float is not none (Default)
  3. The value of overflow is not visible (Default)
  4. The value of display is inline-block, table-cell, table-caption
  5. The value of position is absolute Or fixed

For more introduction to BFC, please see my article What is BFC? What is the use?

4. What are the CSS selectors? What properties can be inherited? How is the CSS priority algorithm calculated?

CSS selector:

1. id selector (# myid)

2. Class selector (.myclassname)

3. Tag (element) selector (p, h1, p)

4. Adjacent selector (h1 p)

5. Sub-selector (ul > li)

6. Descendant selector (li a)

7. Wildcard selector (*)

8. Attribute selector (a[rel = "external"])

9. Pseudo-class selector (a:hover, li:nth-child)

Pseudo-element selector, group selector.

Inheritance:

Inheritable styles: font-size, font-family, color, ul, li, dl, dt, dd;

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

Priority (proximity principle): !important > [ id > class > tag ]
!important takes precedence over inline High level

Priority algorithm calculation

Priority principle of proximity, in the case of the same weight, the closest style definition shall prevail

!important>id > ;class>tag

important has higher priority than inline

元素选择符的权值:元素标签(派生选择器):1,class选择符:10,id选择符:100,内联样式权值最大,为1000

  1. !important声明的样式优先级最高,如果冲突再进行计算。
  2. 如果优先级相同,则选择最后出现的样式。
  3. 继承得到的样式的优先级最低。

5. CSS3新增伪类有那些?

p:first-of-type 选择属于其父元素的首个

元素的每个

元素。

p:last-of-type 选择属于其父元素的最后

元素的每个

元素。

p:only-of-type 选择属于其父元素唯一的

元素的每个

元素。

p:only-child 选择属于其父元素的唯一子元素的每个

元素。

p:nth-child(2) 选择属于其父元素的第二个子元素的每个

元素。

:enabled

:disabled 控制表单控件的禁用状态。

:checked,单选框或复选框被选中。

:before在元素之前添加内容,也可以用来做清除浮动

:after在元素之后添加内容

6. 如何居中p?如何居中一个浮动元素?如何让绝对定位的p居中?如何居中一个img(position定位)

水平居中p:

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

水平垂直居中一个浮动元素(position定位)

第一种:未知元素宽高

<p class="outer">
    <span>我想居中显示</span>
</p>
<style>
    .outer{
        width:300px;
        height:300px;
        position:relative;
        background-color:#ccc;
    }
    span{        float:left;
        position:absolute;
        backgroond-color:red;
        top:50%;
        left:50%;
        transform:translate(-50%,-50%);
    }
</style>复制代码
Copy after login

第二种:已知元素宽高的

<p class="outer">
    <span>我想居中显示</span>
</p>
<style>
    .outer{
        width:300px;
        height:300px;
        position:relative;
        background-color:#ccc;
    }
    span{        float:left;
        position:absolute;
        backgroond-color:red;
        width:150px;
        height:50px;
        top:50%;
        left:50%;
        margin:-25px 0px 0px -75px;
    }
</style>复制代码
Copy after login

如何垂直居中一个img(display : table-cell 或者 position定位)

<p class="outer">        
    <img src="nz.jpg" alt="">    
</p>
<style>        
    .outer{            
        width: 300px;           
        height: 300px;            
        border: 1px solid #cccccc;            
        display: table-cell;            
        text-align: center;            
        vertical-align: middle;        
    }        
    img{            
        width: 150px;            
        height: 150px;        
    }    
</style>复制代码
Copy after login

绝对定位的p水平垂直居中:

border: 1px solid black;
position: absolute;
width: 200px;
height: 100px;
margin: auto;
left: 0;
right: 0; 
top:0;
bottom:0;复制代码
Copy after login

还有更加优雅的居中方式就是用 flex布局,点击查看我的文章 掌握flex布局,这篇文章就够了

更多的居中问题,点击查看我的文章 p居中的几种方法

7. display 有哪些值?说明他们的作用?

描述
inline默认。此元素会被显示为内联元素,元素前后没有换行符。
block此元素将显示为块级元素,此元素前后会带有换行符。
none此元素不会被显示(隐藏)。
inline-block行内块元素。(CSS2.1 新增的值)
list-item此元素会作为列表显示。
table此元素会作为块级表格来显示(类似table),表格前后带有换行符

8. position 的值?

描述
absolute

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

fixed

生成固定定位的元素,相对于浏览器窗口进行定位。(老IE不支持)

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。

relative

生成相对定位的元素,相对于其正常位置进行定位,不脱离文档流。

因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。

static默认值。没有定位,元素出现在正常的文档流中(忽略 top, bottom, left, right 或者 z-index 声明)。
inherit规定应该从父元素继承 position 属性的值。

css 定位还有一个新增属性,粘性定位 sticky,它主要用在对 scroll 事件的监听上;

粘性定位可以被认为是相对定位和固定定位的混合。元素在跨越特定阈值前为相对定位,之后为固定定位。例如:

#one { position: sticky; top: 10px; }复制代码
Copy after login

在 viewport 视口滚动到元素 top 距离小于 10px 之前,元素为相对定位。之后,元素将固定在与顶部距离 10px 的位置,直到 viewport 视口回滚到阈值以下。

更多关于 sticky 的介绍,请点击查看 粘性定位介绍

9. CSS3有哪些新特性?

  1. 新增各种 CSS 选择器 :not(p) 选择每个非p的元素; p:empty 选择每个没有任何子级的p元素(包括文本节点)
  2. 边框(Borders):
    p{ 
        border:2px solid; 
        border-radius:25px; //用于设置圆角
        box-shadow: 10px 10px 5px #888888; //水平阴影 垂直阴影 模糊距离 阴影颜色
        border-image:url(border.png) 30 30 round;// 设置所有边框图像的速记属性。
    }复制代码
    Copy after login
  3. 背景 background-clip(规定背景图的绘制区域),background-origin,background-size
    p{ 
        background:url(img_flwr.gif); 
        background-repeat:no-repeat; 
        background-size:100% 100%; //规定背景图的尺寸
        background-origin:content-box;//规定背景图的定位区域
    } 
    多背景 
    body{ 
        background-image:url(img_flwr.gif),url(img_tree.gif); 
    }复制代码
    Copy after login
  4. 线性渐变 (Linear Gradients) 向下/向上/向左/向右/对角方向
    background: linear-gradient(direction, color-stop1, color-stop2, ...);复制代码
    Copy after login
  5. 文本效果 阴影text-shadow,textwrap,word-break,word-wrap;
  6. 2D 转换 transform:scale(0.85,0.90) | translate(0px,-30px) | skew(-9deg,0deg) |rotate() 3D转换 perspective();transform是向元素应用 2D 或者 3D 转换;
  7. 过渡 transition
  8. 动画
  9. 多列布局 (multi-column layout)
  10. 盒模型
  11. flex 布局
  12. 多媒体查询 定义两套css,当浏览器的尺寸变化时会采用不同的属性

更多 CSS3 和 HTML5的新特性,请点击 CSS3 和 HTML5 新特性一览

10. 请解释一下 CSS3 的 flexbox(弹性盒布局模型),以及适用场景?

该布局模型的目的是提供一种更加高效的方式来对容器中的条目进行布局、对齐和分配空间。在传统的布局方式中,block 布局是把块在垂直方向从上到下依次排列的;而 inline 布局则是在水平方向来排列。弹性盒布局并没有这样内在的方向限制,可以由开发人员自由操作。
试用场景:弹性布局适合于移动前端开发,在Android和ios上也完美支持。

更多关于 flex 布局,请点击查看我的文章 你真的了解 flex 布局吗?

11. 用纯CSS创建一个三角形的原理是什么?

首先,需要把元素的宽度、高度设为0。然后设置边框样式。

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. 一个满屏 品 字布局如何设计?

第一种真正的品字:

  1. 三块高宽是确定的;
  2. 上面那块用margin: 0 auto;居中;
  3. 下面两块用float或者inline-block不换行;
  4. 用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; 将其转化为行内属性。渐进识别的方式,从总体中逐渐排除局部。首先,巧妙的使用“9”这一标记,将IE浏览器从所有情况中分离出来。接着,再次使用“+”将IE8和IE7、IE6分离开来,这样IE8已经独立识别。 渐进识别的方式,从总体中逐渐排除局部。首先,巧妙的使用“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
  3. 设置较小高度标签(一般小于10px),在IE6,IE7中高度超出自己设置高度。hack:给超出高度的标签设置overflow:hidden;或者设置行高line-height 小于你设置的高度。
  4. IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性。解决方法:统一通过getAttribute()获取自定义属性。
  5. Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示,可通过加入 CSS 属性 -webkit-text-size-adjust: none; 解决。
  6. 超链接访问过后hover样式就不出现了,因为被点击访问过的超链接样式不再具有hover和active了解决方法是改变CSS属性的排列顺序:L-V-H-A :
    a:link {} 
    a:visited {} 
    a:hover {} 
    a:active {}复制代码
    Copy after login
  7. IE下,even对象有x,y属性,但是没有pageX,pageY属性;

    Under Firefox, the event object has pageX and pageY attributes, but no x, y attributes.

    Solution: (Conditional comment) The disadvantage is that additional HTTP requests may be added under IE browser.

  8. png24-bit pictures appear as backgrounds on iE6 browsers, the solution is to make them PNG8.

14. Write down several solutions to IE6 BUG

1. Double margin BUG float caused by using display

2. 3 pixel problem caused by using float using dislpay:inline -3px

3. Hyperlink hover invalid after click Correct writing order link visited hover active

4. Ie z-index problem add position:relative

5. Png transparently use js code to change

6. Min-height minimum height!Important solution'

7. Select is used to cover the iframe nesting under IE6

8. Why is there no way to define a width container of about 1px (caused by the default line height of IE6 Yes, use over:hidden,zoom:0.08 line-height:1px)

9. IE 6 does not support !important

15. Why should we initialize the CSS style

because Browser compatibility issues. Different browsers have different default values ​​for some tags. If CSS is not initialized, page display differences between browsers will often occur. Of course, the initialization style will have a certain impact on SEO, but you can't have your cake and eat it too, but try to initialize it with the least impact.

16. How is the calculation method of absolute containing block different from that of normal flow?

No matter which one it belongs to, you must first find the element whose nearest position value is not static among its ancestor elements, and then judge:

  1. If this element is an inline element, then The containing block is the smallest rectangle that can contain the padding box of the first and last inline box generated by this element (the area except margin and border);
  2. Otherwise, it is composed of the padding box of this ancestor element .

If neither is found, it is the initial containing block.

Supplement:

1. static (default)/relative: simply speaking, it is the content box of its parent element (that is, removing the padding part)

2. absolute : Find the nearest element positioned as absolute/relative

3. fixed: Its containing block is always the root element (html/body), and the root element is also the initial containing block

17. The visibility attribute in CSS has a collapse attribute value. What is it used for? What will be the difference in different browsers?

When the <strong>visibility</strong> attribute of an element is set to the <strong>collapse</strong> value, for general elements, its The performance is the same as <strong>hidden</strong>. But the exception is that if this element is a table-related element, such as table row, table group, table column, table column group, its behavior is the same as <strong>display: none</strong>, That is, the space they occupy will also be released.

In Google Chrome, there is no difference between using the <strong>collapse</strong> value and using the <strong>hidden</strong> value.

In Firefox, Opera and IE11, the effect of using the <strong>collapse</strong> value is exactly what it sounds like: the table row will disappear, and the row below it will disappear. Will replenish its place.

18. What is the difference between display:none and visibility:hidden?

display : none Hide the corresponding element and no longer allocate space in the document layout (reflow and redraw)

visibility:hideen Hide the corresponding element The elements will still retain the original space in the document layout (redraw)

After using the CSS display:none attribute, the width, height and other attribute values ​​​​of the HTML element (object) will be "lost"; After using the visibility:hidden attribute, the HTML element (object) is only visually invisible (completely transparent), and the spatial position it occupies still exists.

19. What will happen when the features of position, display, overflow, and float are superimposed on each other?

The display attribute specifies the type of box that the element should generate; the position attribute specifies the positioning type of the element; the float attribute is a layout method that defines in which direction the element floats.
Similar to the priority mechanism: position: absolute/fixed has the highest priority. When they are present, float does not work and the display value needs to be adjusted. Elements positioned by float or absolute can only be block elements or tables.

20. Why does float appear? When does it need to be cleared? What are the ways to clear floats? What are the pros and cons? Which one do you think is the best? Why?

The reason for floating:

The floating element encounters the border containing it or stays on the border of the floating element. In the CSS specification, floating positioning does not belong to the normal page flow, but is positioned independently, so the block box of the document flow behaves as if the floating box does not exist. Floated elements float on the block box of the document flow.

关于css的定位机制:普通流,浮动,绝对定位(position:fixed是position:absolute的一个子类)。浮动的框可以左右移动,直到它的外边缘遇到包含框或者另一个浮动框的边缘,所以才说浮动定位不属于正常的页面流。文档中的普通流就会表现得和浮动框不存在一样,当浮动框高度超出包含框的时候,就会出现包含框不会自动伸缩高度类笔盒浮动元素。所以,只含有浮动元素的父容器在显示时不需要考虑子元素的位置,就造成显示父容器像空容器一样。

浮动带来的问题:

  1. 父元素的高度无法被撑开,影响与父元素同级的元素
  2. 与浮动元素同级的非浮动元素(内联元素)会跟随其后
  3. 若非第一个元素浮动,则该元素之前的元素也需要浮动,否则会影响页面显示的结构。

清除浮动的方式

  1. 父级p定义height
  2. 最后一个浮动元素后加空 p 标签 并添加样式 clear:both。(理论上能清除任何标签,增加无意义的标签)
  3. 包含浮动元素的父标签添加样式 overflow 为 hidden 或 auto。
  4. 父级 p 定义 zoom(空标签元素清除浮动而不得不增加无意义代码的弊端,使用zoom:1用于兼容IE)
  5. 用after伪元素清除浮动(用于非IE浏览器)

1、父级p定义height

原理:父级p手动定义height,就解决了父级p无法自动获取到高度的问题

优点:简单,代码少,容易掌握

缺点:只适合高度固定的布局,要给出精确的高度,如果高度和父级p不一样时,会产生问题

建议:不推荐使用,只建议高度固定的布局时使用

2、结尾处加空 p 标签 clear:both

原理:添加一个空p,利用css提高的clear:both清除浮动,让父级p能自动获取到高度

优点:简单,代码少,浏览器支持好,不容易出现怪问题

缺点:不少初学者不理解原理;如果页面浮动布局多,就要增加很多空p,让人感觉很不爽

建议:不推荐使用,但此方法是以前主要使用的一种清除浮动方法

3、父级 p 定义 overflow:hidden

原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域的高度

优点:简单,代码少,浏览器支持好

缺点:不能和position配合使用,因为超出的尺寸的会被隐藏

建议:只推荐没有使用position或对overflow:hidden理解比较深的朋友使用

4、父级p定义伪类 :after 和 zoom

原理:IE8以上和非IE浏览器才支持:after,原理和方法2有点类似,zoom(IE转有属性)可解决ie6,ie7浮动问题

优点:浏览器支持好,不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)

缺点:代码多,不少初学者不理解原理,要两句代码结合使用,才能让主流浏览器都支持

建议:推荐使用,建议定义公共类,以减少CSS代码

(1) 在子元素后添加一个空p p{clear:both;}

(2) 在父元素中{overflow:hidden|auto;zoom:1}

(3) :after伪选择符,在父容器的尾部自动创建一个子元素

.clearfix:after {
    content: "\0020";display: block;height: 0;clear: both;
}
.clearfix {
    zoom: 1;
}复制代码
Copy after login

"clearfix"是父容器的class名称,"content:"020";"是在父容器的结尾处放一个空白符,

"height: 0;"是让这个这个空白字符不显示出来,"display: block; clear: both;"是确保这个空白字符是非浮动的独立区块。:after选择符IE 6不支持,添加一条IE 6的独有命令"zoom:1;"就行了.

21. 上下 margin 重合的问题

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

<p class="aside"></p>
<p class="text">
    <p class="main"></p>
</p>
<!--下面是css代码-->
 .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. 里边
    <link rel="stylesheet" type="text/css" href="xxx.css" media="only screen and (max-device-width:480px)">复制代码
    Copy after login
  2. CSS :
  3. @media only screen and (max-device-width:480px) {
        /css样式/}复制代码
    Copy after login

24. 什么是CSS 预处理器 / 后处理器?大家为什么要使用他们?

预处理器例如:LESS、Sass、Stylus,用来预编译Sass或less,增强了css代码的复用性,还有层级、mixin、变量、循环、函数等,具有很方便的UI组件模块化开发能力,极大的提高工作效率。

后处理器例如:PostCSS,通常被视为在完成的样式表中根据CSS规范处理CSS,让其更有效;目前最常做的是给CSS属性添加浏览器私有前缀,实现跨浏览器兼容性的问题。

CSS 预处理器为 CSS 增加一些编程的特性,无需考虑浏览器的兼容性问题”,例如你可以在 CSS 中使用变量、简单的逻辑程序、函数(如右侧代码编辑器中就使用了变量$color)等等在编程语言中的一些基本特性,可以让你的 CSS 更加简洁、适应性更强、可读性更佳,更易于代码的维护等诸多好处。

其它 CSS 预处理器语言:

  • Sass(SCSS)
  • LESS
  • Stylus
  • Turbine
  • Swithch CSS
  • CSS Cacheer
  • DT CSS

为什么要使用它们?

结构清晰,便于扩展。

可以方便地屏蔽浏览器私有语法差异。这个不用多说,封装对浏览器语法差异的重复处理,减少无意义的机械劳动。

可以轻松实现多重继承。

完全兼容 CSS 代码,可以方便地应用到老项目中。LESS 只是在 CSS 语法上做了扩展,所以老的 CSS 代码也可以与 LESS 代码一同编译。

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

  1. 避免过度约束
  2. 避免后代选择符
  3. 避免链式选择符
  4. 使用紧凑的语法
  5. 避免不必要的命名空间
  6. 避免不必要的重复
  7. 最好使用表示语义的名字。一个好的类名应该是描述他是什么而不是像什么
  8. 避免!important,可以选择其他选择器
  9. 尽可能的精简规则,你可以合并不同类里的重复规则
  10. 修复解析错误
  11. 避免使用多类选择符
  12. 移除空的css规则
  13. 正确使用display的属性:由于display的作用,某些样式组合会无效,徒增样式体积的同时也影响解析性能。

    display:inline后不应该再使用width、height、margin、padding以及float。

    display:inline-block后不应该再使用float。

    display:block后不应该再使用vertical-align。

    display:table-*后不应该再使用margin或者float。

  14. 不滥用浮动:虽然浮动不可避免,但不可否认很多css bug是由于浮动而引起。
  15. 不滥用web字体

    对于中文网站来说Web Fonts可能很陌生,国外却很流行。web fonts通常体积庞大,而且一些浏览器在下载web fonts时会阻塞页面渲染损伤性能。

  16. 不声明过多的font-size:这是设计层面的问题,设计精良的页面不会有过多的font-size声明。
  17. 不在选择符中使用ID标识符,主要考虑到样式重用性以及与页面的耦合性。
  18. 不给h1~h6元素定义过多的样式
  19. 全站统一定义一遍heading元素即可,若需额外定制样式,可使用其他选择符作为代替。
  20. 不重复定义h1~h6元素
  21. 值为0时不需要任何单位
  22. 标准化各种浏览器前缀:通常将浏览器前缀置于前面,将标准样式属性置于最后,类似:.foo{
        -moz-border-radius: 5px;
        border-radius: 5px; 
    }复制代码
    Copy after login
  23. 使用CSS渐变等高级特性,需指定所有浏览器的前缀
  24. 避免让选择符看起来像正则表达式
  25. CSS3添加了一些类似~=等复杂属性,也不是所有浏览器都支持,需谨慎使用。
  26. 遵守盒模型规则(Beware of broken box models)

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

需要在border外侧添加空白时。

空白处不需要背景(色)时。

上下相连的两个盒子之间的空白,需要相互抵消时。如15px+20px的margin,将得到20px的空白。

◆何时应当时用padding

需要在border内测添加空白时。

空白处需要背景(色)时。

上下相连的两个盒子之间的空白,希望等于两者之和时。如15px+20px的padding,将得到35px的空白。

◆浏览器兼容性问题

在IE5.x、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。

<meta name=’viewport’ content=”width=device-width, initial-scale=1. maximum-scale=1,user-scalable=no”>复制代码
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

注意:对于IE6/7/8仅支持单冒号表示法,而现代浏览器同时支持这两种表示法。另外,在CSS3中单冒号和双冒号的区域主要是用来区分伪类和伪元素的。

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 下无效怎么处理 ?

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>复制代码
Copy after login

38. 如果需要手动写动画,你认为最小时间间隔是多久,为什么?

多数显示器默认频率是60Hz,即1秒刷新60次,所以理论上最小间隔为1/60*1000ms = 16.7ms。

39. What is the reason for the invisible white space between li and li? What is the solution?

The arrangement of line boxes will be affected by intervening spaces (carriage return spaces), etc. Because spaces are also characters, these spaces will also be styled and occupy space, so there will be gaps. Set the character size to 0, there are no spaces.
Solution:

  1. You can write all the
  2. code in a row of
  3. float li float: left
  4. Use font in ul -size: 0 (not supported by Google);
  5. You can change ul{letter-spacing: -4px;};li{letter-spacing: normal;}

40. display When does :inline-block show gaps?

  1. There will be gaps when there are spaces. Solution: s except spaces.
  2. Solution when margin is positive: use negative value for margin.
  3. Solution when using font-size: font-size:0, letter-spacing, word-spacing

41. There is a height-adaptive p with two ps in it, one with a height of 100px, and I hope the other one fills the rest. Height

The outer p uses position: relative; the p that requires adaptive height uses position: absolute; top: 100px; bottom: 0; left: 0

42. png, jpg, gif Explain these image formats and when to use them. 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 the style tag written after body and before 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 what will happen to the content of the content area of ​​the overflow element. Processing?

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 is hidden.

45. What are CSS Sprites? Its advantages and disadvantages?

CSS Sprites small picture background sharing technology. It combines a bunch of small pictures into one big picture. Then use the combination of CSS background-image, background-repeat, and background-position to position the background. 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.

Advantages:

1. Very good at reducing web page requests and greatly improving page performance;

2. Reduce the bytes of images;

3. It solves the problem of web designers in naming pictures;

4. It is easy to change the style and maintain it easily.

Disadvantages:

1. Sufficient space needs to be reserved when merging pictures. Background breakage is prone to occur on widescreen and high-resolution screens;

2. Development is cumbersome and measurement is cumbersome; (style generator can be used)

3. Maintenance is cumbersome. A small change in the background may affect the entire image, resulting in an increase in bytes and css changes.

46. What is the difference between CSS pseudo-classes and pseudo-elements?

First answer:

Pseudo classes: :focus, :hover, :active

Pseudo elements: :before, :after

Pseudo classes are essentially to make up for the shortcomings of regular CSS selectors in order to obtain more information;

Pseudo elements essentially create a virtual container with content;

## The syntax of pseudo-classes and pseudo-elements in #CSS3 is different;

You can use multiple pseudo-classes at the same time, but only one pseudo-element can be used at the same time;

Second answer:

Pseudo-class: The pseudo-class selection element is based on the state of the current element, or the current characteristics of the element, rather than the static id, class, attributes, etc. of the element. symbols of. Since the state changes dynamically, when an element reaches a specific state, it may get a pseudo-class style; when the state changes, it will lose this style. It can be seen from this that its function is somewhat similar to that of class, but it is based on abstraction outside of the document, so it is called a pseudo class.

:first-child :link: :visitive :hover :active :focus :lang

伪元素:与伪类针对特殊状态的元素不同的是,伪元素是对元素中的特定内容进行操作,它所操作的层次比伪类更深了一层,也因此它的动态性比伪类要低得多。实际上,设计伪元素的目的就是去选取诸如元素内容第一个字(母)、第一行,选取某些内容前面或后面这种普通的选择器无法完成的工作。它控制的内容实际上和元素是相同的,但是它本身只是基于元素的抽象,并不存在于文档中,所以叫伪元素。

:first-line 
:first-letter 
:before 
:after复制代码
Copy after login

47.有哪项方式可以对一个 DOM 设置它的CSS样式?  

外部样式表,引入一个外部css文件

内部样式表,将css代码放在 标签内部

内联样式,将css样式直接定义在 HTML 元素内部

48. CSS 中可以通过哪些属性定义,使得一个 DOM 元素不显示在浏览器可视范围内?  

最基本的:设置 display 属性为 none,或者设置 visibility 属性为 hidden

技巧性:设置宽高为 0,设置透明度为 0,设置 z-index 位置在 -1000

49. 什么是 Css Hack?ie6,7,8 的 hack 分别是什么?

答案:解决各浏览器对 CSS 解释不同所采取的,区别不同浏览器制作不同CSS样式的设置就叫作 CSS Hack。

50. 行内元素和块级元素的具体区别是什么?行内元素的 padding 和 margin 可设置吗?

块级元素( block )特性:

总是独占一行,表现为另起一行开始,而且其后的元素也必须另起一行显示;

宽度(width)、高度(height)、内边距(padding)和外边距(margin)都可控制;

内联元素(inline)特性:

和相邻的内联元素在同一行;

宽度(width)、高度(height)、内边距的top/bottom(padding-top/padding-bottom)和外边距的top/bottom(margin-top/margin-bottom)都不可改变(也就是padding和margin的left和right是可以设置的)。

  那么问题来了,浏览器还有默认的天生inline-block元素(拥有内在尺寸,可设置高宽,但不会自动换行),有哪些?

  答案: