How did CSS `content` property work for `img` element in WebKit?

前端 未结 1 447
臣服心动
臣服心动 2021-01-01 15:28

Long time ago there was a draft of CSS3 Generated Content spec which allowed the content property for any HTML element (not only ::before/::after pseudo-element

相关标签:
1条回答
  • 2021-01-01 15:29

    I just recently noticed this. I was really frustrated with it. Glad to see somebody already mentioned it.

    You guys seem much more knowledgeable on the topic than me. So, probably you already know these methods, but I will write my solution anyway, for juniors who will find this topic like myself.

    I needed a method for broken image placeholders. I thought I could check files and give ".brokenimg" class names to tag in Back-end and change the image with CSS "content:". But It didn't work. (Tested in Chrome, Firefox, Opera, Edge, Samsung's Android Web Browser. All same.)

    Then, I tried to change my script and use s instead of s. And tried this in CSS;

    .brokenimg::before { content: url(picture.jpg); }
    

    It almost worked but, But this is really limited and It wasn't what I needed.

    So, solved my problem with JavaScript event;

    <img onerror="this.src='broken.jpg'" src="image.jpg">
    

    I know it is not the same thing. But in my case, it did exactly what I needed.

    I guess we have to find ways like these for now.

    0 讨论(0)
提交回复
热议问题