将图标图像添加到hss元素的css类

8 html css html5 css3

我需要在字体真棒和引导程序的情况下将图标图像添加到HTML元素中.

像这样

在此输入图像描述

例如,我需要将图标图像与css类相关联.假设类名是ico,如果我这样做的话

 <a href="#" class="ico">Email Link</a>
Run Code Online (Sandbox Code Playgroud)

然后它应该如上图所示.

如何使用CSS完成UI设计这一非常重要的功能?

Aks*_*hay 14

你可以使用 pseudo elements

.ico{
    width:100px;
    display:block;
    height:20px;
    background:black;
    color:white;
    text-decoration:none;
    padding-left:20px;
}
.ico:before{
    content: '';
    background:url('https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSj3XfIpf-OOJRCcWz4iN2CU3qMKVVbj0p0bRvKNGVo1U9pk_8ZIlyR8pWreA');
    background-size:cover;
        position:absolute;
    width:20px;
    height:20px;
    margin-left:-20px;
}
Run Code Online (Sandbox Code Playgroud)
 <a href="#" class="ico">Email Link</a>
 <a href="#" class="ico">Another Link</a>
Run Code Online (Sandbox Code Playgroud)


Yaz*_*suf 9

试试这个

.ico:after{
    content: '';
    display: block;
    height: 40px;  /*height of icon */
    width: 40px;  /*width of icon */
    position: absolute;
      /*where to replace the icon */
    top: 0px;
    left: -40px;
      /*background */
    background: #F8E6AE url(ico.gif) no-repeat 0px 0px;
}
Run Code Online (Sandbox Code Playgroud)