简体   繁体   English

如何使 png 图像充当按钮?

[英]How can I make png image act as a button?

I am using Dreamweaver for the first time to code.我第一次使用 Dreamweaver 进行编码。 I am intermediate in HTML.我是 HTML 中级。

I've been trying to use a png file as a button.我一直在尝试使用 png 文件作为按钮。 I've found some sources stating that a ...我发现一些消息来源说...

<button src=Home_button> </button>    

... will work, but I have tried it, and to no avail, it does not work. ...会起作用,但我已经尝试过了,但无济于事,它不起作用。 Any suggestions?有什么建议?


NOTE:笔记:

I am also using a CSS to build this Very basic website.我还使用 CSS 来构建这个非常基本的网站。

Just add background-image to your button.只需将background-image添加到您的按钮。

<button id="home_button">click me</button>

and then add:然后添加:

#home_button {
  background-image: url(...);
}

You may need to add further styling to it of course, such as widths and other background properties to get it just right.当然,您可能需要为其添加进一步的样式,例如宽度和其他背景属性以使其恰到好处。 But this is how you add a background image to a button.但这就是向按钮添加背景图像的方式。

Here's a working demo as well:这里还有一个工作演示:

 #home_button { width: 150px; height: 150px; background-image: url('http://i.stack.imgur.com/sfed8.png'); background-size: cover; background-color: #eee; }
 <button id="home_button"></button>

您可以在button标签中添加一个img标签。

 <button id="bar" type="submit"><img src="http://cdn.sstatic.net/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a" /></button>

There are many ways to create something that looks like an image and behaves like a button.有很多方法可以创建看起来像图像并且行为像按钮的东西。

Here-below are code examples demonstrating 5 options worth considering...下面是代码示例,演示了 5 个值得考虑的选项......


Option 1 :选项1 :

You could put an <img> element inside a <button> element :您可以将<img>元素放在<button>元素中:

 document.getElementById("myButton").addEventListener("click", function() { alert('Button clicked'); });
 button, img { padding: 0; font-size: 0; border: none; }
 <button id="myButton"> <img src="http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150" alt=""> </button>

(see also this Fiddle ) (另见这个小提琴


Option 2 :选项2:

You could use an <a> -tag instead :您可以使用<a> -tag 代替:

 document.getElementById("myButton").addEventListener("click", function() { alert('Button clicked'); });
 a { border: none; display: inline-block; font-size: 0; }
 <a id="myButton" href="#"> <img src="http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150" alt=""> </a>

(see also this Fiddle ) (另见这个小提琴


Option 3 :选项 3:

You could just attach your click handler directly to your image :您可以将点击处理程序直接附加到您的图像:

 document.getElementById("myButton").addEventListener("click", function() { alert('Button clicked'); });
 <img id="myButton" src="http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150" alt="">

(see also this Fiddle ) (另见这个小提琴


Option 4 :选项 4:

You could set your image as a background-image of a <button> -tag or other tag that represents a button.您可以将图像设置为<button> -tag 或其他代表按钮的标记的背景图像。

 document.getElementById("myButton").addEventListener("click", function() { alert('Button clicked'); });
 input[type=submit] { background: url("http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150"); width: 150px; height: 150px; border: none; }
 <input type="submit" id="myButton">

(see also this Fiddle ) (另见这个小提琴


Option 5 :选项 5:

You could set your image as a background-image of an <a> -tag, a <div> -tag or another tag that doesn't represent a button :您可以将图像设置为<a> -tag、 <div> -tag 或其他不代表按钮的标签的背景图像:

 document.getElementById("myButton").addEventListener("click", function() { alert('Button clicked'); });
 div { background: url("http://www.gravatar.com/avatar/bf4cc94221382810233575862875e687?s=150"); width: 150px; height: 150px; border: none; }
 <div id="myButton"></div>

(see also this Fiddle ) (另见这个小提琴

你可以做<a href="#"><img src="#" alt="" /></a>来实现你想要的。

There are several ways to use an image as a button..有几种方法可以将图像用作按钮。

    <a href="your_link.html">
      <img id="mypic' src="path_to_your.png" alt="MyImage" width="200" height="300" />
    </a>
css..
#mypic {
    background: #390239;
    color:#7e4a00;
    border:#7e4a00 2px solid;
    border-color:;
    outline:none;
    padding: 6px;
    border-radius:5px;
    text-align: center;
}

Chris also has a good answer... it depends on what looks good on the page, as well as your coding knowledge. Chris 也有一个很好的答案……这取决于页面上看起来不错的内容以及您的编码知识。

Here's what you can do...这是你可以做的...

HTML HTML

<button>Click Me</button>

CSS CSS

button {
  display: inline-block;
  height: 50px // height of your button image
  text-indent: -9999px // hide if you have text inside <button>
  width: 100px // width of your button image
}

Hope this will help :)希望这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM