CSS3’s Content Property

CSS3’s content property allows for content to be added to the page from within the CSS. This is useful in styling, but it’s especially useful if for some reason you don’t have access to or don’t want to edit your HTML files. The content property is used in conjunction with the :before and :after pseudo-selectors to make content appear before or after certain HTML elements.

Let’s say you wanted a question mark to appear after every h1 element. Below is the h1 tag before the content property is applied to it, followed by the CSS for adding a question mark after the h1 element using the content property.

Join us in our newest publication:

Screen Shot 2016-07-19 at 3.38.15 PM

  1. h1:after{
  3. content: "?";
  5. }

The result of the above CSS should be this:

Screen Shot 2016-07-19 at 3.38.26 PM

You can add style rules to your pseudo selected element just like you would to any other element. You can change the color, font-size, and even the placement (HOWEVER – you can only change the placement/add margins or padding by defining the position of the pseudo-selected element as absolute. If you don’t take this step then any margins or padding you add will be ineffective).

  1. h1:after{
  2. content: "?";
  3. color: #ff0074;
  4. font-size: 50px;
  5. }

Screen Shot 2016-07-19 at 3.44.00 PM

You can even use the content property to insert an image into your HTML using a url() syntax, like this:

  1. h1:after{
  2. content: url(insert/path/to/image);
  3. }


Share and Enjoy !

0 0