Pseudo Elements and Image Elements: A CSS Conundrum
Despite the widespread applicability of pseudo elements like :before and :after in CSS, their functionality with image elements (<img src="/static/imghwm/default1.png" data-src="http://0.gravatar.com/avatar/this-is-not-a-hash" class="lazy" alt="Why Don't Pseudo-elements Work with `` Tags in CSS?" >) has remained a puzzling exception. As demonstrated in the code snippet below, adding pseudo elements to an img element does not produce the expected result:
<img src="/static/imghwm/default1.png" data-src="http://0.gravatar.com/avatar/this-is-not-a-hash" class="lazy" alt="Why Don't Pseudo-elements Work with `` Tags in CSS?" >
img:before { content: "hello"; }
While it works seamlessly with other elements like div and span, the desired effect is absent when applied to img elements.
Behind the Scene: The CSS Specification
The CSS specification sheds light on this discrepancy. It explicitly states that the interaction between pseudo elements and replaced elements, including img in HTML, is not fully defined in the current version. This implies that the current specification does not specify the behavior of pseudo elements with img elements.
Potential Solution: Awaiting Standardization
However, this situation may not persist indefinitely. The specification suggests that the interaction between pseudo elements and replaced elements will be further defined in a future version. This leaves us with the expectation that pseudo elements will eventually be supported for img elements in a more comprehensive and consistent manner.
Until then, developers looking to achieve similar effects using img elements may need to consider alternative approaches, such as using JavaScript or creating an additional wrapper element around the img.
The above is the detailed content of Why Don't Pseudo-elements Work with `` Tags in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
