directory
- Less grammar
- annotation
- Variables (rules, usage)
- Minxin mixed with
- The name of the class with
- Function with
- Functions with arguments are mixed in
- nested
- Import (reference)
- Functions (built-in functions and operations)
- Less defines functions
- Built-in function
- String function
- Synthetic class function
- The arithmetic function
- The color function
- Judging function
The syntax of Less is as follows: 1. The syntax of Less is as follows: the syntax of Less is as follows:
Less grammar
annotation
/ * * /
This annotation can be used in CSS or less//
This annotation can be used in less, but there is no way to compile this type of annotation because CSS does not support itcss
In the file
Variable
- Variables allow us to define a set of generic styles that can be called when needed, so that we only need to modify the global variable, which is very convenient.
- Rules:
less
Variable to@
The prefix cannot start with a number or contain special characters. - Use:
/* defines a variable */
@mainColor:#ff4400;
/* Use the variable */
a:hover{
color:@mainColor;
}
p{
border: 1px solid @mainColor;
}
/* You can concatenate variables */
@left:left;
/* Wrap curly braces around */
border-@{left}: 1px solid @mainColor;
Copy the code
Mixins with
What is mixin? This is something you see a lot in Bootstrap. Mixin can easily introduce A defined class A into class B, so that class B inherits all the attributes of class A. When you define it, you need to dot it.
The name of the class with
So if I define a button1, and I have some properties in the button, and I have a button2 somewhere else, and I want to add the properties of that button1 and some other properties, if I have to rewrite it the way CSS works, THEN I can mix it in with Less. Here’s the code:
/* Some properties of the original button and other properties */
.button1{
width:200px;
height: 50px;
background: #fff;
}
.btn_border{
border: 1px solid #ccc;
}
.btn_danger{
background: red;
}
.btn_block{
display: block;
width: 100%;
}
/* Mix the styles of CSS classes */
.button2{
.button1(a);.btn_border(a);.btn_danger(a);.btn_block(a); }Copy the code
Function with
There are some bad things about the mixin above, but it is still compiled in button2. Everything in button1 is compiled in button2, but it is written succinctly. This is where you can mix in functions. (Detailed function knowledge is written later)
.button1() {width:200px;
height:200px;
background: #fff;
}
.btn_border() {border:1px solid #ccc;
}
.button2{
.button1(a);.btn_border(a); }Copy the code
This way.button1 and.btn_border are not displayed in the CSS file, reducing code redundancy in the CSS. But what if I want to change the exact value of the style? It’s parameters
Functions with arguments are mixed in
If there is no default value, the call must pass a value as follows:
/* less will return an error */ if no value is passed
.btn_border(@len) {border:1px solid #ccc;
}
.btn_b{
.btn(a);.btn_border(a); }Copy the code
Two solutions
1. Pass in default values at definition time
/*1. Pass the default value */ at definition time
.btn_border(@len:10px) {border:1px solid #ccc;
border-radius:@len;
}
.btn_b{
.btn(a);.btn_border(a); }/* The final border-radius is 10px*/
.btn_b{
.btn(a);.btn_border(20px);
}
/* If a parameter is passed in, the final value is 20px*/
Copy the code
2. There is no default value when defining, and the value is passed in when calling
.btn_border(@len){
border:1px solid #ccc;
border-radius:@len;
-webkit-border-radius:@len;
-moz-border-radius:@len;
-ms-border-radius:@len;
-o-border-radius:@len;
}
.btn_b{
.btn();
.btn_border(10px);
}
Copy the code
nested
Nesting enhances the hierarchy of the code, and we can also implement inheritance through nesting, which greatly reduces the amount of code and makes the amount of code look cleaner.
.nav{
border-bottom: 1px solid #ccc;
font-size: 12px;
color:# 666;
a {
color:# 666;
}
> .container{
line-height: 40px;
text-align: center;
> div{
height:40px;
~ div{
border-left:1px solid #ccc; }}}}Copy the code
This time to pay attention to a problem, that is, if you write pseudo-elements, intersection selector, there will be a problem, write directly in the middle of the nested will default space. So how do you put it together?
/* This is how to write pseudo-elements and intersection selectors, preceded by &*/
.nav{
font-size:12px;
&:hover{
text-decoration:none;
}
&::before{
content:""; }}Copy the code
Import (reference)
When using less to write files, you can write a module of less, but the index. HTML to reference so many less files is not appropriate, this time to establish a total less, this is a decoupled development idea, “high cohesion, low coupling”.
Let’s sort out the structure:
< modules needed in less >
- variable
- function
- Functional modules (depending on your situation)
- Total (reference variable, function, function module)
It is first introduced in index.less
/*index.less*/
@import "variable.less"; /* Introduces the variable, which can be followed by the suffix */
@import "mixin.less"; /* Introduces the function */
@import "topBar.less"; /* Import function module 1*/
@import "navBar.less"; /* Introduce function module 2*/
Copy the code
In index. HTML, you need to introduce an index.less value
Functions (built-in functions and operations)
There are two ways to define functions in JS
- function fun(){ }
- var fun = function(){ }
Less defines functions
Direct operation
a{
color:red/2; /* The result is #800000*/
}
li{
width:100%/7; /* Each LI label is 1/7 the width of ul */
}
Copy the code
It’s defined at the beginning, and it’s defined when it’s used. The function name ()
.a(@len:12) {width:100%/@len;
color:lighten(#ddd.10%); /* Brightness increases by 10%, resulting in # f7F7f7 */
}
col-xs-1{
.a(a); }Copy the code
Built-in function
There’s a function manual
Note: functions with * are only available in 1.4.0 beta and above!!
String function
escape(@string)
; / / byURL-encoding
Encoding stringe(@string)
; // Escape the string%(@string, values...) ;
// Format a string
Synthetic class function
unit(@dimension, [@unit: ""]);
// Remove or replace units of attribute valuescolor(@string);
// Parse the string into a color value* data-uri([mimetype,] url);
//*
Embed the resource tocss
In, maybe back tourl()
The arithmetic function
ceil(@number);
// round upfloor(@number);
// round downpercentage(@number);
// Convert numbers to percentages, for example0.5 - > 50%
round(number, [places: 0]);
// Round up (the second is precision)* sqrt(number);
// Calculate the square root of the number.* abs(number);
// The absolute value of a number (the argument can take units)* sin(number);
//sin
Function (no default radian value in units)
sin(1deg); // Sine of 1 Angle 0.01745240643728351
sin(1grad); // Sine of 1 degree Angle 0.015707317311820675
/* The gradient is the division of a circle into 400 parts, each gradian, grad*/
Copy the code
* asin(number);
//arcsin
function- parameter
- 1
to1
Floating point number between, return value in radians, interval between-PI/2
到PI/2
Between, out of range outputNaNrad
- parameter
* cos(number);
//cos
function- with
sin
Function the same
- with
* acos(number);
//arccos
function- parameter
- 1
to1
Floating point number between, return value in radians, interval between0
在PI
Between, out of range outputNaNrad
- parameter
* tan(number);
//tan
function- with
sin
Function the same
- with
* artan(number);
//arctan
function- The return value ranges from
-PI/2
到PI/2
Between, the rest andarsin
The same
- The return value ranges from
* pi();
/ / returnPI
* pow(@base, @exponent);
/ / return@base
the@exponent
To the power- The return value and
@base
If there are identical units, the second unit is ignored and returned without conforming to the ruleNaN
- The return value and
* mod(number, number);
// The first argument mod the second argument- The return value is the same as the unit of the first argument, and can handle negative numbers and floating-point numbers
* convert(number, units);
// Convert between numbers* unit(number, units);
// Replace units of numbers without conversion
The color function
color(string);
// Convert strings or escaped values to colors- See synthesis function
rgb(@r, @g, @b);
// Convert to a color value- Parameters are integers
0-255.
Percentage,0-100%
Convert to hexadecimal
- Parameters are integers
rgba(@r, @g, @b, @a);
// Convert to a color value- The first three parameters are integers
0-255.
Percentage,0-100%
The fourth is0-1
Or percentage0-100%
.
- The first three parameters are integers
argb(@color);
/ / create#AARRGGBB
Format the color value- Used in the
IE
In the filter, and.NET
andAndroid
The development of
- Used in the
hsl(@hue, @saturation, @lightness);
// Create a color value- By hue, saturation, brightness three values
@hue
: an integer0-360.
Said degree@saturation
The percentage:0-100%
Or digital0-1
@lightness
The percentage:0-100%
Or digital0-1
- Returns the hexadecimal color value
/* This is handy if you want to use one color to create another */
@new: hsl(hue(@old),45%.90%);
/* Here @new will use @old's hue value, along with its own saturation and brightness */
Copy the code
hsla(@hue, @saturation, @lightness, @alpha);
// Create a color valuehsv(@hue, @saturation, @value);
// Create a color value@hue
Represents hue, integer0-360.
Said degree@saturation
It’s saturation, percentage0-100%
Or digital0-1
@value
Hue, percentage0-100%
Or digital0-1
- Creates an opaque color object
hsv(90.100%.50%)
/ / o # 408000
Copy the code
hsva(@hue, @saturation, @value, @alpha);
// Create a color valuehue(@color);
// Extract from the color valuehue
Value (hue)- The return value
0-360.
The integer
- The return value
hue(hsl(90.100%.50%))
/ / output 90
Copy the code
saturation(@color);
// Extract from the color valuesaturation
Value (saturation)- Return value percentage
0-100.
- Return value percentage
saturation(hsl(90.100%.50%))
/ / output by 100%
Copy the code
lightness(@color);
// Extract from the color value'lightness'
Value (brightness)- return
0-100.
Percentage value of
- return
lightness(hsl(90.100%.50%))
/ / output by 50%
Copy the code
* hsvhue(@color);
// Extract from colorhue
Value toHSV
Color space representation (hue)- return
0-360.
The integer
- return
* hsvsaturation(@color);
// Extract from colorsaturation
Value toHSV
Color space representation (Saturation)- return
0-100.
The percentage
- return
* hsvvalue(@color);
// Extract from colorvalue
Value toHSV
Color space representation (tone)- return
0-100.
The percentage
- return
red(@color);
// Extract from the color value'red'
Value (red)- Returns an integer
0-255.
- Returns an integer
green(@color);
// Extract from the color value'green'
Value (green)- Returns an integer
0-255.
- Returns an integer
blue(@color);
// Extract from the color value'blue'
Value (blue)- Returns an integer
0-255.
- Returns an integer
alpha(@color);
// Extract from the color value'alpha'
Value (transparency)- Return floating point
0-1
- Return floating point
luma(@color);
// Extract from the color value'luma'
Value (percentage of brightness)- Return percentage
0-100%
- Return percentage
Color Operations
There are several points to note in color value calculation:
- Parameters must be in the same unit/format
- Percentages will be treated as absolute values,
10%
increase10%
, it is20%
Rather than11%
- Parameter values must be within a limited range
- Returns a value in a simplified format except hexadecimal
saturate(@color, 10%);
// Saturation increases10%
desaturate(@color, 10%);
// Saturation decreases10%
lighten(@color, 10%);
// Brightness increases10%
darken(@color, 10%);
// Brightness decreases10%
fadein(@color, 10%);
// Opacity is increased10%
And more opaquefadeout(@color, 10%);
// The opacity is reduced10%
And more transparentfade(@color, 50%);
// Set transparency to50%
spin(@color, 10);
// The hue value increases10
- Rotate the hue Angle of the color in any direction
0-360.
, will continue to rotate from the starting point, such as rotation360
and720
It’s the same result. - It’s important to note that the color goes through
RGB
This process does not preserve the hue value of the gray (gray has no saturation and the hue value is meaningless), so the hue value of the color is preserved by the function - Because the color value is always printed as
RGB
Format, thereforespin()
Function has no effect on gray
- Rotate the hue Angle of the color in any direction
mix(@color1, @color2, [@weight: 50%]);
// Mix the two colors- The third parameter is the percentage to balance the two colors, which is the default
50%
- The third parameter is the percentage to balance the two colors, which is the default
greyscale(@color);
// Remove saturation completely and output grey- with
desaturate(@color, 100%)
The effect is the same
- with
contrast(@color1, [@darkcolor: black], [@lightcolor: white], [@threshold: 43%]);
/ / if@color1
的luma
Value >43%
The output@darkcolor
, otherwise output@lightcolor
- It’s a color comparison, it’s a little bit more complicated, and we’ll talk about it when we use it
Color blending (Color blending)
Color blending modes are similar to the layer blending modes of image editors Photoshop, Firework, or GIMP, so the same color blending modes used to create.psd files can be applied to CSS.
multiply(@color1, @color2);
- Put the two colors separately
RGB
Multiply the three values and divide by them255
The output is a darker color - The corresponding
ps
“Darken/Multiply” in
- Put the two colors separately
screen(@color1, @color2);
- The result is a brighter color, corresponding to
ps
“Lighten/filter” in
- The result is a brighter color, corresponding to
overlay(@color1, @color2);
- Combine the effects of the previous two functions to make the shallow one shallower and the deep one deeper, similar to
ps
Superposition in - The first argument is the superimposed object, and the second argument is the color to be superimposed
- Combine the effects of the previous two functions to make the shallow one shallower and the deep one deeper, similar to
softlight(@color1, @color2);
- with
overlay
The effect is similar, except that when pure black and pure white are used as parameters, the output result is not pure black and white, corresponding tops
“Soft Light” in - The first parameter is the blending color (light source) and the second parameter is the color to be blended
- with
hardlight(@color1, @color2);
- with
overlay
The effect is similar, but the second color parameter determines the brightness or blackness of the output color, corresponding tops
Bright light/bright light/linear light/dot light - The first parameter is the blending color (light source) and the second parameter is the color to be blended
- with
difference(@color1, @color2);
- Subtracting the second color from the first color value, the output is a darker color, corresponding to
ps
“Difference/exclusion” in - The first argument is the subtracted color object, and the second argument is the subtracted color object
- Subtracting the second color from the first color value, the output is a darker color, corresponding to
exclusion(@color1, @color2);
- Effect and
difference
The function is similar, but the output is less different,ps
“Difference/exclusion” in - The first argument is the subtracted color object, and the second argument is the subtracted color object
- Effect and
average(@color1, @color2);
/ / forRGB
Average the three colorsnegation(@color1, @color2);
- and
difference
The function does the opposite, producing a brighter color - Note: doing the opposite does not mean doing the addition
- The first argument is the subtracted color object, and the second argument is the subtracted color object
- and
Judging function
iscolor(@colorOrAnything);
// Determine if a value is a colorisnumber(@numberOrAnything);
// Check whether a value is a number (can contain units)isstring(@stringOrAnything);
// Determine if a value is a stringiskeyword(@keywordOrAnything);
// Determine if a value is a keywordisurl(@urlOrAnything);
// Check whether a value isurl
ispixel(@pixelOrAnything);
// Check whether a value is apx
Is the value of the unitispercentage(@percentageOrAnything);
// Determine if a value is a percentageisem(@emOrAnything);
// Check whether a value is aem
Is the value of the unit* isunit(@numberOrAnything, "rem");
// Check whether a value is the value of the specified unit