InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    CSS ::before and ::after examples

    on Feb 5, 2016

    CSS pseudo element ::before and ::after (CSS 2 syntax :before and :after) can be used to place some content before or after an element. Most browsers support the CSS3 syntax (::before and ::after) now. Here are some examples.

    Basic ::before and ::after example

    <style type="text/css" media="screen">
    #id1 {background:lightgreen;}
    #id1::before {
      content:"before text";
    }
    #id1::after {
      content:"after text";
    }
    </style>
    <div id="id1">
    Hello world.
    <div>
    refresh done
    try it online

    ::before and ::after example using display block and background color

    <style type="text/css" media="screen">
    #id1 {background:lightgreen;}
    #id1::before {
      content:"before text";
      display:block;
      background-color:lightgray;
    }
    #id1::after {
      content:"after text";
      display:block;
      background-color:lightgray;
    }
    </style>
    <div id="id1">
    Hello world.
    <div>
    refresh done
    try it online

    Related

    • CSS ::first-line pseudo element
    • CSS ::first-letter pseudo element
    • CSS generated content for ::after and ::before

    Specification

    • W3C CSS 3 ::before and ::after specification

    Suggested posts:

    1. Running php eval on code with tags
    2. CSS placeholder style
    3. CSS clip
    4. CSS animation shorthand property
    5. CSS text-overflow – truncate text with three dots
    6. CSS selector specificity
    7. HTML5 label tag examples
    8. How to install and run Chrome PageSpeed insights for measuring site performance
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS Pseudo Elements, CSS selectors, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress