Px, EM, REM, VW, VH

1. Px (pixel, pixel) :

Dots Per Inch (DPI) is a virtual unit of length used in a digital image of a computer system. If you want to convert PX to physical length, you need to specify the precision DPI(Dots Per Inch), which is usually optional during scanning and printing. The default value is 96dpi for Windows and 72dpi for Apple.

2. Em (unit of relative length, relative to the font size of the text in the current object) :

A unit of relative length, originally the width of the letter M, hence the name em. It is a multiple of the character width, similar to percentage, for example: 0.8em, 1.2em,2em, etc. Usually 1 em = 16 px.

Rem (root EM):

Rem is a relative unit, 1rem is equal to the font size set on an HTML element. We can change the size represented by REM simply by setting the font size on the HTML. So we have a unified frame of reference that we can control. We now have two goals:


  • The rem unit represents a size proportional to the screen width, that is, setting the font size of an HTML element proportional to the screen width
  • Rem units and PX units are easy to convert, so we can write CSS according to annotations
The difference between REM and EM:
  • Rem is the font size relative to the root element (HTML), while EM is the font size relative to its parent element
  • Em is at most the last three decimal places

4. Vw, vh:

Css3 introduced a new unit vw/vh related to the view window, vw denotes the width relative to the view window, vh denotes the height relative to the view window, in addition to vw and Vh, there are two related units vmin and vmax. The specific meanings of each unit are as follows:

unit meaning
vw The window width is 100vw relative to the window width
vh The window height is 100vh relative to the window height
vmin Smaller values in VW and Vh
vmax Larger values in VW and VH

So here we see that the window width and height are all 100 VWS over 100vh, so vw or vh, vh for short, is very similar to the percentage units. The difference between VW and % is:

unit meaning
% Most are relative to ancestor elements, but also relative to themselves (border-radius, translate, etc.)
vw/vh Size relative to the window





Grail layout – Twin wings layout

Both the Holy Grail layout and the Twin Wings layout are adaptive web layouts with three fixed left and right columns and a fixed middle bar border

  • Three column layout, middle width adaptive, fixed width on both sides
  • The middle bar shows the rendering first in the browser
  • Allow the highest height of any column

Note: In the Holy Grail layout, the main element must be the first element in the container relative to the layout

HTML code:

<div class="container">
   <div class="main">main</div>
   <div class="left">left</div>
   <div class="right">right</div>
</div>Copy the code

1. Relative Layout:

.container {
  width: 100%;
  min-height: 300px;
  padding: 0 60px;
  box-sizing: border-box;
}
.container > div {
    position: relative;
    float: left;
}
.left, .right {
    width: 60px;
    height: 100%;
}
.left {
    left: -60px;
    margin-left: -100%;
}
.right {
    right: 0;
    margin-right: -100%;
}
.main {
    width: 100%;
    height: 100%;
}Copy the code

2. The flex layout:

.container {
    width: 100%;
    min-height: 300px;
    display: flex;
}
.main {
    flex-grow: 1;
}
.left {
    order: -1;
    flex-basis: 60px;
}
.right {
    flex-basis: 60px;
}Copy the code

3. Absolute Layout:

.container {
    width: 100%;
    min-height: 300px;
    position: relative;
}
.container > div {
    position: absolute;
}
.left, .right {
    width: 60px;
    height: 100%;
}
.main {
    width: calc(100% - 120px);
    height: 100%;
    left: 60px;
}
.left {
    left: 0;
}
.right {
    right: 0;
}Copy the code

Streaming layout vs. responsive layout

A streaming layout uses non-fixed pixels to define web content, known as a percentage layout, by setting the width of the box as a percentage to scale the rows according to the width of the screen. It is not limited by fixed pixels, and the content is filled to the side.

Reactive development utilizes Media Query in CSS3 to specify a web page layout of a width range by querying the width of the screen.

  • Ultra small screen (mobile device) under 768px
  • Small screen devices 768px to 992px
  • Medium screen 992px to 1200px
  • Widescreen devices above 1200px

