current position:Home>Read pseudo classes and pseudo elements
Read pseudo classes and pseudo elements
2022-04-29 16:28:05【Ziu】
The content of this paper is summarized from
MDN
CSS Selectors
, To learn more, please check the original text .
What is a pseudo-class ?
Pseudo classes are added to selectors keyword , Specifies the special state of the element to be selected .
Typical pseudo class keywords
in the majority of cases , Pseudo classes are used with base selectors , More pseudo classes MDN - Standard pseudo class index
The following is the application of pseudo classes in some typical scenarios .
:hover
Pointer in <button>
Hover up , But when it's not activated , The button color changes to blue
button:hover {
color: blue;
}
Copy code
Be careful : On the touch screen
:hover
Basically not available . On different browsers:hover
Behave differently . Web developers should not allow any content to be displayed only by hovering , Otherwise, these contents are difficult or impossible for touch screen users to see .
:not
:not()
Used to match elements that do not match a set of selectors . Because its function is to prevent specific elements from being selected , It's also called Reverse selection of pseudo class (negation pseudo-class).
Will all not <p>
Change the color of the element to blue :
body:not(p) {
color: blue;
}
/* The following code has no effect */
:not(p) {
color: blue;
}
Copy code
Be careful : On the touch screen
:hover
Basically not available . On different browsers:hover
Behave differently . Web developers should not allow any content to be displayed only by hovering , Otherwise, these contents are difficult or impossible for touch screen users to see .
:first-child
To all <ul>
Next first <li>
Apply different styles
ul li {
color: blue;
}
ul li:first-child {
color: red;
font-weight: bold;
}
Copy code
:active
:active
It shows the time from pressing the mouse to releasing it , The following code indicates <a>
Styles in different states .
a:link { color: blue; } /* No access to the link */
a:visited { color: purple; } /* Visited link */
a:hover { background: yellow; } /* User mouse over */
a:active { color: red; } /* Activate Links */
Copy code
Be careful :
:active
The style given may be overridden by other link related pseudo classes declared later . To ensure that the style is effective , Need to put:active
The style of is placed after all link related styles .
This order of linking pseudo classes is called LVHA The order ::link
> :visited
> :hover
> :active
.
Pseudo class keywords used alone
Our common pseudo class keyword is characterized by a single colon :
start , Follow the base selector .
But the pseudo class keyword used alone can also have an effect on the page , for example :
adopt :focus
pseudo-classes , You can change the color of any element to red after it gets the focus .
:focus {
color: red;
}
Copy code
What are pseudo elements ?
The pseudo element is a Keywords appended to the end of the selector , Allows you to modify the style for a specific part of the selected element .
Same as pseudo class , Only one pseudo element can be used in a selector . however , The pseudo element must immediately follow the underlying selector in the statement .
for example , The following code can be given to each... In the page <a>
Add an expression before the label .
a::before {
content: "";
}
Copy code
Compared to pseudo classes , The use of pseudo elements is more fixed , Its basic grammar :
selector::pseudo-element {
property: value;
}
Copy code
Be careful : When writing pseudo elements , You'll see a single colon
:
Writing , But this is CSS2 Outdated Syntax , Only used to support IE8, In most cases, please write double colons::
To represent pseudo elements .
Pseudo classes and pseudo elements are used together
In the following case , Both pseudo classes and pseudo elements are used :
use CSS Implement a switch style ,hover Trigger when , The slider is square , The specific size is optional , Here's the picture
Try to meet the following requirements , Can realize part of :
- The switch action is animated excessively ( Slider shift 、 Background color )
- Just one dom Element realization
- The height of the switch is fixed , But the width is not fixed , That is, the width is the width of the unknown parent element 100%, The width is always greater than the height
Topic interpretation
Because only one DOM
Elements , To distinguish between the different states of the slider and the background , So use pseudo elements ::before
stay .box
Add a slider inside , The sliding block adopts inline-block
Way to show , And by the transition
Property to specify the transition animation .
The background is also used transition
To specify the transition animation , And both pass :hover
Trigger .
Implementation code
HTML
<div class="father">
<div class="box"></div>
</div>
Copy code
CSS
.box {
height: 50px;
width: 100%;
background-color: #c2d3e4;
transition: background-color 0.25s
}
.box::before {
content: "b";
color: white;
display: inline-block;
background-color: white;
position: relative;
top: 5px;
left: 5%;
height: 40px;
width: 35%;
transition: left 0.25s;
}
.box:hover {
background-color: #348fe4;
}
.box:hover::before {
left: 60%;
}
/* The width of the parent element is arbitrary */
.father {
width: 100px;
}
Copy code
code snippet
copyright notice
author[Ziu],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/04/202204291627590589.html
The sidebar is recommended
- Software design pattern -- Chapter 3 structural pattern -- sharing element pattern
- Vue uses the online form of Excel in the front end of lucky sheet to import, display and export Excel files
- Vue uses echart to draw national maps and overlay charts
- Vue + element UI: Vue user-defined instruction monitors the scrolling event of El table to scroll the scroll bar to the bottom and load new data
- Vue + element when there is no paging at the back end, the front end completes the paging of El table independently - scrolling to the bottom to load new data
- [react] react routing concept
- Lenovo z475 disassembly and repair - plate No. kl6c
- Random array into an array, requiring that the elements cannot be repeated
- The belated Toyota bz4x, even with the blessing of e-tnga architecture, is still not worth starting
- In element plus, for example, how to change the checkbox status in the list by clicking on the header and selecting all
guess what you like
Crawler reverse advanced, using ast technology to restore JavaScript obfuscated code
Help, after changing the user name, the computer is useless and can't log in
Drag the left column of Vue and keep the right width unchanged; The scroll bar appears
HTML notes
In depth analysis of headless single linked list -- dynamic diagram demonstration of C language
Share 9 development skills related to vue3
CSS box centered
Used in Vue projects Sync modifier and $emit (update: XXX)
Vue class & Style binding and computed
Vue project uses this$ forceUpdate(); Force render page
Random recommended
- HTTP becomes HTTPS, self-made certificate
- Web front-end operation - tourism enterprise marketing publicity responsive website template (HTML + CSS + JavaScript)
- Self inspection list of a [qualified] front-end Engineer
- This principle in JavaScript and six common usage scenarios
- JavaScript this priority
- Analyzing the principle of HTTPS encryption
- Difference and principle between websocket and http
- Use of elementui scroll bar component El scrollbar
- Nginx security optimization
- GAC group has become the first pilot enterprise of "yueyouhang". Blessed are the car owners in Guangdong!
- Loki HTTP API usage
- JavaScript - prototype, prototype chain
- Front end experience
- JavaScript -- Inheritance
- HTTP cache
- Filters usage of table in elementui
- A JavaScript pit encountered by a non front-end siege lion
- Grain College - image error when writing Vue with vscode
- Utility gadget - get the IP address in the HTTP request
- Could not fetch URL https://pypi.org/simple/pytest-html/: There was a problem confirming the ssl cer
- Function of host parameter in http
- Use nginx proxy node red under centos7 and realize password access
- Centos7 nginx reverse proxy TCP port
- In eclipse, an error is reported when referencing layuijs and CSS
- Front end online teacher Pink
- Learn to use PHP to insert elements at the specified position and key of the array
- Learn how to use HTML and CSS styles to overlay two pictures on another picture to achieve the effect of scanning QR code by wechat
- Learn how to use CSS to vertically center the content in Div
- Learn how to use CSS to circle numbers
- Learn to open and display PDF files in HTML web pages
- The PHP array random sorting function shuffle() randomly scrambles the order of array elements
- JQuery implements the keyboard enter search function
- 16 ArcGIS API for JavaScript 4.18 a new development method based on ES modules @ ArcGIS / core
- 17 ArcGIS API for JavaScript 4.18 draw points, lines and faces with the mouse
- 18 ArcGIS API for JavaScript 4.18 obtain the length and area after drawing line segments and surface features
- Vue environment construction -- scaffold
- Build a demo with Vue scaffold
- Using vuex in Vue projects
- Use Vue router in Vue project
- 26 [react basics-5] react hook