Does :before not work on img elements?

后端 未结 12 2236
眼角桃花
眼角桃花 2020-11-22 01:46

I\'m trying to use the :before selector to place an image over another image, but I\'m finding that it simply doesn\'t work to place an image before an im

相关标签:
12条回答
  • 2020-11-22 02:14

    The pseudo-elements generated by ::before and ::after are contained by the element's formatting box, and thus don't apply to replaced elements such as img, or to br elements.

    https://developer.mozilla.org/en-US/docs/Web/CSS/::after

    0 讨论(0)
  • 2020-11-22 02:14

    I tried and found a simpler method to do so. Here is the HTML:

        <img id="message_icon" src="messages2.png">
        <p id="empty_para"></p>
    

    What I did was place an empty <p> tag after my image tag. Now I will use p::before to show the image and position it according to my needs. Here is the CSS:

    #empty_para
    {
        display:inline;
        font-size:40;
        background:orange;
        border:2px solid red;
        position:relative;
        top:-400px;
        left:100px;
    }
    #empty_para::before
    {
        content: url('messages.png');
    }
    

    Try it.

    0 讨论(0)
  • 2020-11-22 02:15

    I found a way to make this work in pure css:

    The I'm just fake content-method

    a pure CSS method to enable img:after.

    You can check out the CodePen: I'm just fake content or see the source.

    Source & Snippet

    img {
        /* hide the default image */
        height:0;
        width:0;
    
        /* hide fake content */
        font-size:0;
        color:transparent;
    
        /* enable absolute position for pseudo elements */
        position:relative;
    
        /* and this is just fake content */
        content:"I'm just fake content";
    }
    
    /* initial absolute position */
    img:before,
    img:after {
        position:absolute;
        top:0;
        left:0;    
    }
    
    /* img:before - chrome & others */
    img:before {
        content:url(http://placekitten.com/g/250/250);
    }
    
    /* img:before - firefox */
    body:not(:-moz-handler-blocked) img:before {
        padding:125px;
        background:url(http://placekitten.com/g/250/250) no-repeat;
    }
    
    /* img:after */
    img:after {
        /* width of img:before */
        left:250px;
    
        content:url(http://lorempixel.com/350/200/city/1);
    }
    <img
        alt="You are watching the ~ I'm just fake content ~ method"  
    />

    Browser support

    ✓ Chrome 10+

    ✓ Firefox 11+

    ✓ Opera 9.8+

    ✓ Safari

    No support

    ⊗ Internet Explorer 8 / 9

    Please test in other browsers

    0 讨论(0)
  • 2020-11-22 02:19

    The before and after pseudo-selectors don't insert HTML elements — they insert text before or after the existing content of the targeted element. Because image elements don't contain text or have descendants, neither img:before or img:after will do you any good. This is also the case for elements like <br> and <hr> for the same reason.

    0 讨论(0)
  • 2020-11-22 02:20

    This one works for me:

    html

    <ul>
        <li> name here </li>
    </ul>
    

    CSS

    ul li::before {
        content: url(../images/check.png);
    }
    
    0 讨论(0)
  • 2020-11-22 02:22

    Unfortunately, most browsers do not support using :after or :before on img tags.

    http://lildude.co.uk/after-css-property-for-img-tag

    However, it IS possible for you to accomplish what you need with JavaScript/jQuery. Check out this fiddle:

    http://jsfiddle.net/xixonia/ahnGT/

    $(function() {
    
        $('.target').after('<img src="..." />');
    
    });
    

    Edit:

    For the reason why this isn't supported, check out coreyward's answer.

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