Since responsive development is cumbersome, third-party responsive frameworks, such as Bootstrap, are generally used to complete part of the work. Of course, you can also write your own responsive frameworks.

Fluid layout Responsive development
The development way Mobile Web development +PC development Responsive development
Application scenarios Generally in the PC side of the site, the development of mobile only need to develop mobile side alone For some new websites, now required to adapt to the mobile terminal, so a set of pages compatible with various terminals
The development of Strong alignment, high development efficiency Compatible with various terminals, low efficiency
adapter For mobile devices only, the experience on the Pad is relatively poor Can be adapted to various terminals
The efficiency of Simple code, fast loading The code is relatively complex and slow to load


4. CSS priority algorithm

  • Element selector: 1
  • Class selector: 10
  • Id selector: 100
  • Element tag: 1000
  1. ! importantThe declared style has the highest precedence and is evaluated if it conflicts.

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

  3. Inherited styles have the lowest priority.

Five, CSS3 new pseudo class has those?

  • P :first-of-type Selects the first element belonging to its parent element
  • P :last-of-type Selects the last element belonging to its parent element
  • P :only of type Selects an element that is unique to its parent element
  • P :only-child Selects the only child element belonging to its parent element
  • P :nth-child(2) selects the second child belonging to its parent
  • :enabled :disabled Indicates that the form control is disabled.
  • : Checked check boxes or check boxes are selected.

CSS3 new features

  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: 5px 5px 5px #FF0000; (Horizontal shadow, vertical shadow, Blur distance, shadow color)

  5. Font -face property: Define your own font

  6. Rounded Corner (border radius) : The border-radius property 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: Defines two sets of CSS with different properties when the browser size changes

What are the methods of CSS optimization and performance improvement?

  1. Avoid over-constraining

  2. Avoid descendant selectors

  3. Avoid chain selectors

  4. Use compact syntax

  5. Avoid unnecessary namespaces

  6. Avoid unnecessary repetition

  7. It is best to use semantic names. A good class name describes what it is, not what it looks like

  8. Avoid!!! Important, you can select other selectors

  9. Simplify rules as much as possible. You can combine duplicate rules from different classes

CSS animation

CSS’s transform, Transition and animation are separated into three parts. Transfrom mainly controls the deformation of elements without the concept of time control, while Transition and animation are the parts of animation. They can control the effect of an element switching between two or more states at a time.

1.transition

The transition properties:

  • Transition-delay How long to delay the animation

  • Transition-duration Specifies the duration of a transition animation

  • Transition-property performs the corresponding properties of the animation, such as color, background, etc. You can use all to specify all properties

  • Transition-timing-function Computes the trajectory of the animation over time. Common examples are Linear, ease, ease-in, ease-out, cubic- Bezier (…). And so on.

Transition related events

The transitionEnd event is emitted when the Transition animation ends. Usually we perform some method after the animation, such as moving on to the next animation effect or something. Animation methods in Zepto.js are handled using CSS animation properties, and the callback after the animation runs should be handled using this event.

2.animation

Although Transition already provides great animation effects, we can only control the transition from one state to another, not multiple states. Animation helps us achieve this. The premise of using animation is that we need to use @keyframes to define an animation effect. The rules defined by @keyframes can be used to control the various states in the animation process. The syntax is basically like this:

@keyframes W { from { left: 0; top: 0; } to { left: 100%; top: 100%; }}Copy the code

The @keyframes keyword is followed by the name of the animation, followed by a block that contains the various selectors for the animation progress. The blocks behind the selectors are still the various CSS style properties we see all the time.

Animation attributes:

  • Animation-name Specifies the @keyframes name of the animation effect you want.

  • Animation-delay Is the same as transition-delay.

  • Animtaion-duration, like transition-duration, is the duration of the animation.

  • Animation-direction Controls one direction of an animation. The default is normal, and if the above left ranges from 0% to 100%, the default is left to right. If the value is reverse, it goes from right to left

