当前位置: 动力学知识库 > 问答 > 编程问答 >

html5 - 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.

网友答案:

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

网友答案:

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);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: