Writing in the front
Pseudo-classes are often confused with pseudo-elements, which have effects similar to those achieved by adding an actual element, and pseudo-classes, which have effects similar to those achieved by adding an actual class. In fact, CSS3 explicitly specifies that pseudo-classes are represented by a colon, and pseudo-elements by two colons, in order to distinguish the two. The essential difference between a pseudo-class and a pseudo-element is whether an abstraction creates a new element. This paper will not go into the specific knowledge of pseudo classes and pseudo elements. Here we will introduce the skills and application of 10 CSS3 pseudo classes from Bronze to King. Go straight to the case code
Bronze-1, pseudo-classes implement box shadows
After Animate/ Transition box-shadow you can Animate/transition box-shadow with box-shadow properties, but repaint requires a lot of work
Implementation principle:
By changing the transparency, so from a non-default value updates its value, there is no need to take any redraw (see: https://csstriggers.com/opacity)
(PS: Seemingly inexplicably unlocked an optimization about front-end CSS, smirk)
Here, an empty pseudo-element is set to zero shadow opacity to hide, and its opacity is restored by hovering over it. Here is a code comparison between the traditional and pseudo-class implementations
<div class="before"</p> </div> <hr /> <div class= after </h1> <p>Animate/transition box-shadow You can use the box-shadow attribute to implement box shadows"after"> <h1>After</h1> <p> Achieve the same effect by modifying the transparency of pseudo-elements without redrawing the cost </p> </div>Copy the code
.before {
padding: 1em;
background-color: #fff;- its - the transition: 0.2 s; The transition: 0.2 s; }. Before :hover {box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3); } .after { position: relative; padding: 1em; background-color:#fff;
}
.after:before {
content: ""; position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: -1; Box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3); opacity: 0; will-change: opacity; - its - the transition: 0.2 s; The transition: 0.2 s; } .after:hover:before { opacity: 1; }Copy the code
Effect :(see link below for full code)
Bronze-2, Pseudo-element: Before implemented breadcrumb navigation bar
<ul class="breadcrumb">
<li><a href="#">Home</a>
</li>
<li><a href="#">Pictures</a>
</li>
<li><a href="#">Summer 15</a>
</li>
<li>Italy</li>
</ul>
Copy the code
ul.breadcrumb {
padding: 8px 16px;
list-style: none;
background-color: #eee;
}
ul.breadcrumb li {
display: inline;
}
ul.breadcrumb li+li:before {
padding: 8px;
color: black;
content: "/\00a0";
}
ul.breadcrumb li a {
color: green;
}
Copy the code
Effect:
Bronze-3, pseudo-element to hover button filling and border floating animation
Effect :(see link below for full code)
Bronze-4, pseudo-class after implementation of the triangular arrow
Implementation principle: The border in the direction the arrow points to is not set. The border on either side of the arrow is transparent. The opposite side is the main border color (larger)/main background color (smaller), because we want the triangle arrow with the border color. After the first arrow (larger) is accurately covered by the second arrow (smaller), the remaining uncovered edge is the border of the composite triangle arrow, whose color is the color of the larger triangle arrow, adjustable. The color of the smaller triangle arrow should be set to the body color, and the negative offset should be covered by the body border, so that it is integrated with the body
<div class='container'>
<img alt=' ' src='http://placehold.it/400x200'>
<div class='arrow-left'></div>
</div>
<div class='container new'>
<div class='arrow-right'></div>
<img alt=' ' src='http://placehold.it/400x200'>
</div>
Copy the code
.arrow-left:before {
z-index: 9999;
content: "";
display: block;
width: 0;
height: 0;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-right: 20px solid #E9E9E9;
position: absolute;
left: -20px;
top: 80px;
}
Copy the code
Effect :(see link below for full code)
Bronze-5, pseudo-class after implemented picture arrow
Effect :(see link below for full code)
Bronze-6, pseudo-element implementation with angled bottom boundary (sloping boundary)
Principle: Modify the Webkit-transform: skewY property to change the skew (same with rotation)
.edge--bottom {
position: relative;
z-index: 1;
}
.edge--bottom:after {
background: inherit;
content: ' '; display: block; height: 50%; left: 0; position: absolute; right: 0; z-index: -1; } .edge--bottom:after { bottom: 0; - its - transform: skewY (1.5 deg); - ms - transform: skewY (1.5 deg); The transform: skewY (1.5 deg); -webkit-transform-origin: 100%; -ms-transform-origin: 100%; transform-origin: 100%; }Copy the code
Effect :(see link at the end of this article for full code)
King -1, Fake elements and translate translate implementation prompt box
<div class="row">
<a rel="nofollow" rel="noreferrer" href="#" class="btn tooltip top">
<span>TOOLTIP TOP</span>
<span class="tooltip-content">Lorem ipsum dolor sit amet</span>
</a>
</div>
Copy the code
.tooltip .tooltip-content::after {
background: #05a8ff;
content: ""; height: 10px; position: absolute; -webkit-transform: rotate(45deg); transform: rotate(45deg); width: 10px; }.tooltip.top. tooltip-content {bottom: calc(100% + 1.5em); left: 50%; -webkit-transform: translateX(-50%); transform: translateX(-50%); } .tooltip.top .tooltip-content::after { bottom: -5px; left: 50%; margin-left: -5px; }Copy the code
Effect :(see link at the end of this article for full code)
King -2, the use of CSS3 pseudo-element to achieve automatic typing animation
Typing Animation with pseudo-elements looks like Typing, but it’s actually a visual effect of overlaying pseudo-elements on a string and then gradually reducing the pseudo-element overlay width
<div>
<h1>Typing Animation</h1>
<p class="tagline">
<span class="tagline-skill"><span class="tagline-skill_inner">webdesign</span></span>
</p>
</div>
Copy the code
.tagline-skill_inner:after {
content: "";
position: absolute;
top: -1px;
right: 0;
bottom: -2px;
left: 0;
border-left: 1px solid #fff;
background-color: #2a2a28;
-webkit-animation: animatetoright 1s steps(10) infinite alternate;
animation: animatetoright 1s steps(10) infinite alternate;
}
Copy the code
Effect :(see link at the end of this article for full code)
King -3, CSS3 pseudo-element construction of the article watermark background
h1 { position: relative; margin: 0; font-weight: bold; Letter - spacing: 0.05 rem. line-height: 1; text-transform: uppercase; z-index: 10; } h1:before { content:"2018/08";
font-family: monospace;
font-size: 10rem;
position: absolute;
top: 2rem;
left: -2rem;
z-index: 0;
line-height: 1;
color: rgba(50, 25, 0, 0.1);
}
Copy the code
Effect :(see link at the end of this article for full code)
King -4, CSS3 with false elements do page number summary
a { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-flow: row nowrap; -ms-flex-flow: row nowrap; flex-flow: row nowrap; -webkit-box-align: baseline; -webkit-align-items: baseline; -ms-flex-align: baseline; align-items: baseline; text-decoration: none; -webkit-transition: color .2s ease-in-out; transition: color .2s ease-in-out; } a::before { height: .1em; -webkit-box-flex: 1; -webkit-flex: 1 1 auto; -ms-flex: 1 1 auto; flex: 1 1 auto; -webkit-box-ordinal-group: 2; -webkit-order: 1; -ms-flex-order: 1; order: 1; background: left bottom/contain repeat-x url(data:image/svg+xml; base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA3IDIiPjxjaXJjbGUgZmlsbD0iI2ZmZiIgY3g9IjMuN SIgY3k9IjEiIHI9IjEiLz48L3N2Zz4=); content:' ';
}
a::after {
-webkit-box-ordinal-group: 3;
-webkit-order: 2;
-ms-flex-order: 2;
order: 2;
content: "p." attr(data-page);
}
Copy the code
Effect :(see link at the end of this article for full code)
King -5, pseudo-class compatibility to understand
IE8 does not support many features in CSS3, such as the pseudo-nth-child element, which can be used with the + sign (representing adjacent elements) to achieve the same function
2. Ie9.js is a JS library to solve the compatibility problem of IE5.5 to IE8 CSS3 features
The last
CSS world is very beautiful, each knowledge point can worthy of in-depth study and practice, for pseudo-classes, pseudo elements also has a lot of earthy effects can be written out, such as masks, fuzzy image background images, more advanced special effects and so on mouseover events, 10 cases above is my personal work summary and reference on online resources arrangement, hope to be of help
Finally attached github sample source code, downloadable experience.
In the next part, I will write some interactive skills on vUE pages, which are also dry goods. This article portal “When most people understand VUE to be perfect, is it time to think about how to make VUE pages sexy?”