Search code examples
htmlcsspseudo-elementcss-content

How do I resize an image in a pseudo-element?


I'm trying to do some tricks with pseudo-element.

I'm showing a ranking and I want a crown to show on top of the winner, tilted 45 degrees in the corner of the box where the image will be. But the image is too big, is there a way for me to resize an image that is added on a pseudo-element?

I have only this div:

<div class='box'></div>

With this CSS:

.box {
    width: 50px;
    height: 50px;
    float: left;
    border: 1px solid #000;
    position:absolute;
    top: 100px;
    left: 100px;
}

.box::after{
    content: url(URL_OF_IMAGE);
    position: absolute;
    top: -10px;    
    right: -10px;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
}

I made an example on JSFiddle: http://jsfiddle.net/GEtds/

I tried altering height and width but to no effect.


Solution

  • Since you are already using transforms, go a step further. Use them to move and rescale the image:

    .box::after{
        content: url(http://www.clker.com/cliparts/9/5/e/b/12428065451316964828Simple_crown_icon.svg.med.png);
        position: absolute;
        top: -10px;
        right: -10px;
        width: 0.1em;
        height: 0.1em;
        -webkit-transform: translateY(-35px) rotate(45deg) scale(0.2);
        -moz-transform: translateY(-35px) rotate(45deg) scale(0.2);
    }
    

    updated fiddle