Table of Contents
About syntax and browser support
What it does
This the main content.
Pseudo-element styles
content : open -quote;
Associated background image
content : " " ;
MORE INSPIRATION
Captivating Shadows
Conclusion
Home Web Front-end HTML Tutorial Learning about pseudo-elements :Before and :After_html/css_WEB-ITnose

Learning about pseudo-elements :Before and :After_html/css_WEB-ITnose

Jun 24, 2016 pm 12:07 PM

The main purpose of Cascading Style Sheets (CSS) is to add styles to HTML elements. However, in some cases adding additional elements to the document is redundant or impossible. In fact, there is a feature in CSS that allows us to add additional elements without disturbing the document itself, which are "pseudo-elements".

You must have heard of this term, especially if you have been following our tutorials. Click here to browse other articles by the original author

In fact, there are indeed some members of the CSS family (CSS selectors) that are classified as pseudo-elements such as: :first-line, :first-letter, ::selection, :before and :after. However, for the purposes of this article, we will limit our discussion to the :before and :after elements. Therefore, "pseudo-elements" in this article will refer specifically to these two pseudo-elements (:before and :after), and we'll start with the basics to examine this unique topic.

About syntax and browser support

Pseudo elements actually exist in CSS1, but the :before and :after we are discussing now were released in CSS2.1. Initially, the syntax for pseudo-elements was ":" (one colon). With the development of the web, the revised pseudo-element in CSS3 used "::" (two colons), that is::before and :: after? to distinguish pseudo-elements and pseudo-classes (such as :hover, :active, etc.)

However, whether you use a single colon or a double colon, the browser will recognize them. Since IE8 only supports the single-colon format, for safety reasons, if you want wider browser compatibility, then use the single-colon format!

What it does

In short, pseudo-elements will insert additional elements before and after the content element, so when we add them, use the following markup, they Technically equal.

:before

  This the main content.

:after

:before

This the main content.

:after /tr>

But these elements are not actually generated in the document. They will be visible externally, but you won't find them in the document's source code, so in effect they are "fake" elements.

Using pseudo-elements

Using pseudo-elements is relatively easy, :before will "add" an element before the content and :after will "add" an element after the content. To add content among them we can use the content attribute.

For example, the following code snippet will add a quote before and after the quote.

