Is it possible to set the equivalent of a src attribute of an img tag in CSS?

后端 未结 25 2189
借酒劲吻你
借酒劲吻你 2020-11-22 09:30

Is it possible to set the src attribute value in CSS? At present, what I am doing is:


25条回答
  •  忘了有多久
    2020-11-22 10:06

    I found a better way than the proposed solutions, but it does use the background-image indeed. Compliant method (cannot confirm for IE6) Credits: http://www.kryogenix.org/code/browser/lir/

    
    

    The CSS:

    img[src*="pathTo/myImage.jpg"] {
    
        background-image: url("mynewimg.jpg"); /* lets say 20x20 */
        width: 20px;
    
        display:inline-block;
        padding: 20px 0 0 0;
        height: 0px !important;
    
        /* for IE 5.5's bad box model */
        height /**/:20px;
    }
    

    The old image is not seen and the new is seen as expected.


    The following neat solution only works for webkit

    img[src*="pathTo/myImage.jpg"] {
    
        /* note :) */
        content:'';
        display:inline-block;
    
        width: 20px;
        height: 20px;
        background-image: url("mynewimg.jpg"); /* lets say 20x20 */
    
    }
    

提交回复
热议问题