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

后端 未结 25 2187
借酒劲吻你
借酒劲吻你 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:04

    There is a solution that I found out today (works in IE6+, FF, Opera, Chrome):

    
    

    How it works:

    • The image is shrunk until no longer visible by the width & height.
    • Then, you need to 'reset' the image size with padding. This one gives a 16x16 image. Of course you can use padding-left / padding-top to make rectangular images.
    • Finally, the new image is put there using background.
    • If the new background image is too large or too small, I recommend using background-size for example: background-size:cover; which fits your image into the allotted space.

    It also works for submit-input-images, they stay clickable.

    See live demo: http://www.audenaerde.org/csstricks.html#imagereplacecss

    Enjoy!

提交回复
热议问题