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!
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复制代码
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;//适用场所:计算一个元素的绝对位置复制代码
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>复制代码
BFC (margin overlap solution)?
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
(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:
BFC can be triggered if one of the following conditions is met
For more introduction to BFC, please see my article What is BFC? What is the use?
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
p:first-of-type 选择属于其父元素的首个
元素的每个
元素。
p:last-of-type 选择属于其父元素的最后
元素的每个
元素。
p:only-of-type 选择属于其父元素唯一的
元素的每个
元素。
p:only-child 选择属于其父元素的唯一子元素的每个
元素。
p:nth-child(2) 选择属于其父元素的第二个子元素的每个
元素。
:enabled
:disabled 控制表单控件的禁用状态。
:checked,单选框或复选框被选中。
:before在元素之前添加内容,也可以用来做清除浮动
:after在元素之后添加内容
水平居中p:
border: 1px solid red; margin: 0 auto; height: 50px; width: 80px;复制代码
水平垂直居中一个浮动元素(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>复制代码
第二种:已知元素宽高的
<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>复制代码
如何垂直居中一个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>复制代码
绝对定位的p水平垂直居中:
border: 1px solid black; position: absolute; width: 200px; height: 100px; margin: auto; left: 0; right: 0; top:0; bottom:0;复制代码
还有更加优雅的居中方式就是用 flex布局,点击查看我的文章 掌握flex布局,这篇文章就够了
更多的居中问题,点击查看我的文章 p居中的几种方法
值 | 描述 |
---|---|
inline | 默认。此元素会被显示为内联元素,元素前后没有换行符。 |
block | 此元素将显示为块级元素,此元素前后会带有换行符。 |
none | 此元素不会被显示(隐藏)。 |
inline-block | 行内块元素。(CSS2.1 新增的值) |
list-item | 此元素会作为列表显示。 |
table | 此元素会作为块级表格来显示(类似table),表格前后带有换行符 |
值 | 描述 |
---|---|
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; }复制代码
在 viewport 视口滚动到元素 top 距离小于 10px 之前,元素为相对定位。之后,元素将固定在与顶部距离 10px 的位置,直到 viewport 视口回滚到阈值以下。
更多关于 sticky 的介绍,请点击查看 粘性定位介绍
:not(p)
选择每个非p的元素; p:empty
选择每个没有任何子级的p元素(包括文本节点)p{ border:2px solid; border-radius:25px; //用于设置圆角 box-shadow: 10px 10px 5px #888888; //水平阴影 垂直阴影 模糊距离 阴影颜色 border-image:url(border.png) 30 30 round;// 设置所有边框图像的速记属性。 }复制代码
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); }复制代码
background: linear-gradient(direction, color-stop1, color-stop2, ...);复制代码
更多 CSS3 和 HTML5的新特性,请点击 CSS3 和 HTML5 新特性一览
该布局模型的目的是提供一种更加高效的方式来对容器中的条目进行布局、对齐和分配空间。在传统的布局方式中,block 布局是把块在垂直方向从上到下依次排列的;而 inline 布局则是在水平方向来排列。弹性盒布局并没有这样内在的方向限制,可以由开发人员自由操作。
试用场景:弹性布局适合于移动前端开发,在Android和ios上也完美支持。
更多关于 flex 布局,请点击查看我的文章 你真的了解 flex 布局吗?
首先,需要把元素的宽度、高度设为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;复制代码
第一种真正的品字:
第二种全屏的品字布局:
上面的p设置成100%,下面的p分别宽50%,然后使用float或者inline使其不换行。
*{margin:0;padding:0;}
来统一;
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识别*/}复制代码
a:link {} a:visited {} a:hover {} a:active {}复制代码
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.
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
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.
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:
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
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.
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.
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.
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、父级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; }复制代码
"clearfix"
是父容器的class名称,"content:"020";"
是在父容器的结尾处放一个空白符,
"height:
0;"
是让这个这个空白字符不显示出来,"display:
block; clear: both;"
是确保这个空白字符是非浮动的独立区块。:after
选择符IE 6不支持,添加一条IE 6的独有命令"zoom:1;"
就行了.
在重合元素外包裹一层容器,并触发该容器生成一个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属性。 }复制代码
自动变成display:block
通过媒体查询可以为不同大小和尺寸的媒体定义不同的css,适应相应的设备的显示。
<link rel="stylesheet" type="text/css" href="xxx.css" media="only screen and (max-device-width:480px)">复制代码
@media only screen and (max-device-width:480px) { /css样式/}复制代码
预处理器例如:LESS、Sass、Stylus,用来预编译Sass或less,增强了css代码的复用性,还有层级、mixin、变量、循环、函数等,具有很方便的UI组件模块化开发能力,极大的提高工作效率。
后处理器例如:PostCSS,通常被视为在完成的样式表中根据CSS规范处理CSS,让其更有效;目前最常做的是给CSS属性添加浏览器私有前缀,实现跨浏览器兼容性的问题。
CSS 预处理器为 CSS 增加一些编程的特性,无需考虑浏览器的兼容性问题”,例如你可以在 CSS 中使用变量、简单的逻辑程序、函数(如右侧代码编辑器中就使用了变量$color)等等在编程语言中的一些基本特性,可以让你的 CSS 更加简洁、适应性更强、可读性更佳,更易于代码的维护等诸多好处。
其它 CSS 预处理器语言:
为什么要使用它们?
结构清晰,便于扩展。
可以方便地屏蔽浏览器私有语法差异。这个不用多说,封装对浏览器语法差异的重复处理,减少无意义的机械劳动。
可以轻松实现多重继承。
完全兼容 CSS 代码,可以方便地应用到老项目中。LESS 只是在 CSS 语法上做了扩展,所以老的 CSS 代码也可以与 LESS 代码一同编译。
正确使用display的属性:由于display的作用,某些样式组合会无效,徒增样式体积的同时也影响解析性能。
display:inline后不应该再使用width、height、margin、padding以及float。
display:inline-block后不应该再使用float。
display:block后不应该再使用vertical-align。
display:table-*后不应该再使用margin或者float。
不滥用web字体
对于中文网站来说Web Fonts可能很陌生,国外却很流行。web fonts通常体积庞大,而且一些浏览器在下载web fonts时会阻塞页面渲染损伤性能。
-moz-border-radius: 5px; border-radius: 5px; }复制代码
CSS选择器的解析是从右向左解析的,为了避免对所有元素进行遍历。若从左向右的匹配,发现不符合规则,需要进行回溯,会损失很多性能。若从右向左匹配,先找到所有的最右节点,对于每一个节点,向上寻找其父节点直到找到根元素或满足条件的匹配规则,则结束这个分支的遍历。两种匹配规则的性能差别很大,是因为从右向左的匹配在第一步就筛选掉了大量的不符合条件的最右节点(叶子节点),而从左向右的匹配规则的性能都浪费在了失败的查找上面。
而在 CSS 解析完毕后,需要将解析的结果与 DOM Tree 的内容一起进行分析建立一棵 Render Tree,最终用来进行绘图。在建立 Render Tree 时(WebKit 中的「Attachment」过程),浏览器就要为每个 DOM Tree 中的元素根据 CSS 的解析结果(Style Rules)来确定生成怎样的 Render Tree。
使用偶数字体。偶数字号相对更容易和 web 设计的其他部分构成比例关系。Windows 自带的点阵宋体(中易宋体)从 Vista 开始只提供 12、14、16 px 这三个大小的点阵,而 13、15、17 px时用的是小一号的点。(即每个字占的空间大了 1 px,但点阵没变),于是略显稀疏。
◆何时应当使用margin
需要在border外侧添加空白时。
空白处不需要背景(色)时。
上下相连的两个盒子之间的空白,需要相互抵消时。如15px+20px的margin,将得到20px的空白。
◆何时应当时用padding
需要在border内测添加空白时。
空白处需要背景(色)时。
上下相连的两个盒子之间的空白,希望等于两者之和时。如15px+20px的padding,将得到35px的空白。
◆浏览器兼容性问题
在IE5.x、IE6中,为float的盒子指定margin时,左侧margin可能会变成两倍的宽度。通过改用padding或指定盒子为display:inline可以解决。
当按百分比设定一个元素的宽度时,它是相对于父容器的宽度计算的,但是,对于一些表示竖向距离的属性,例如 padding-top , padding-bottom , margin-top , margin-bottom 等,当按百分比设定它们时,依据的也是父容器的宽度,而不是高度。
响应式网站设计(Responsive Web design)是一个网站能够兼容多个终端,而不是为每一个终端做一个特定的版本。
基本原理是通过媒体查询检测不同的设备屏幕尺寸做处理。
页面头部必须有meta声明的viewport。
<meta name=’viewport’ content=”width=device-width, initial-scale=1. maximum-scale=1,user-scalable=no”>复制代码
视差滚动(Parallax Scrolling)通过在网页向下滚动的时候,控制背景的移动速度比前景的移动速度慢来创建出令人惊叹的3D效果。
:before 和 :after 这两个伪元素,是在CSS2.1里新出现的。起初,伪元素的前缀使用的是单冒号语法,但随着Web的进化,在CSS3的规范里,伪元素的语法被修改成使用双冒号,成为::before ::after
注意:对于IE6/7/8仅支持单冒号表示法,而现代浏览器同时支持这两种表示法。另外,在CSS3中单冒号和双冒号的区域主要是用来区分伪类和伪元素的。
行高是指一行文字的高度,具体说是两行文字间基线的距离。CSS中起高度作用的是height和line-height,没有定义height属性,最终其表现作用一定是line-height。
单行文本垂直居中:把line-height值设置为height一样大小的值可以实现单行文字的垂直居中,其实也可以把height删除。
多行文本垂直居中:需要设置display属性为inline-block。
p{ font-size:10px; -webkit-transform:scale(0.8);//0.8是缩放比例 } 复制代码
-webkit-font-smoothing 在 window 系统下没有起作用,但是在 IOS 设备上起作用 -webkit-font-smoothing:antialiased 是最佳的,灰度平滑。
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>复制代码
多数显示器默认频率是60Hz,即1秒刷新60次,所以理论上最小间隔为1/60*1000ms = 16.7ms。
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:
The outer p uses position: relative; the p that requires adaptive height uses position: absolute; top: 100px; bottom: 0; left: 0
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)
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.
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.
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复制代码
外部样式表,引入一个外部css文件
内部样式表,将css代码放在
标签内部内联样式,将css样式直接定义在 HTML 元素内部
最基本的:设置 display 属性为 none,或者设置 visibility 属性为 hidden
技巧性:设置宽高为 0,设置透明度为 0,设置 z-index 位置在 -1000
答案:解决各浏览器对 CSS 解释不同所采取的,区别不同浏览器制作不同CSS样式的设置就叫作 CSS Hack。
块级元素( 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元素(拥有内在尺寸,可设置高宽,但不会自动换行),有哪些?
答案: 、 、
相关文章 为何img、input等内联元素可以设置宽高
外边距重叠就是 margin-collapse。
在CSS当中,相邻的两个盒子的外边距可以结合成一个单独的外边距。这种合并外边距的方式被称为折叠,并且因而所结合成的外边距称为折叠外边距。
折叠结果遵循下列计算规则:
rgba()和opacity都能实现透明效果,但最大的不同是opacity作用于元素,以及元素内的所有内容的透明度,
而rgba()只作用于元素的颜色或其背景色。(设置rgba透明的元素的子元素不会继承透明效果!)
垂直方向:line-height
水平方向:letter-spacing (letter-spacing 属性增加或减少字符间的空白)
那么问题来了,关于letter-spacing的妙用知道有哪些么?
答案:可以用于消除inline-block元素间的换行符空格间隙问题。
px和em都是长度单位,区别是:px的值是固定的,指定是多少就是多少,计算比较容易。em得值不是固定的,并且em会继承父级元素的字体大小。
浏览器的默认字体高都是16px。所以未经调整的浏览器都符合: 1em=16px。那么12px=0.75em, 10px=0.625em。
不同的浏览器对一些元素有不同的默认样式,如果不处理,在不同的浏览器下会存在必要的风险。
你可能会用Normalize来代替你的重置样式文件。它没有重置所有的样式风格,但仅提供了一套合理的默认样式值。既能让众多浏览器达到一致和合理,但又不扰乱其他的东西(如粗体的标题)。
不能。它只能移动x,y轴的位置。translate3d可以。
1. CSS3 implements rounded corners (border-radius), shadow (box-shadow),
2. Add special effects to text (text-shadow,), linear gradient (gradient), Rotation (transform)
3. transform:rotate(9deg) scale(0.85,0.90) translate(0px,-30px) skew(-9deg,0deg); // Rotate, scale, position, tilt
4. Added more CSS selectors multi-background rgba
5. The only pseudo-element introduced in CSS3 is ::selection.
6. Media queries, multiple Column layout
7. border-image
Functional difference: HTML is a basic WEB web page design language; XHTML is a markup language based on XML. XHTML is compatible with major browsers, mobile phones and PDAs, and the browser can quickly and correctly compile web pages.
Writing habits:
(1) XHTML tags must be lowercase
(2) XHTML tags must be in pairs
(3) The order of XHTML tags must be correct
(4) All XHTML attributes must use double quotes
(5) Target="_blank" is not allowed in XHTML
(6) XHTML documents must have a root element.
(7) XHTML elements must be nested correctly.
Structural layer Html Presentation layer CSS Behavior layer js
Selector {Attribute 1: value 1; Attribute 2: value 2;...}
Deprecated elements include:
· frame
· frameset
· noframe
· applet
· big
· center
· basefront
Answer: Inline, embedded, external link, import
(1) link is an XHTML tag. In addition to loading CSS, it can also be used to define RSS, define rel connection attributes, etc. , no compatibility, supports using javascript to change the style; and @import is provided by CSS, can only be used to load CSS, does not support using javascript to change the style;
(2) When the page is loaded, the link will are loaded at the same time, and the CSS referenced by @import will wait until the page is loaded before loading;
(3) import is proposed by CSS2.1, and is not supported by browsers below CSS2.1, and can only be used by IE5 or above. Recognition, and link is an XHTML tag, no compatibility issues.
It is mainly divided into two parts: the rendering engine (layout engineer or Rendering Engine) and the JS engine.
(1) Rendering engine: Responsible for obtaining the content of the web page (HTML, XML, images, etc.), organizing the information (such as adding CSS, etc.), and calculating the display method of the web page, and then outputting it to the monitor or printer . Different browser kernels will have different grammatical interpretations of web pages, so the rendering effects will also be different. All web browsers, e-mail clients, and other applications that need to edit and display the Internet require the kernel.
(2) JS engine: parses and executes JavaScript to achieve dynamic effects on web pages;
At first, the rendering engine and JS were not clearly distinguished. Later, the JS engine became more and more independent. , the kernel will tend to only render the engine.
Trident core: IE, MaxThon, TT, The World, 360, Sogou browser, etc. [Also known as MSHTML]
Gecko kernel: Netscape6 and above, FF, MozillaSuite/SeaMonkey, etc.
Presto kernel: Opera7 and above. [Opera core was originally: Presto, now: Blink;]
Webkit core: Safari, Chrome, etc. [Chrome: Blink (a fork of WebKit)]
EdgeHTML Core: Microsoft Edge. [This kernel is actually from MSHTML fork, and almost all IE private features have been deleted]
Tag closure, tag lowercase, No random nesting, improve the search probability of search robots, use external link css and js scripts, separation of structural behavior and performance, faster file download and page speed, content can be accessed by more users, and content can be accessed by a wider range of devices Access, less code and components, easy to maintain, easy to revise, no need to change the page content, provide a print version without copying the content, and improve the usability of the website;
HTML5 does not use SGML or XHTML. It is a brand new thing, so you do not need to refer to the DTD. For HTML5, you only need to place the following document type code to tell the browser to recognize that this is an HTML5 document
No, the browser will not recognize it as an HTML document, and the HTML5 tags will not work properly
The Datalist element in HTML5 helps to provide text box auto-complete feature, as shown in the following figure:
CSS column layout helps you split text into columns. For example, consider the following magazine news in a large text, but we need to divide it into 3 columns using boundaries between them. Here HTML5 column layout is Helpful
Set background-position:center;
Let a font be marked as italic (oblique). If there is no such format, use italic font
Inheritance: After an attribute is applied to a specific tag, the attribute should also be applied to the child tags of that tag. This behavior is called inheritance. But not all properties will be inherited. For example, text styles are generally inherited, but the width and height of box images are generally not inherited. In short, everything that should be inherited will be inherited, and anything that should not be inherited will not be inherited.
Reuse: A style file can be used on multiple pages, which is very useful for the reconstruction of some common styles.
Answer: It involves manpower, division of labor, and synchronization
(1) The early team must determine the global style, coding mode, etc.
(2) Writing habits must be consistent
(3) Mark the style writer, and mark each module in time (mark the place where the key style is called)
(4) Mark the page
(5) Css and html are stored in parallel in separate folders, and the naming must be unified
(6) Js are stored in folders, and the naming is based on the JS function. English translation
(7) Images are used in integrated .png format files and should be integrated as much as possible to facilitate future management.
Word-break:break-all;
The declaration is placed first in the document, before the tag.
It is not an HTML tag, it is used to inform (state) the web browser which HTML version is used on the page
Inline elements:
(1) Inline elements do not wrap
(2) Inline elements cannot be sized
( 3) The size of inline elements is determined by the content
Block elements :
(1) Block elements form independent lines
(2) Block elements can be sized
(3) If the width of a block element is not set, the width will adapt to the width of its parent
Line elements:
span, img, a, label, input, abbr (abbreviation), em (emphasis), big, cite (citation), i (italic), q ( Short quote), textarea, select (drop-down list), small, sub, sup, strong, u (underline), button (default display: inline-block)
As can be seen from the browser display results, Adjacent inline elements do not wrap, the width is the width of the content, and the four directions of padding are valid (as can be seen from the span tag, for inline non-replacement elements, the line height will not be affected, and the parent element will not be stretched; For replaced elements, the parent element will be stretched), margin is only valid in the horizontal direction (the vertical margin is valid for in-line replaced elements (such as img elements), and is invalid for in-line non-replaced elements), and the width and height attributes cannot be set. . Inline block elements behave the same as inline elements, except that they can set the width and height attributes.
Block elements:
p, p, h1~h6, ul, ol, dl, li, dd, table, hr, blockquote, address, table, menu, pre, HTML5 new header, section, aside, footer, etc.
It can be seen from the browser display results that block-level elements open a new row (even if the width attribute is set It also occupies an exclusive line), try to fill the width of the parent element, you can set the width and height attributes; the default display attribute of the table element browser is table.
(1) Find the parent corresponding to its label, and set text-align: center for its parent;
(2) Convert the element into a block element, set margin: 0 auto, (it must be a block element and have width)
Bold:
CSS: font- weight : bold
HTML : ,
Italic:
CSS: font-style: italic | oblique, font-style: normal (normal font)
HTML: ,
This is explained in Google:
The gap at the bottom of the picture actually involves the layout model of inline elements. The default vertical alignment of the picture is the baseline, and the position of the baseline is related to the font. So sometimes the gap at the bottom of the image might be 2px, and other times it might be 4px or more. Different font-size should also affect the size of this gap.
解决办法:
最优的解决办是定义vertical-align,注:定义vertical-align为middle时在IE6中大概还有一像素的顶边距,最好为top或bottom。当然还有种极端解决办法大家可以试试就是将 父容器的字体大小为零,font-size:0
(1)转化成(行级)块元素
display : block复制代码
(2)浮动,浮动后的元素默认可以转化为块元素(可以随意设置宽高属性)
float : left;复制代码
(3)给 img 定义 vertical-align(消除底部边距)
img{ border: 0; vertical-align: bottom; }复制代码
(4)将其父容器的font-size 设为 0;
(5)给父标签设置与图片相同的高度
父级的宽度不够,导致元素无法并排显示;
行内样式、内部样式(写一个style)、外部样式
padding margin复制代码
padding : 0; margin : 0;复制代码
word-spacing : 单词
letter-spacing : 字母、中文
text-decoration : underline | overline | line-through
list-style:none; background-image:url()复制代码
宽度(width)
颜色(color)
线形(solid,dashed,dotted,)实线,虚线,点画线
diaplay:block | inline-block
title :鼠标放入时提示的文字,
alt : 图片路径出错时,提示文字
<meta charset="UTF-8">
相关教程推荐:CSS视频教程
The above is the detailed content of 2019 CSS classic interview questions. For more information, please follow other related articles on the PHP Chinese website!