Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ::after with an image tag? [duplicate]

Tags:

html

css

Is it possible to append text to images using the ::after pseudo-selector?

img[src="images/interface-zoom-buttons.png"]::after {
    content: "these are the zoom buttons";
}

The desire is to use the selector to provide a caption (other styling will be applied to the content of the selector) for images that get used over and over again in multiple HTML documents.

In the above example the image appears correctly and you can see that the appropriate CSS has been applied, but the content (supplied by the selector) is not seen in the output.

like image 900
Jay Blanchard Avatar asked Oct 14 '14 22:10

Jay Blanchard


People also ask

Can we use after in IMG tag?

The same applies to the :after pseudo-element.

What does :: After mean in HTML?

::after. In CSS, ::after creates a pseudo-element that is the last 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.

How do you duplicate an image in HTML?

CTRL C + CTRL V.

What is :: After in Javascript?

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


1 Answers

An img tag can not have child elements. Because img is a void element and its content model never allows it to have contents under any circumstances.

::after creates a pseudo element as the last child of its parent.

So img::after will not work ultimately because the browser will not allow it based on definition.

What you can do is to contain the img in a container and have ::after on the container.

Demo http://jsfiddle.net/x2za91jw/

HTML

<div class="container">
    <img src="http://www.gazette-ariegeoise.fr/IMG/jpg/test.jpg" />
</div>

CSS

.container {
    position: relative;
}
.container::after {
    position: absolute;
    top: 0;
    left: 0;
    border: 1px solid #000;
    content:"these are the zoom buttons";
}
like image 74
Arbel Avatar answered Oct 27 '22 08:10

Arbel