Home  >  Article  >  Web Front-end  >  Some interview questions about CSS

Some interview questions about CSS

零到壹度
零到壹度Original
2018-03-28 11:26:422529browse

Some interview questions about CSS

This article mainly shares an interview question about CSS with you. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to have a look.

1 The problem of overlapping upper and lower margins

Wrap a layer of containers around the overlapping elements and trigger the generation of the container A BFC.
Example:

<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属性。
        }

Special recommendation:2020 CSS interview question summary (latest)

2 After setting the element to float, what is the display value of the element?

automatically becomes display:block

3 Have you ever used media queries for mobile layout?

Through media queries, you can define different css for media of different sizes and dimensions to adapt to the display of the corresponding device.

  1. ##93f0f5c25f18dab9d176bd4f6de5d30e inside397433ecad6e0d09245b58b2f134190f

  2. ##CSS : @media only screen and (max-device-width:480px) {/

    cssStyle/}

4 Do you use CSS preprocessors?


Less sass

5 What are the ways to optimize CSS and improve performance?

  1. Avoid over-constraints

  2. Avoid descendant selectors

  3. Avoid chain selectors

  4. Use compact syntax

  5. Avoid unnecessary namespaces

  6. Avoid unnecessary duplication

  7. It is best to use names that represent semantics. A good class name should describe what it is rather than something like

  8. Avoid! important, you can choose other selectors

  9. to streamline the rules as much as possible. You can merge repeated rules in different categories

6 How does the browser parse CSS selectors?

CSS selectors are parsed from right to left. If the matching from left to right is found to be inconsistent with the rules, backtracking will be required, which will cause a lot of performance loss. If matching from right to left, first find all the rightmost nodes. For each node, search upwards for its parent node until the root element or a matching rule that meets the conditions is found, then the traversal of this branch ends. The performance of the two matching rules is very different because the right-to-left matching filters out a large number of rightmost nodes (leaf nodes) that do not meet the conditions in the first step, while the performance of the left-to-right matching rule All wasted on failed searches.

After the CSS is parsed, the parsed results need to be analyzed together with the contents of the DOM Tree to create a Render Tree, which is ultimately used for drawing. When creating a Render Tree (the "Attachment" process in WebKit), the browser must determine what kind of Render Tree to generate for each element in the DOM Tree based on the CSS parsing results (Style Rules).

#7 Should I use odd or even numbers of fonts in a web page? why?

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

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

何时使用margin:

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

  2. 空白处不需要背景色

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

何时使用padding:

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

  2. 空白处需要背景颜色

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

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

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

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

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

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

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

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

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

<
meta 
name
=
"’viewport’"
 
content
=
"”width=device-width,"
 
initial
-
scale
=
"1."
 
maximum
-
scale
=
"1,user-scalable=no
”"
/>


12 视差滚动效果?

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

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

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

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

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

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

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

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

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

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

多行文本垂直居中:需要设置display属性为inline-block。

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

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

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

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

17 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"
/>

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

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

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

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

  1. 可以将25edfb22a4f469ecb59f1190150159c6代码全部写在一排

  2. 浮动li中float:left

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

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

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

  2. Solution when margin is positive: use negative value for margin

  3. Solution when using font-size: font- size:0, letter-spacing, word-spacing

21 There is a height-adaptive p with two p’s inside, one with a height of 100px , hoping that another one will fill up the remaining height

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

22 Please explain these image formats png, jpg, and gif and when to use them. Have you ever learned about webp?

  1. png is a portable network graphics (Portable Network Graphics) is a lossless data compression bitmap file format. The advantages are: high compression ratio, color good. 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.

23 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 on Windows (that is, the page flickering problem caused by style failure)

24 CSS attribute overflow attribute definition How will the content of the overflow element content area be handled?

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.

25 Explain CSS Sprites

Include all images involved in a page into one large image Go, and 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.

Recommended related tutorials: CSS video tutorial

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

Statement:
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 admin@php.cn