search
HomeWeb Front-endHTML TutorialWhat 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

  1. 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

  1. 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:

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

    Html code

    1. a:after { content: "?"; }
  • attr() ? Calling the attributes of the current element can conveniently display the Alt prompt text of the image or the Href address of the link. Example:

    Html code

    1. a:after { content:"(" attr(href) ")"; }
  • url() / uri() ? Used to reference media files. Example:

    Html code

    1. h1::before { content: url(logo.png); }
  • counter() ? By calling the counter, you can implement the serial number function without using list elements. For details, please see the usage of counter-increment and counter-reset attributes. Example:

    Html code

    1. 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

    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 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

    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, 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

    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. a:hover::before, a:hover::after { position: absolute; }
    10. a:hover::before { content: "5B"; left: -20px; }
    11. a:hover::after { content: "5D"; right: -20px; }
    Similarly, we only need to match the display : block and position: absolute, you can treat them as two containers and combine them into a special effect of floating double borders:

    Html code

    a {

    position: relative;
    1. display: inline-block;
    2. outline: none;
    3. text-decoration: none;
    4. color: #000;
    5. font-size: 32px;
    6. padding: 0 10px;
    7. }
    8. /* Large frame */
    9. a:hover::before, a:hover::after {
    10. content: "";
    11. display: block;
    12. position: absolute;
    13. top: -15%%;
    14. left: -14%%;
    15. width: 120%;
    16. height: 120%;
    17. border-style: solid;
    18. border-width: 4px;
    19. border-color: #DDD; 🎜>
    20. /* small box */
    21. a:hover::after {
    22. top: 0%;
    23. left: 0 %;
    24. width: 100%;
    25. height: 100%;
    26. border-width: 2px; 🎜>
    27. 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?

    Statement
    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
    Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

    HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

    What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

    The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

    HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

    How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

    Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

    What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

    HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

    How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

    TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

    HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

    HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

    How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

    ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

    See all articles

    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 Article

    Hot Tools

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.