Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does *:before and *:after do in css

Tags:

html

css

I researched on SO about *(Asterisk) and I have found that it selects all elements and applies styles to them.

I followed this link, Use of Asterisk and I noticed that this code will apply border to all of the elements.

* {
 border: 1px solid red;
}

Now, my concern is that what does *:before and *:after do in CSS?

*:before,
*:after {
          box-sizing: border-box;
}
like image 889
Amit singh Avatar asked Sep 27 '15 07:09

Amit singh


People also ask

What does :: before and :: after do?

Save this answer. Show activity on this post. :before selector inserts something before the content of each selected element(s). :after selector inserts something after the content of each selected element(s).

What is pseudo before and after in CSS?

A CSS pseudo-element is used to style specified parts of an element. For example, it can be used to: Style the first letter, or line, of an element. Insert content before, or after, the content of an element.

What does :: before means in CSS?

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.


2 Answers

Like their name said it, :before & :after are used to apply css properties JUST before/after the content WITHIN the matching element.

One day, a wise man said 'One fiddle is worth thousands words', so :

div {
  border: solid 1px black;
  padding: 5px;
  }

div:before {
  content: "Added BEFORE anything within the div!";
  color:red;
 }

div:after {
  content: "Added AFTER anything within the div!";
  color:green;
 }
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
like image 103
Sir McPotato Avatar answered Sep 28 '22 17:09

Sir McPotato


:before selector inserts something before the content of each selected element(s). :after selector inserts something after the content of each selected element(s).

so *:before like Dan White said would be before all elements and *:after will be after all elements

like image 42
Nalani Avatar answered Sep 28 '22 18:09

Nalani