我想在CSS的:before
元素中显示一张图片。
.withimage:before {
content: url(path/to/image.svg);
display: block;
height: 20px;
width: 20px;
}
问题在于,height
和width
被应用于元素,但SVG不会缩小。我该如何将大小应用于之前创建的实际元素?
我想在CSS的:before
元素中显示一张图片。
.withimage:before {
content: url(path/to/image.svg);
display: block;
height: 20px;
width: 20px;
}
问题在于,height
和width
被应用于元素,但SVG不会缩小。我该如何将大小应用于之前创建的实际元素?
您可以使用SVG作为背景图像:
.withimage:before {
content: "";
display:block;
height:125px;
width:125px;
background-size: 125px 125px;
background-image: url(test.svg);
background-repeat: no-repeat;
}
这里是示例
此外,您也可以尝试使用以下内容:
.withimage:before {
content: url("data:image/svg+xml; utf8, <svg.. code here</svg>");
display:block;
height:20px;
width:20px;
}
缩放变换对我有用:
::before {
content: url(path/to/image.svg); // source image is 24px wide
transform: scale(0.5); // will be rendered 12px wide
}
大多数浏览器不支持在img标签上使用:after
和:before
,但您可以创建一个具有高度和宽度的div
并为其设置background
/* Styles go here */
.withimage:before {
content:"";
background: url(test.svg);
background-size: cover;
display: block;
width:20px;
height: 10px;
}
.withimage{
background: pink;
height: 100px;
width: 100px;
}