


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
Basic Grammar
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
- p:before {}
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
- img::after {}
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
- a:after { content: "?"; }
Html code
- a:after { content:"(" attr(href) ")"; }
Html code
- h1::before { content: url(logo.png); }
Html code
- h2:before { counter-increment: chapter; content: "Chapter " counter(chapter) ". " }
Second Advanced Tips
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
- .clear -fix { *overflow: hidden; *zoom: 1; }
- .clear-fix:after { display: table; content: ""; width: 0; clear: both; }
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
- blockquote::before {
- content: open-quote;
- position: absolute;
- z-index: -1;
- color: #DDD;
- font-size: 120px;
- font-family: serif;
- font-weight: bolder;
- }
Three special effects
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
- a {
- position: relative;
- display: inline-block;
- outline: none;
- text-decoration : none;
- color: #000;
- font-size: 32px;
- padding: 5px 10px;
- a:hover::before, a:hover::after { position: absolute; }
- a:hover::before { content: "5B"; left: -20px; }
- a:hover::after { content: "5D"; right: -20px; }
Html code
a {
position: relative;- display: inline-block;
- outline: none;
- text-decoration: none;
- color: #000;
- font-size: 32px;
- padding: 0 10px;
- }
- /* Large frame */
- a:hover::before, a:hover::after {
- content: "";
- display: block;
- position: absolute;
- top: -15%%;
- left: -14%%;
- width: 120%;
- height: 120%;
- border-style: solid;
- border-width: 4px;
- border-color: #DDD; 🎜>
- /* small box */
- a:hover::after {
- top: 0%;
- left: 0 %;
- width: 100%;
- height: 100%;
- border-width: 2px; 🎜>
- This article is equivalently inspired by an insight into programming life. Using the :before and :after pseudo-classes to combine more powerful features of CSS3, you can also complete many interesting special effects and Hacks. This is just a starting point. What magical uses can you come up with?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
