html - 为什么添加背景图片后按钮内的文字会重叠?

标签 html css

更新:我更新了我的代码(感谢 Kodos Johnson)。但是我对背景图像的填充有另一个问题。

当我尝试在我的按钮内添加背景图像时,其中的文本移到按钮外。有人可以帮帮我吗?我的代码如下:

<div>           
  <button type="button" id="secondBarButton">See How It Works</button>
</div>

--- Update 2: I want to add padding or margin for the image.

#secondBarButton {
  float: right;
  padding: 5px;
  margin: 10px;
  background-image: url(images/button.png);
}

I cannot make the text move back inside the button and center.

最佳答案

忘记为简单图标使用背景图像吧。使用来自 FontAwesome 的图标,此按钮上使用的特定图标是:

fa-play-circle

片段

<!doctype html>
<html>

<head>
  <meta charset='utf-8'>
  <title>Button</title>
  <link href='https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css' rel='stylesheet'>
  <style>
    #btn1 {
      position: relative;
      width: 300px;
      height: 50px;
      border-radius: 25px;
      background-color: white;
      border: 1px solid #0070BA;
      font-weight: bold;
      font-size: 0.8em;
      color: #0070BA;
      line-height: 1;
      cursor: pointer;
    }
    .fa {
      position: absolute;
      left: 2px;
      top: -2px;
    }
    #btn1:hover {
      background-color: #0070BA;
      border: 1px solid #fff;
      color: #fff;
    }
  </style>
</head>

<body>

  <button id="btn1">
    <i class='fa fa-4x fa-play-circle'></i> See How It Works
  </button>


</body>

</html>

关于html - 为什么添加背景图片后按钮内的文字会重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40775907/

相关文章:

jquery - 单击列表项后如何关闭侧边菜单?

html - 如何垂直对齐导航栏上的图标?

css - 多列在使用 ASP.NET 的 IE11 中不起作用

CSS 3D 转换独立于元素大小?

html - 从 css 中删除拼写错误会奇怪地弄乱显示

html - 从 <ul> 中删除要点

javascript - 如何有效地为多个基于 Canvas 的 THREE.Texture 重用 Canvas

html - 仅当相邻元素发生碰撞时才插入相邻元素的移动元素

html - 是否可以将 Bootstrap 列 "snap"放置到位?

javascript - 将绝对坐标与 transform translate 一起使用