Loading...

Top
PFQ Banner

This is PokéFarm Q, a free online Pokémon collectables game.

Already a user? New to PFQ?

Single post in Free CSS, Userscripts, etc

Forum Index > Other > Other Art > Free CSS, Userscripts, etc >

DrWho's AvatarDrWho
DrWho's Avatar

Replacing images with CSS

QUOTE originally posted by Spewpa

Just curious, is there a way to replace the images on the counters?
Spewpa asked for a way to replace counter images. That gave me the idea to make a post about replacing images with CSS. There's two type of images:

1. As a CSS property

The image is loaded as a CSS property, so you can just set a new property. For example, select all fields with the 'normal2.png' background and set a new custom background. .field[style*='normal2.png'] { background-image: url("https://placekitten.com/g/600/345") !important }

2. As the src of an img tag

The more interesting case is when the image is loaded as the HTML <img> element. There's no way to tell CSS "select all images and replace the value of src with something else". CSS cannot modify existing attributes. But there's one trick we can use, the object-position property. For an img tag, object-position describes where the graphic is positioned relative to the box of the img element. To make this clear, here's a cat loaded with the img tag. We give the img a red border and padding to highlight the box of the element.
img { box-sizing: border-box; border: 3px solid red; padding: 3px; } Now, we use object-position to move the position of the cat pixels relative to the box. img { box-sizing: border-box; border: 3px solid red; padding: 3px; object-position: 25px 10px; } So, the trick is to move the old sprite way off the visible screen with object-position. Then we can set a background-image to fill the now empty img element. If your new background-image has a different size than the original, use the CSS width and height properties to resize the img element. img { box-sizing: border-box; border: 3px solid red; padding: 3px; height: 90px; width: 90px; object-position: 9999px; background-image: url("https://placekitten.com/90/90"); }
Link: object-position on Mozilla Developer Network.
Avatar by Kaelwolfur. Sent from my PokéNav
© PokéFarm 2009-2024 (Full details)Contact | Rules | Privacy | Reviews 4.6★Get shortlink for this page