如何通过CSS将图像添加到某些文本?
我有这个:
<span class="create">Create something</span>
我想通过使用CSS在其左侧添加一个16x16图像。这是可能的还是我应该像这样手动添加此图像:
<span class="create"><img src="somewhere"/>Create something</span>
我宁愿不必手动更改所有位置,这就是为什么我想通过CSS进行更改。
谢谢!
Answers:
.create
{
background-image: url('somewhere.jpg');
background-repeat: no-repeat;
padding-left: 30px; /* width of the image plus a little extra padding */
display: block; /* may not need this, but I've found I do */
}
尝试填充和可能的边距,直到获得所需的结果。您也可以使用“ background-position”来处理背景图像的位置(*向Tom Wright致意),或者完全定义“ background”(链接到w3)。
尝试类似:
.create
{
margin: 0px;
padding-left: 20px;
background-image: url('yourpic.gif');
background-repeat: no-repeat;
}
width:2em; background-size: contain;
为了限制图像的大小,添加可能也很有用。
这很棒
.create:before{
content: "";
display: block;
background: url('somewhere.jpg') no-repeat;
width: 25px;
height: 25px;
float: left;
}
url()
吗?