Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using :before and :after pseudo-elements with images

I'm wondering how you format the Content CSS in a ::after pseudo-element?

I'm trying to have an h1 heading that has small images to the left and right of it.

My code so far:

HTML:

<h1>This Week's Photo Features</h1>

CSS:

h1 {
  text-align: center;
}

h1:before {
  content: ("images/NikonD100_40x30.jpg");
}
like image 438
SkyjumperTalon Avatar asked Aug 05 '17 17:08

SkyjumperTalon


People also ask

How do you use before and after pseudo-elements?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

What can you do with the before pseudo element?

The ::before pseudo-element can be used to insert some content before the content of an element.

What is :: before and :: after?

Definition and Usage The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content. Version: CSS2.


1 Answers

the content property can accept a url using this format:

content: url("the url")

Demo:

h1 {
  text-align: center;
}

h1:before {
  content: url("https://picsum.photos/40/40");
}

h1:after {
  content: url("https://picsum.photos/40/40");
}
<h1>This Week's Photo Features</h1>

Another option is to set the images as background of the pseudo elements:

Demo:

h1 {
  text-align: center;
}

h1:before, h1:after {
  display: inline-block;
  width: 40px;
  height: 40px;
  content: '';
}

h1:before {
  background: url("https://picsum.photos/40/40");
}

h1:after {
  background: url("https://picsum.photos/40/40");
}
<h1>This Week's Photo Features</h1>

And if you use the images as backgrounds, you can ditch the pseudo elements by using multiple backgrounds on the H1 element.

Demo:

h1 {
  height: 40px;
  padding: 0 40px;
  text-align: center;
  background: url("https://picsum.photos/40/40") left no-repeat,
              url("https://picsum.photos/40/40") right no-repeat;
}
<h1>This Week's Photo Features</h1>
like image 105
Ori Drori Avatar answered Sep 29 '22 01:09

Ori Drori