10 high-frequency interview questions about css in web front-end interviews
coldplay.xixi
Release: 2020-08-03 17:05:04
forward
2804 people have browsed it
1. What is the BFC mechanism
BFC (Block Formatting Context), Block-level formatting Context is an independent rendering area that isolates elements inside the BFC from external elements so that the positioning of internal and external elements will not affect each other.
Trigger condition (any one of the following)
The value of float is not none
The value of overflow is not visible
The value of display is One of table-cell, table-caption and inline-block
The value of position is not any one of static or releative
Under IE, Layout can be triggered through zoom:1
The difference between BFC layout and ordinary document flow layout:
BFC layout rules:
Floating elements will have their height calculated by their parent (the parent element triggers BFC)
Non-floating elements will not cover the position of floating elements (non-floating elements trigger BFC) )
Margin will not be passed to the parent (the parent triggers BFC)
The upper and lower margins of two adjacent elements belonging to the same BFC will overlap
Normal document flow layout : The height of floating elements will not be calculated by the parent
Non-floating elements will cover the position of floating elements
margin will be passed to the parent element
Two adjacent The margins above and below the element will overlap
Application under development
Prevent margin overlap
You can include floating elements - clear internal floats (clear floating ones The principle is that both p's are located in the same BFC area)
Adaptive two-column layout
can prevent elements from being covered by floating elements
are just listed here. For specific usage, please check my article about new selectors and attributes in CSS3
Attributes Selector
##Attribute Selector
Meaning description
#E[att^="val"]
The element whose attribute att value starts with "val"
E[att$="val"]
The element whose attribute att value ends with "val"
##E[att*="val"]
The value of the attribute att contains the "val" string element
Structure pseudo-class selector
##Selector
Meaning Description
##E :root
matches the root element of the document, which for HTML documents is the HTML element
##E:nth-child(n)
Matches the nth child of its parent element Element, the first number is 1
##E:nth-last-child(n)
Matches the nth child element from the bottom of its parent element, the first number is 1
E:nth-of-type(n)
##Similar to :nth-child() , but only matches elements using the same tag
#E:nth-last-of-type(n)
Similar to :nth-last-child(), but only matches elements using the same tag
E:last-child
## Matches the last element of the parent element Child element, equivalent to:nth-last-child(1)
##E:first-of-type
Matches the first child element using the same tag under the parent element, which is equivalent to: nth-of-type(1)
##E:last-of-type
## Matches the last child element using the same tag under the parent element, which is equivalent to: nth-last-of-type(1)
E:only-child
Matches the only child element under the parent element, which is equivalent to: first-child:last- child or:nth-child(1):nth-last-child(1)
##E:only-of -type
matches the only child element using the same tag under the parent element, which is equivalent to: first-of-type:last-of- type or:nth-of-type(1):nth-last-of-type(1)
##E :empty
##matches an element that does not contain any child elements. Note that text nodes are also considered child elements
css3 new attributes
##Attributes
Meaning Description
Compatible
transition
##Set transition effect
##transform
Transformation effects (move, scale, rotate, stretch or stretch)
##animation
##Animation effect
##box-shadow
Shadow Effect
FF3.5, Safari 4, Chrome 3
##text-shadow
##Text-shadow
FF 3.5, Opera 10, Safari 4, Chrome 3
##border -colors
##Set multiple colors for the border
## FF3
boder-image
Picture Border
##FF 3.5, Safari 4, Chrome 3
text-overflow
Text truncation
IE6 , Safari4, Chrome3, Opera10
##word-wrap
Automatic line wrapping
##IE6, FF 3.5, Safari 4, Chrome 3
##border-radius
circle Corner Border
##FF 3, Safari 4, Chrome 3
opacity
##opacity
all
##box-sizing
Control box model composition mode
FF3, Opera 10, Safari 4, Chrome 3
outline
##Outer border
FF3 , safari 4, chrome 3, opera 10
# #background-size
Do not specify the size of the background image
##safari 4, chrome 3, opera 10
##background-origin
Specify where to start displaying the background image
##safari 4, chrome 3, FF 3
background-clip
Specify background Where to start cropping the picture
safari 4, chrome 3
rgba
Set the color value based on the three color channels of r, g, and b, through a Set transparency
safari 4, chrome 3, FF3, opera 10
3. Centered layout
Horizontal centering
Inline elements: text-align:center
Block-level elements: margin:0 auto
Absolute positioning and movement: absolute transform
Absolute positioning and negative margins: absolute margin
flex layout: flex justify-content:center
Vertical centering
The child element is a single line of text : line-height:height
4. What are the methods for clearing floats, and what are the advantages and disadvantages of each?
Use an empty element with the clear attribute and use an empty element after the floating element, such as
, and assign .clear{clear:both;} attribute in CSS to clear the float. You can also use <br class="clear" /> or <hr class="clear" /> for cleaning.
Advantages: Simple, write a small amount of code, good compatibility Disadvantages: Adding unsemantic html elements is not conducive to code semantics, and the later maintenance cost is high
Use the overflow attribute of css to add
overflow:hidden;
or overflow:auto; to the container of the floating element to clear the float. In addition, hasLayout needs to be triggered in IE6, for example Set the container width and height for the parent element or set zoom:1. After adding the overflow attribute, the floating element returns to the container layer, raising the height of the container, achieving the effect of cleaning up the floating elements.
Advantages: simple, less code, good browser support Disadvantages: cannot be used with position, because the excess size will be hidden
overflow:hidden
Using CSS's :after pseudo-element combined with the :after pseudo-element (note that this is not a pseudo-class, but a pseudo-element, representing the nearest element after an element) and IEhack, it is perfectly compatible with the current mainstream For major browsers, IEhack here refers to triggering hasLayout. Add a
clearfix
class to the container of the floating element, and then add an :after pseudo-element to this class to add an invisible block element (Block element) to the end of the element to clear the float. An invisible space "020" or dot "." is added to the end of the internal element of the container through CSS pseudo-elements, and the clear attribute is assigned to clear the float. It should be noted that for IE6 and IE7 browsers, a zoom:1; must be added to the clearfix class to trigger haslayout.
Advantages: Good browser support, not prone to strange problems (currently: used by large websites, such as: Tengxun, NetEase, Sina, etc.) Disadvantages: A lot of code, requiring Only by combining the two lines of code can mainstream browsers support
Set the height of the parent element
Simple, less code, easy to master Disadvantages: Only suitable for fixed-height layouts
5. What is the principle of creating a triangle using pure CSS
When writing triangles before, they were written directly. I was stuck in code and didn’t explore the reasons. It wasn’t until I had an interview that the interviewer asked me to talk about the principle of creating triangles with css. I just... came back and quickly looked through the information. Next, I explained what I understood at the time. The process is listed:
1. Write a border application we are most familiar with
The above is the detailed content of 10 high-frequency interview questions about css in web front-end interviews. For more information, please follow other related articles on the PHP Chinese 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 admin@php.cn