Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do ::before and ::after mean?

Tags:

html

css

I was looking at a couple Twitter Bootstrap templates and I saw that a lot of ::before and ::after were inserted before and after div tags.

Can anybody tell me what they are?

like image 687
Thegree0ne Avatar asked Mar 18 '14 15:03

Thegree0ne


People also ask

What is :: After mean?

: following in time or place : afterward, behind, later we arrived shortly after returned 20 years after. after.

What does the :: before do?

::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

Whats a before-and-after?

Before-and-after definition Describing a pair of images showing the difference made by a specified treatment.

What do u mean by before?

Definition of before preposition. previous to; earlier or sooner than: Phone me before noon. in front of; ahead of; in advance of: his shadow advancing before him;She stood before the window. ahead of; in the future of; awaiting: The golden age is before us.


2 Answers

The ::before and ::after pseudo elements are for css and are in no way bootstrap specific.

A quick example of some of the stuff it can do is this:

Say you have a basic p element:

<p>Hello</p>

In your css, if you had this:

p::before{
  content:'Hi';
}

The p tag in html would now say:

HiHello

If it was

p::after{
  content:'Hi';
}

It would be:

HelloHi

This can be very useful if you're using it with things such as phone numbers, or e-mail addresses e.g

p.phone_number::before{
  content:'Phone #: ';
}

<p class='phone_number'> would now have Phone #: before it.

You can do very many things, even use it for styling.

If you look at The shapes of CSS, you will see that it's used on the more complex shapes.

One thing that ::before and ::after have in common and MUST have to work, is the content attribute. If it doesn't have a content attribute it wont show up. Don't mistake this as having a blank content, though, as this will work provided you give it a height/width like any other element.

::before and ::after aren't the only Pseudo elements though, here is a list:

::after
::before
::first-letter
::first-line
::selection

You can also double up on these elements:

For example:

p:hover::before{
  content:'Hovered! ';
}
like image 143
Albzi Avatar answered Sep 29 '22 03:09

Albzi


They represent pseudo-elements, which you don't include directly in your markup, but are available to you to create some neat effects with CSS. You have mentioned ::before and ::after, and they represent pseudo-elements that appear, shockingly, before and after your elements.

The whole list is included below and should be fairly self-explanatory:

::after 
::before 
::first-letter 
::first-line 
::selection

ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements

Note the use of the double-colon, which is consistent with the spec. Sometimes you will see pseudo-elements specified with a single colon, but that was only because we needed to support browsers that didn't understand the double-colon syntax.

like image 5
Mister Epic Avatar answered Sep 29 '22 03:09

Mister Epic