Things You Don’t Know About the CSS ::before and ::after Pseudo-Elements

Things You Don’t Know About the CSS ::before and ::after Pseudo-Elements

CSS has two pseudo-classes that are not commonly used: before and :after. They are occasionally used to add some custom formatting, but their functions are not limited to this. A few days ago, I found Creative Link Effects, a very interesting page that introduces creative link effects. The amazing effects in it use a lot of features, in addition to the transform attribute for deformation, the two pseudo-elements that I will introduce next.

Creative Button Styles

Creative Button Styles

A basic grammar

Before understanding advanced applications, let's first understand the grammar rules. Normally, when you only need to use these two pseudo-elements to add some custom characters, just use the single colon notation used by pseudo-classes to ensure browser compatibility:

CSS CodeCopy content to clipboard
  1. p:before {}

However, in CSS3, double colons are used to distinguish pseudo-elements from pseudo-classes. Therefore, if attributes such as display or width are used so that the display is separated from the original element, it is recommended to write them in double colons according to the standard. Old browsers may have support issues, but pseudo-elements are mostly used with CSS3, so backward compatibility is not a big deal:

CSS CodeCopy content to clipboard
  1. img::after {}

The content attribute unique to these two pseudo-classes is used to add content to the logical head or tail of the element in CSS rendering. Note that these additions do not change the document content, do not appear in the DOM, are not replicable, and are only added to the CSS rendering layer. The following values ​​are more useful:

•[String] - Enclosing a string in quotes will add the string to the element content. Example:

CSS CodeCopy content to clipboard
  1. •a:after { content : "↗" ; }
attr() – Calls the attributes of the current element, which can be convenient for displaying, for example, the Alt text of an image or the Href address of a link. Example:
CSS CodeCopy content to clipboard
  1. •a:after { content : "("   attr (href) ")" ; }
url() / uri() – used to reference media files. Example:
CSS CodeCopy content to clipboard
  1. •h 1: :before { content : url (logo.png); }
counter() – Calls the counter to implement serial number functionality without using list elements. For details, see the usage of the counter-increment and counter-reset properties. Example:
CSS CodeCopy content to clipboard
  1. h 2: before { counter counter-increment : chapter; content : "Chapter "   counter (chapter) ". " }

Second advanced skills

Clearing floats is a problem that is often encountered. Many people's solution is to add an empty div and apply the clear:both; attribute. Now, instead of adding meaningless elements, all you need is the following styles to automatically clear the float at the end of the element:

CSS CodeCopy content to clipboard
  1. . clear -fix { * overflow : hidden ; *zoom: 1; }
  2. . clear -fix:after { display : table; content : "" ; width : 0; clear : both ; }

Many people like to add huge quotation marks as the background of the blockquote. In this case, we can use :before instead of background, which can leave space for the background and use text instead of images:

CSS CodeCopy content to clipboard
  1. blockquote::before {
  2.      content : open-quote ;
  3.      position : absolute ;
  4.      z-index : -1;
  5.      color : #DDD ;
  6.      font-size : 120px ;
  7.      font-family : serif ;
  8.      font-weight : bolder ;
  9. }

Three special effects

In addition to simply adding characters, with the powerful positioning and special effects features of CSS, it is entirely possible to add up to two containers to simple elements. One thing to note is that if you don’t need the content and only want to use the style attribute to produce an effect, the content attribute cannot be empty, that is, content:””. Otherwise, other style attributes will not take effect.

懸浮出現方括號

Square brackets appear when hovering

Move the mouse over the link and square brackets will appear:

CSS CodeCopy content to clipboard
  1. a {
  2.      position : relative ;
  3.      display : inline - block ;
  4.      outline : none ;
  5.      text-decoration : none ;
  6.      color : #000 ;
  7.      font-size : 32px ;
  8.      padding : 5px   10px ;
  9. }
  10.      
  11. a:hover::before, a:hover::after { position : absolute ; }
  12. a:hover::before { content : "\5B" ; left : - 20px ; }
  13. a:hover::after { content : "\5D" ; right right : - 20px ; }

Similarly, we only need to use display: block and position: absolute to treat it as two containers and combine them to create a double-border effect when suspended:

CSS CodeCopy content to clipboard
  1. a {
  2.      position : relative ;
  3.      display : inline - block ;
  4.      outline : none ;
  5.      text-decoration : none ;
  6.      color : #000 ;
  7.      font-size : 32px ;
  8.      padding : 0 10px ;
  9. }
  10.      
  11. /* Large frame */      
  12. a:hover::before, a:hover::after {
  13.      content : "" ;
  14.      display : block ;
  15.      position : absolute ;
  16.      top : -15%%;
  17.      left : -14%%;
  18.      width : 120%;
  19.      height : 120%;
  20.      border-style : solid ;
  21.      border-width : 4px ;
  22.      border-color : #DDD ;
  23. }
  24.      
  25. /* Small frame */      
  26. a:hover::after {
  27.      top : 0%;
  28.      left : 0%;
  29.      width : 100%;
  30.      height : 100%;
  31.      border-width : 2px ;
  32. }

The above article about the unknown usage of CSS ::before and ::after pseudo-elements is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

Original URL: http://www.cnblogs.com/androidshouce/archive/2016/06/12/5576493.html

<<:  Sample code of uniapp vue and nvue carousel components

>>:  Implementation of adding a mask layer effect when the CSS mouse hovers over the image

Recommend

How to deploy SpringBoot project using Docker

The development of Docker technology provides a m...

The difference between GB2312, GBK and UTF-8 in web page encoding

First of all, we need to understand that GB2312, ...

A brief discussion on JavaScript throttling and anti-shake

Table of contents Throttling and anti-shake conce...

Example code for implementing a QR code scanning box with CSS

We usually have a scanning box when we open the c...

Solution for multiple Docker containers not having the same port number

Background In Docker, four containers are created...

WeChat applet implements sorting function based on date and time

I recently took over a small program project, and...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...

How to use CSS media query aspect-ratio less

CSS media query has a very convenient aspect rati...

How to use jsonp in vue

Table of contents 1. Introduction 2. Installation...

js to implement collision detection

This article example shares the specific code of ...

Detailed application of Vue dynamic form

Overview There are many form requirements in the ...

Summary of MySQL 8.0 Online DDL Quick Column Addition

Table of contents Problem Description Historical ...

VSCode+CMake+Clang+GCC environment construction tutorial under win10

I plan to use C/C++ to implement basic data struc...