Since the animation provides control over the loop, there are also alternate and alternate-reverse values, which will reverse the animation direction at the beginning of each loop, albeit in a different starting direction.

Animation related events

Several states of the animation can be listened to by binding events:

  • Animationstart the animationstart event, if delayed, will not be triggered until the animation actually starts, if not delayed, this event will be triggered when the animation effect is applied to the element.

  • Animationend The event at which the animation ends, similar to transitionEnd. If there are multiple animations, this event will fire multiple times, as in the example above, this event will fire three times. If animation-rotund-count is set to infinite, this event will not be emitted.

  • Animationiteration An animation cycles an end-of-life event. Unlike the previous event, this event is triggered at the end of each iteration instead of at the end of the entire animation. In an infinite loop, this event will fire indefinitely unless duration is 0

Nine, landing the

BFC stands for Block Formatting Context. It is a concept defined by the CSS2.1 specification regarding CSS rendering positioning. To understand what the BFC really is, let’s first look at what a visual formatting model is.

Visual formatting model

The Visual Formatting Model is the mechanism used to process documents and display them in visual media, and is also a concept in CSS.

The visual formatting model defines the generation of boxes, which include block boxes, inline boxes, anonymous boxes (boxes without names that cannot be selected by selectors), and some experimental boxes that may be added to the specification in the future. The type of box is determined by the display property.