blockquote:before {

  content : open-quote ;

blockquote:after {

  content : close-quote ;

blockquote:before { content : open-quote ; blockquote:after { content : close-quote ;

Pseudo-element styles

Despite being "fake" elements, pseudo-elements actually behave like "real" elements and we can add any style to them, such as Change their color, add background color, adjust font size, adjust the text within them, and more.

blockquote:before {

  content : open-quote ;

  font-size : 24pt ;

  text-align : center ;

  line-height : 42px ;

  color : #fff ;

  background : #ddd ;

  float : left ;

  position : relative ;

  top : 30px ;

blockquote:after {

  content : close-quote ;

  font-size : 24pt ;

  text-align : center ;

  line-height : 42px ;

  color : #fff ;

  background : #ddd ;

  float : right ;

  position : relative ;

  bottom : 40px ;

blockquote:before {

content : open -quote;

font-size: 24pt;

text-align: center; line-height: 42px;

color: #fff;

background : #ddd ;

blockquote:before {

  content : open-quote ;

  font-size : 24pt ;

  text-align : center ;

  line-height : 42px ;

  color : #fff ;

  background : #ddd ;

  float : left ;

  position : relative ;

  top : 30px ;

  border-radius: 25px ;

  height : 25px ;

  width : 25px ;

blockquote:after {

  content : close-quote ;

  font-size : 24pt ;

  text-align : center ;

  line-height : 42px ;

  color : #fff ;

  background : #ddd ;

  float : right ;

  position : relative ;

  bottom : 40px ;

  border-radius: 25px ;

  height : 25px ;

  width : 25px ;

float : left ; position : relative ; top : 30px ; blockquote:after { content : close-quote ; font-size : 24pt ; text-align : center ; line-height : 42px ; color : #fff ; background : #ddd ; float : right ; position : relative ; bottom : 40px ;
Specify pseudo-element size The default generated element is an inline element, so when If we want to specify their height and width, we first have to declare them as block-level elements using display: block.  Since float has been set, there is no need to set display:black.
blockquote:before { content: open-quote; font-size: 24pt; text-align: center; line-height: 42px; color: #fff ; background : #ddd ; float : left ; position : relative ; top : 30px ; border-radius: 25px; height: 25px; width: 25px; > font-size: 24pt; text-align: center; line-height: 42px; color: #fff; background : #ddd ; float : right ; position : relative ; bottom : 40px ; border-radius: 25px ; height : 25px ; width : 25px ;

Associated background image

We can also replace the content with an image instead of just plain text. Although the content attribute provides url() to insert images, in more cases, I prefer to use the background attribute to have better control over the image.

blockquote:before {

  content : " " ;

  font-size : 24pt ;

  text-align : center ;

  line-height : 42px ;

  color : #fff ;

  float : left ;

  position : relative ;

  top : 30px ;

  border-radius: 25px ;

  background : url (images/quotationmark.png) -3px -3px #ddd ;

  display : block ;

  height : 25px ;

  width : 25px ;

blockquote:after {

  content : " " ;

  font-size : 24pt ;

  text-align : center ;

  line-height : 42px ;

  color : #fff ;

  float : right ;

  position : relative ;

  bottom : 40px ;

  border-radius: 25px ;

  background : url (images/quotationmark.png) -1px -32px #ddd ;

  display : block ;

  height : 25px ;

  width : 25px ;

blockquote :before {

content : " " ;

font-size : 24pt;

text-align : center ;

line-height : 42px;

blockquote:hover:after, blockquote:hover:before {

  background-color : #555 ;

color : #fff ; float : left ;

position : relative ;

top : 30px ;

border-radius : 25px;

transition: all 350ms;

-o-transition: all 350ms;

-moz-transition: all 350ms;

-webkit-transition: all 350ms;

background: url (images/quotationmark.png) -3px -3px #ddd; display: block; height: 25px; width : 25px ; blockquote:after { content : " " ; font-size : 24pt ; text-align : center ; line-height : 42px ; color : #fff ; float : right ; position : relative ; bottom : 40px ; border-radius: 25px ; background : url (images/quotationmark.png) -1px -32px #ddd ; display : block ; height : 25px ; width : 25px ;
However, as you As you can see from the code snippet above, we have still declared the content attribute and used an empty string at this time. The content attribute is required and should be used frequently. Otherwise, pseudo-elements won't work properly anyway. Combined with Pseudo-Classes Although there are different types of pseudo- Hopefully, when our mouse moves over blockqoute, the background color of the quotes will become slightly darker.
blockquote:hover:after, blockquote:hover:before { background-color : #555 ;
Add transition effect We even You can apply the transition attribute on pseudo-elements to create beautiful color transitions.
transition: all 350ms; -o-transition: all 350ms; -moz-transition: all 350ms; -webkit-transition: all 350ms;

MORE INSPIRATION

To inspire you, we have chosen three cool examples that will give you lots of ideas on web design.

Captivating Shadows

In this tutorial Paul Underwood explains how to create more realistic and attractive shadow effects.

Use the pseudo-elements :before and :after . Both of them are absolutely positioned and use negative z-index to place them behind the image to achieve a shadow effect.

3D button

This is a very clever implementation, using pseudo elements combined with CSS3 box-shadow to draw an amazing 3D button, using only CSS and single anchor text. The pseudo element: before is used to add the number "1" to the left side of the button.

Overlay Image Effect

It is also possible to use pseudo-elements to create a "messy" overlay image effect using just an image tag. Pseudo elements are used to create the illusion of overlapping images by using a negative z-index value to make the "overlay" image behind the real image.

Conclusion

Pseudo elements are cool and practical. Basically, every element we add does not interfere with the existing ones. HTML structure, and pseudo-elements can do almost everything we can think of.

In fact, there are some improvements to pseudo-elements, which are currently being carried out gradually, such as nested pseudo-elements div::before::before { content: ”; } and multiple pseudo-elements div::before(3) { content: "; }. Obviously, in future web design, these improvements will give our designs more forms (more possibilities). However, they will be supported in the latest browsers, so let's wait patiently for now!

Original source: Thoriq Firdaus

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

See all articles