使用 css/html 将 svg 图标添加到按钮中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34393465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:40:46  来源:igfitidea点击:

Add svg icon into button with css/html?

htmlcsssvg

提问by Mikael

I want to have the icon displayed inside the button tag, see code below:

我想在按钮标签内显示图标,请参阅下面的代码:

#header-search {
  width: 200px;
  background: @header-color;
  color: white;
  font-size: 12pt;
  border: 0px solid;
  outline: 0;
  vertical-align: -50%;
}

#header-search::-webkit-input-placeholder {
  color: white;
}

#search-button {
  background: #FFFFFF;
  vertical-align: -50%;
}

.header-view-logo {
  vertical-align: middle;
}

#search-icon {
  fill: white;
}
<button id="search-button" />
<svg id="search-icon" class="search-icon" viewBox="0 0 24 24">
    <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
    <path d="M0 0h24v24H0z" fill="none"/>
</svg>

But the icon is just displayed all over the place really big one, how can i get it to fit inside the button ?

但是图标只是显示在一个非常大的地方,我怎样才能让它适合按钮?

回答by shadeed9

Not sure if the button is not closed or just you copied it here and forgot to close it.

不确定按钮是否未关闭,或者只是您将其复制到此处而忘记关闭它。

#search-button {
  width: 100px;
  height: 50px;
}
    
#search-button svg {
  width: 25px;
  height: 25px;
}
<button id="search-button">
    <svg id="search-icon" class="search-icon" viewBox="0 0 24 24">
        <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
        <path d="M0 0h24v24H0z" fill="none"/>
    </svg>
 </button>

回答by JosephGarrone

I've modified your code a bit an put it up at thisfiddle.

我稍微修改了你的代码,把它放在这个小提琴上。

The two problems were:

这两个问题是:

  1. You didn't close your buttonproperly, it needs to end afterthe SVG
  2. You needed to specify a width for the button (As you can see I left off some of your other CSS)
  1. 你没有button正确关闭你的,它需要在 SVG之后结束
  2. 您需要为按钮指定宽度(如您所见,我省略了一些其他 CSS)

回答by iyyappan

I have checked your code,Plz try to do this.

我已经检查了您的代码,请尝试执行此操作。

box-model(Depending on height,weight,margin,padding) only reason.

box-model(取决于高度,重量,边距,填充)唯一的原因。

<!DOCTYPE html>
<html>
<head>


<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <style type="text/css">

  #header-search {
    border: 1px solid #eee;
    color: black;
    float: left;
    font-size: 12pt;
    height: 40px;
    margin: 0;
    outline: 0 none;
    padding: 0 10px;
    vertical-align: top;
    width: 200px;
}

#header-search::-webkit-input-placeholder {
    color: white;
}

#search-button {
    background-color: red !important;
    height: 40px;
    margin: 0 auto !important;
    padding: 0;
    width: 40px;
}
.header-view-logo {
    vertical-align: middle;
}

#search-icon {
    fill:white;
}
    </style>

</head>
<body>
    <div>
<input type="search" id="header-search" placeholder="Search..." />
<button id="search-button">
<svg id="search-icon" class="search-icon" viewBox="0 0 24 24">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z">
<path fill="none" d="M0 0h24v24H0z">
</svg>
</button>

</body>

</html>

回答by Pmpr

I suggest you, doing it this way:

我建议你这样做:

.search > div, .search > button {
    display: inline-block;
    vertical-align: middle;
}
#header-search{
    margin: 0;
    line-height: 0;
}
#search-button svg {
  width: 12px;
  height: 12px;
}
<div class="search">
    <input id="header-search" type="search" placeholder="Search..." />
    <button id="search-button">
        <svg id="search-icon" class="search-icon" viewBox="0 0 24 24">
            <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
            <path d="M0 0h24v24H0z" fill="none"/>
        </svg>
    </button>
</div>

回答by Bijal

<svg height="20" width="40" >
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5  3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>