How can A BFC be formed

  • The root element or other element containing it;
  • floating(elements offloatDon’t fornone);
  • Absolute location element(elements ofpositionforabsoluteorfixed);
  • Inline blocksinline-blocks(elements ofdisplay: inline-block);
  • Table cell(elements ofdisplay: table-cell, default attributes of HTML table cells);
  • overflowThe value of thevisibleThe element;
  • Flex boxes(elements ofdisplay: flexorinline-flex);
  • Overflow :hidden, float:left/right, position:absolute That is, each time you see these attributes, you represent the element and create a BFC.

    The browser has the following restrictions on the BFC area:

    1. The child elements that generate the BFC elements are placed one after the other.
    2. Vertically, they start at the top of a contained block, and the vertical distance between two adjacent child elements depends on the element’s margin properties. In the BFC, adjacent block-level elements have their margins collapsed.
    3. In children that generate BFC elements, the left margin of each child touches the left margin of the containing block (for right-to-left formatting, the right margin touches the right margin), even for floating elements (although the content area of the child element is compressed due to floating), Unless the child element also creates a new BFC (i.e., it is itself a floating element).

    The BFC is a separate container on the page, and the child elements inside the container do not affect the outside elements and vice versa. We can do a lot with this feature of the BFC.

    Flex layout

    The basic concept

    Elements with a Flex layout are called Flex containers, or “containers” for short. All of its child elements automatically become container members and are called Flex items, or “items.”



    The container has two axes by default: a horizontal main axis and a vertical cross axis. The starting position of the spindle (where it intersects with the border) is called main start and the ending position is called main end; The starting position of the intersecting axis is called cross start and the ending position is called cross end.

    By default, items are arranged along the main axis. The main axis space occupied by a single project is called main size, and the cross axis space occupied is called cross size.

    Container properties:

    1. Flex-direction: property determines the direction of the main axis (i.e. the alignment of items)

    • Row (default) : The main axis is horizontal and the starting point is on the left.
    • Row-reverse: The main axis is horizontal and the starting point is at the right end.
    • Column: The main axis is in the vertical direction, and the starting point is in the upper edge.
    • Column-reverse: the main axis is vertical and the starting point is at the bottom.

    2. Flex-wrap: By default, projects are arranged in a single line (aka “axis”)

    • Nowrap (default) : no line breaks.
    • Wrap: The first line is at the top.
    • Wrap-reverse: newline with the first line at the bottom.

    3. Flex-flow: short form of flex-direction and flex-wrap properties

    4. Justify -content: Defines the alignment of items on the main axis

    • Flex-start (default) : left-aligned
    • Flex-end: right-aligned
    • Center: a center
    • Space-between: both ends are aligned with equal intervals between items.
    • Space-around: Equal spacing on both sides of each item. As a result, the spacing between items is twice as large as the spacing between items and the border.

    5. Align -items: Define how items are aligned on the cross axis

    • Flex-start: Alignment of the starting point of the cross axes.
    • Flex-end: alignment of ends of crossed axes.
    • Center: Alignment of the midpoint of the cross axis.
    • Baseline: The baseline alignment of the first line of text of the project.
    • Stretch (default) : If the height is not set or auto is set, the project will occupy the entire height of the container.

    6. Align-content: Defines the alignment of multiple axes. This property has no effect if the project has only one axis

    • Flex-start: align with the starting point of the cross axis.
    • Flex-end: aligns with the end of the cross axis.
    • Center: aligns with the midpoint of the intersecting axis.
    • Space-between: aligned with both ends of the intersecting axes, with evenly distributed spacing between axes.
    • Space-around: The spacing on both sides of each axis is equal. Therefore, the spacing between axes is twice as large as the spacing between axes and borders.
    • Stretch (default) : Axis takes up the entire cross axis.

    Project attributes

    1. The order attribute defines the order of items. The smaller the value is, the more advanced it is. The default value is 0.
    2. The Flex-shrink attribute defines the scale by which a project shrinks. The default is 1, that is, if there is insufficient space, the project shrinks.
    3. The Flex-basis property defines the main size of the project before allocating extra space.
    4. The flex attribute is short for flex-grow, flex-shrink, and flex-basis. The default value is 0 1 Auto.
    5. The align-self property allows a single item to have a different alignment than other items, overriding the align-items property.

    This property may take six values, all identical to the align-items property except auto.

    Summary of Grid layout

    Grid layout is a new layout in CSS that has strong control over the location and size of boxes and box contents. Unlike Flex, which focuses on a single axis, grid ADAPTS to multiple axes, so here’s a brief introduction.

    Flex Layout



    Grid Layout



    The basic concept

    Set up the
    display: grid;Is called a container, and its immediate child elements are called items, but note that descendant elements are not items.
    • Grid line: a structure that forms a grid layout, either horizontal or vertical.
    • Grid track: The space between two adjacent grid lines, thought of as a row or a column.
    • Grid cell: a space consisting of dividing lines between two adjacent rows and columns. It is a cell in the grid layout.
    • Grid area: the entire space covered by four grid lines. Any number of grid cells form a grid area.

    Container attribute

    The grid container has a lot of properties, 18 in all, but many of them can be abbreviated, so don’t get too nervous.

    • The grid – the template series
      • grid-template-columns
      • grid-template-rows
      • grid-template-areas
    • The grid – gap series
      • grid-column-gap
      • grid-row-gap
    • Place – items series
      • justify-items
      • align-items
    • Place – the content series
      • justify-content
      • align-content
    • The grid series
      • grid-auto-columns
      • grid-auto-rows
      • grid-auto-flow

    Twelve, box – sizing

    Set the CSS box model to the standard or IE model. The width of the standard model only includes content, while the width of the IE model includes border and padding box-sizing attributes, which can be one of three values:

    • Content-box, the default, only counts the width of the content, not the border and padding
    • Padding-box, the padding goes into the width
    • Border-box, border, and padding are calculated into the width

    Hardware acceleration

    Sometimes animations can cause the user’s computer to stall, and you can avoid this problem by using hardware acceleration in certain elements:

    .block {
        transform: translateZ(0);
    }
    Copy the code

    You won’t notice a difference, but browsers do 3D hardware acceleration for this element, which is useful until the special will-change attribute is fully supported.

    (Continuously updated…)

    reference

    Talk about CSS3 length units (VH, VW, REM)

    CSS classic layout – The Holy Grail layout

    In-depth understanding of BFC

    Flex layout tutorial: Syntax

    , etc.