Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 771
Rodrigo Sasaki Avatar asked Dec 20 '22 23:12

Rodrigo Sasaki


2 Answers

You can do it by setting the image as background-image, in combination with background-size: cover; this will let you control the size of the image by setting the width and height on the pseudo-element. You'll also need to add content: ""; to ensure the pseudo-element is rendered.

Here's a jsFiddle

CSS

.box::after {
    display: block;
    content: "";
    background-image: url(http://www.clker.com/cliparts/9/5/e/b/12428065451316964828Simple_crown_icon.svg.med.png);
    background-size: cover;
    position: absolute;
    top: -10px;
    right: -10px;
    width: 40px;
    height: 40px;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
}
like image 142
tom-19 Avatar answered Dec 31 '22 04:12

tom-19


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

like image 34
vals Avatar answered Dec 31 '22 04:12

vals