Rem unit
- Rem (root EM) is a relative unit, similar to EM, which is the font size of the parent element
- Em is different from REM
- Rem is based on the font size relative to the < HTML > element for example: the root element HTML setting font size=12px; Width: 2rem; Converting to PX means 24px
- Rem’s advantage: The parent element text size may be inconsistent, but the entire page has only one < HTML >, which is a good way to control the element size ratio of the entire page.
Media queries
-
Media Query is a new CSS3 syntax
- Using @media queries, you can define different styles for different media types
- @Media can set different styles for different screen sizes
- When you resize the browser, the page will also be rerendered according to the width and height of the browser
- At present, multimedia query is used for many apple phones, Android phones, tablets and other devices
-
Grammar specification
- @ media at the beginning
- Mediatype mediatype
- Different terminal devices are divided into different media types, as shown in the following table:
value explain all All the equipment print Printer and print preview screen Computer screens, tablets, smartphones, etc
- Keyword and not or only
- Meaning: Concatenates media types or multiple media features as the condition for a media query.
- And: multiple media features can be joined together, equivalent to and
- Not: Excludes a media type. It is equivalent to “not” and can be omitted
- Or: Can test multiple media query conditions, as long as one condition is true, “or” meaning
- Only: specifies a specific media type, which can be omitted
-
Media feature The media feature must be wrapped in parentheses as shown in the following table:
value explain width The width of the page visible area on the output device min-width The minimum visible area width of a page on an output device max-width Maximum visible area width of a page on an output device For example:
<! -- < span style = "max-width: 100%; clear: both; min-height: 1em;@media screen and (min-width: 600px;) {<! The internal style is the same as writing CSS styles.body { background-color: pink; }}Copy the code
-
Example: Change background color depending on page width
- Implementation idea:
- Recommendation: From small to big
- Note: both max-width and min-width contain equal to
- Screen smaller than 540 pixels, background color blue (x<=539)
- The background color is green for screens greater than or equal to 540 pixels and less than or equal to 969 pixels (540 =< x >= 969)
- The background color is red for screens greater than or equal to 970 pixels (x >= 970)
- Implementation idea:
-
Summary: each attribute must be blank, such as: and after the space, otherwise the style will not come out
-
Media queries and REM implement element changes
- Rem units follow < HTML >, and with REM page elements you can set different sizes
- Media queries can be styled for different device widths
- Media query + REM can achieve different device width, dynamic change of page element size
- case
-
Introducing resources (understanding)
- Determine the size of the device in <link> and then refer to different CSS files for example:
<link rel="stylesheet" href="styleA.css" media="screen and (min-width: 400px)"> Copy the code
- Determine the size of the device in <link> and then refer to different CSS files for example:
Less basis
- Disadvantages of maintaining CSS
- CSS is a non-procedural language without concepts such as variables, functions, and scopes
- CSS requires a large amount of seemingly illogical code to be written, and CSS redundancy is relatively high.
- Not convenient maintenance and expansion, not conducive to reuse
- CSS does not have great computing power
- For non-front-end developers, it is often difficult to write well-organized and maintainable CSS code projects because of their lack of CSS writing experience
- CSS is a non-procedural language without concepts such as variables, functions, and scopes
- Less is introduced
- Less(Leaner Style Sheets) is a CSS extension language that is also known as the CSS preprocessor
- As a form of CSS extension, it does not reduce the functionality of CSS, but adds procedural language features to the existing CSS syntax
- On the basis of the syntax of CSS, variables,Mixin, operations and functions are introduced, which greatly simplifies the writing of CSS. It also lowers CSS maintenance costs, and as its name suggests,Less allows us to do more with Less code
- Chinese website: >lesscss.cn/
- Common CSS preprocessors: Sass,Less,Stylus
- Summary :Less is a CSS preprocessor language that extends the dynamic features of CSS
- Less installation
- steps
- Install nodejs > nodejs.org/en/download…
- Check whether the installation is successful
- Run CMD (Widows +r) and enter “node-v” to view the version
- Run the CMD “NPM install -g Less” command to install Less online based on Node. js
- Run the lessc-v command to check whether the installation is successful
- steps
- Less use of
-
Less variables (colors and values commonly used in CSS, etc.)
- @ Variable name: value
- Naming conventions
- Must be prefixed with @
- Contains no special characters
- You can’t start with a number
- Case sensitivity
- Specification for variable use
body { color: @color; } a:hover { color: @color; } Copy the code
-
Less compilation
- Essentially,Less consists of a custom syntax and a parser that allows users to define their own style rules, which are compiled into corresponding CSS files by the parser.
- The.less file must be compiled into a.css file before the HTML page can be used
- Use the command “lessc style.less > style.css” in the current folder as shown in the following figure:
- If you save the Less file, the CSS file will be automatically generated
-
Less nested
- Common nesting
<! -- Common selectors -->#header .logo { width: 300px; } <! < span style = "max-width: 100%; clear: both; min-height: 1em;#header { .logo { width: 300px; }}Copy the code
- Intersection/pseudo-class/pseudo-element selector
- If the inner selector is not preceded by an ampersand, it is resolved to be a descendant of the parent selector
- If there is an ampersand, it is resolved to the parent element itself or to a pseudo-class of the parent element.
Such as:
<! -- Common selectors -->a:hover { color: red; } <! < span style = "max-width: 100%; clear: both; min-height: 1em;a{&:hover { color: red; }}Copy the code
- Common nesting
-
Less operation
- Meaning: Any number/color or variable can be used in the operation. Less provides arithmetic operations of addition (+), subtraction (-), multiplication (*), and division (/)
- Note:
- For an operation between two values of different units, the resulting value takes the units of the first value.
- If only one value between two values has a unit, the result of the operation takes that unit
- The operator is separated by a space to the left and right of 1px + 5rem
-
Rem adaptation scheme
-
Think about:
-
What is the purpose of our adaptation? A: Make some elements that are not equally adaptive, so that when the device size changes, they are equally adaptive to the current device.
-
How will it be used in real development? A: Use media query to set the HTML font size proportionally for different devices, and then use REM as the size unit for page elements. When the HTML font size changes, the element size will also change, so as to achieve proportional scaling adaptation.
-
-
Rem actually develops adaptation solutions
- Dynamically calculate and set the font size of the HTML root tag according to the ratio of the design draft to the width of the device. (Media Enquiries)
- In the CSS, the width, height, and relative position of design elements are converted to rem in the same proportion
-
Use of REM adaptation technology (mainstream market)
-
Technical Solution 1
- less
- Media queries
- rem
-
Technical Solution 2
- flexible.js
- rem
-
-
Conclusion: Scheme 2 is simpler, so you don’t need to know the JS code at this stage
-
Rem + media query +less technology
-
Common dimensions and widths of design draft (as shown below)In general, we fit most screens with one or two sets of renderings, giving up extreme screens or downgrading them gracefully, sacrificing some effects.750 is now the standard.
-
Dynamically set the FONT size of the HTML tag (750 as an example), as shown in the figure
-
Element size method
* Final formula: The rem value of the page element = the page element value (px)/(screen width/partition number) * screen width/partition number is the HTML font size * or the REM value of the page element = the page element value (px)/HTML font sizeCopy the code
-
Independently completed suning mobile terminal home page
- Give the A tag a location and overlay it on top of the search box
- Float \display\position\ three position method