Home > Article > Web Front-end > What you don’t know about CSS ::before and ::after pseudo-elements_html/css_WEB-ITnose
This article is equivalently inspired by an insight into programming life.
CSS has two pseudo-classes that are not commonly used: before and :after. They are occasionally used by people to add custom formats, but their functions are not limited to that. A few days ago, I discovered 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, these two pseudo-elements will be introduced next.
Creative Button Styles
Before understanding advanced applications, let’s first understand the grammar rules . When you usually only need to use these two pseudo-elements to add some custom characters, you only need to use the single colon writing method used by the pseudo-class to ensure browser compatibility:
Html Code
However, in CSS3, in order to distinguish pseudo elements and pseudo classes, they are pseudo The element uses double colons, so if attributes such as display or width are used to separate the display from the original element, it is recommended to double-write according to the standard. Browsers that are too old may have support issues, but most pseudo elements are used with CSS3, so there is no need to be backward compatible:
Html code
The unique attribute content under these two pseudo-classes is used to add logical headers to elements in CSS rendering. Add content at the beginning or end. Note that these additions will not change the document content, will not appear in the DOM, cannot be copied, and will only be added in the CSS rendering layer. The following values are useful:
Html code
Html code
Html code
Html code
Clearing floats is a problem that is often encountered. The solution for many people is to add an empty The div applies clear:both; attribute. Now, instead of adding meaningless elements, you only need the following style to automatically clear floats at the end of the element:
Html code
Many people like to add huge quotation marks as the background to the blockquote quotation paragraph. In this case, we can use :before to replace the background, which can leave space for the background and also You can use text directly instead of pictures:
Html code
In addition to simply adding characters, with the powerful positioning and special effects features of CSS, you can completely add additional effects to simple elements Attach the effect of up to two containers. One thing to note is that if the content is not required to only cooperate with the style attribute to create an effect, the content attribute cannot be empty, that is, content:"" . Otherwise, other style attributes will not take effect.
Hovering square brackets appear
Move the mouse over the link, and square brackets appear:
Html code
Html code
a {
position: relative;