【问题标题】:How to customise button ( CSS, HTML)如何自定义按钮(CSS、HTML)
【发布时间】:2014-11-28 08:56:26
【问题描述】:

我想制作一个带有图片的按钮,该图片将显示在按钮的内部和外部,如下所示:

我不想为按钮使用图像,因为我必须制作至少 5 张图像。 有没有办法用 css 做到这一点?

我正在使用 ASP.NET 和 C#

谢谢

【问题讨论】:

  • 为了做到这一点,你可以使用伪属性来创建按钮,然后在你创建按钮的 div 上调用 bag
  • 感谢您的回复。有示例代码吗?

标签: html css button styles


【解决方案1】:

我听从了marmite的建议,解决了这个问题。 ASP 的按钮可能不喜欢 :before 所以我将它包含在一个带有 css 类 bon 的 div 中,并对其进行适当的样式设置以获得我想要的结果。

页面.aspx

 <div class="bon">
<asp:Button ID="AddToCartBTN" runat="server" CssClass="bot" Text="CART" OnClick="clik"/>
</div>

style.css

.bon {
    position: relative;
    padding: 6px 1em 1em 70px;       
    text-decoration: none; 
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    -moz-box-shadow: 0 1px 3px rgba(0,0,0,0.6);
    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.6);
    display:block;
    background-color: #91bd09;
    height:30px;
    z-index:100;
}
.bon:before {
    content: '';
    position: absolute;
    display: block;
    background: url(path_to_image) no-repeat;
    bottom: 0;
    left: 0;
    height: 100px;
    width: 100px;
    z-index:999;        
}
.bon:hover
{
    background-color: #749a02;
}

.bot {
    width:100%;
    border:none;
    background-color:transparent;
    padding:5px 10px;
    color: #fff;
    font-size: 24px; text-shadow: 0 -1px 1px rgba(0,0,0,0.25);
    border-bottom: 1px solid rgba(0,0,0,0.25);  
     cursor: pointer; 
}

这样做,我可以使用asp的功能而不必改变它的工作方式。

【讨论】:

    【解决方案2】:

    更新:您可以在按钮标签上使用伪元素。使按钮 css position: relative 然后你可以绝对定位伪元素内部,以便它弹出顶部。

    然后向按钮添加左填充,这样文本就不会位于图像下方。

    http://jsfiddle.net/j1zLb8n9/1/

    button {
        position: relative;
        padding: 1em 1em 1em 70px;
        border: 1px solid #333;
        background: #ccc;
    }
    button:before {
        content: '';
        position: absolute;
        display: block;
        background: url(http://placehold.it/60x90/3b3b3d) no-repeat;
        bottom: 0;
        left: 0;
        height: 90px;
        width: 60px;
    }
    

    【讨论】:

    • 感谢您的回复!我试过这个,但我得到这个错误:创建控件时出错 - AddToCartBTNType'System.Web.UI.WebControls.Button'没有名为'Image'的公共属性。我应该提到我正在使用 Asp.net
    • 我更新了答案,这样你就不需要在按钮内添加img标签了。
    • 我会试试这个并返回结果。感谢您的编辑。
    • 它适用于简单的 html 按钮标签,但不适用于 asp:button。你知道为什么吗?
    • 恐怕我不知道 ASP。它会生成什么标记?
    猜你喜欢
    • 2021-12-17
    • 2017-04-23
    • 2014-02-15
    • 2020-08-06
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2013-08-06
    相关资源
    最近更新 更多