【问题标题】:Using CSS to give a black icon another color使用 CSS 为黑色图标赋予另一种颜色
【发布时间】:2012-05-29 16:35:52
【问题描述】:

我看到一些应用程序即使包含黑色图标,也看到了一些应用程序如何使用 CSS 将图标转换为不同颜色的情况。我似乎无法重复这个过程

这是我的 back.css 文件:

.dashboard-buttons a {
    width: 80px; 
    height: 80px; 
    border: 1px solid #ccc; 
    margin: 0px 5px; 
    display:inline-block;
    border-radius: 10px;
    background:white; 
    text-decoration:none;
   -moz-box-shadow: inset 0 0 15px rgba(0,0,0, 0.25);
   -webkit-box-shadow: inset 0 0 15px rgba(0,0,0, 0.25);
}
.dashboard-buttons a:hover {
    text-decoration:underline;
}
.dashboard-buttons span, 
.dashboard-buttons img {
    display:inline; 
    font-size: 12px; 
    font-weight:bold;
}

.dashboard-buttons .sessions img { 
    background-color:#C60; 
}
.dashboard-buttons .sessions img {
    -webkit-mask-image:url(mobile/images/calendar2.png)
}

html 代码如下所示:

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" type="text/css" href="back.css" />
         <title>West</title>
    </head>
    <body>
        <div class="dashboard-buttons">
            <a href="sessions.php" class="sessions">
                <img src="mobile/images/calendar2.png">
                <span>Sessions</span>
            </a>
        </div>
    </body>
</html>

但它在我的 chrome 浏览器中不起作用。我错过了什么吗?

补充说明我只需要支持现代 webkit 浏览器。无需担心 IE 或其他任何事情。

我在这里发布了我的代码 http://jsfiddle.net/ZnbF3/ 。第一次使用 jsfiddle,希望它能正常工作吗?如果没有,只需复制我上面已经发布的 html 文件和 css 文件。我在这个问题上附加了 calendar2.png。

【问题讨论】:

标签: css masking


【解决方案1】:

您的代码不起作用,因为 src 属性被用来在橙色版本之上显示黑色版本。这样,您将能够仅使用 CSS 获得所需的结果:

.dashboard-buttons .sessions .img { width: 60px; height: 60px; background-color: #C60; }
.dashboard-buttons .sessions .img { -webkit-mask-image: url('http://i.stack.imgur.com/gZvK4.png'); }

这里是修改后的 HTML sn-p:

<div class="dashboard-buttons">
   <a href="sessions.php" class="sessions">
       <div class="img"></div>
       <span>Sessions</span>
   </a>
</div>​

这里有一个working sample。

【讨论】:

  • 完美!谢谢你。
【解决方案2】:

尝试为您的图像使用背景图像,然后在第一个 div 中使用另一个 div 来应用 alpha

    <style type="text/css">
        #image{background-image:url(/img/2012-05-24_1834.png);width:400px;height:400px;}
        #image #image_mask{background-color:red;width:400px;height:400px;opacity:0.4;filter:alpha(opacity=40); /* For IE8 and earlier */}
    </style>

</head>


<body>

        <div id="image">
                <div id="image_mask"></div>
       </div>
</body>

抱歉没有使用你的代码,我在你发布之前就开始在你的遮阳篷里工作了

【讨论】:

  • 不透明度的好主意
猜你喜欢
  • 1970-01-01
  • 2020-03-28
  • 2013-10-04
  • 2015-01-31
  • 2015-07-16
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
相关资源
最近更新 更多