在自定义引导按钮中将文本与图标字体对齐



我正在尝试将一些文本与具有自定义高度的按钮中的图标字体图标对齐。图标和文本都应位于按钮的垂直中心,左侧和右侧填充一些像素,以便它们都位于按钮的水平中心。设置图标大小的属性似乎也不起作用。

/* Styles go here */
.btn-group{
  margin-top: 20px;
  margin-left: 20px;
}
#left, #middle, #right{
  height: 25px;
}
.button-content{
  
}
.button-icon{
  
}
.button-text{
  vertical-align:middle;
}
.button-text i{
  font-size: 12px; /* Change of icon-size doesn´t apply */
}
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  </head>
  <body>
    
    <div class="btn-group">
      <label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Left'" id="left">
        <span class="buton-content">
          <span class="button-icon"><i class="material-icons">info</i></span>
          <span class="button-text">CONTENT</span>
        </span>
      </label>
      <label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Middle'" id="middle">
        <span class="buton-content">
          <span class="button-icon"><i class="material-icons">info</i></span>
          <span class="button-text">CONTENT</span>
        </span>
      </label>
      <label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Right'" id="right">
        <span class="buton-content">
          <span class="button-icon"><i class="material-icons">info</i></span>
          <span class="button-text">CONTENT</span>
        </span>
      </label>
    </div>
    
  </body>
</html>

你也可以在Plunker上找到我的代码。


编辑

除了图标和文本不在按钮的垂直中心之外,我修复了所有问题。

Plunker也进行了更新。


编辑2:

通过使用@MarcosPérezGude的想法解决了问题

#left, #middle, #right{
  height: 26px;
  padding-top: 1px;
  padding-bottom: 0px;      
}

除了用部分解决方案回答@iyyappan

.material-icons {vertical-align: middle;}

我检查了你的代码。

请尝试一下。

#left, #middle, #right {
    height: auto;
}
.material-icons {vertical-align: middle;}

请将vertical-align: middle;添加到.material-icons类中。

 .material-icons {
    vertical-align: middle;
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
    -webkit-font-feature-settings: 'liga';
    -webkit-font-smoothing: antialiased;
        }

    .btn-group{
      margin-top: 20px;
      margin-left: 20px;
    }
    #left, #middle, #right{
      height: 25px;
    }
    .button-content{
      
    }
    .button-icon{
      
    }
    .button-text{
      vertical-align:middle;
    }
    .button-text i{
      font-size: 12px; /* Change of icon-size doesn´t apply */
    }
    #left, #middle, #right {
        height: auto;
    }
    .button-icon {
        display: inline-block;
        vertical-align: middle;
        line-height: 0;
    }
    	
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">


     <div class="btn-group">
          <label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Left'" id="left">
            <span class="buton-content">
              <span class="button-icon"><i class="material-icons">info</i></span>
              <span class="button-text">CONTENT</span>
            </span>
          </label>
          <label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Middle'" id="middle">
            <span class="buton-content">
              <span class="button-icon"><i class="material-icons">info</i></span>
              <span class="button-text">CONTENT</span>
            </span>
          </label>
          <label class="btn btn-success" ng-model="radioModel" uib-btn-radio="'Right'" id="right">
            <span class="buton-content">
              <span class="button-icon"><i class="material-icons">info</i></span>
              <span class="button-text">CONTENT</span>
            </span>
          </label>
        </div>

只需从自定义CSS文件格式中删除以下代码.css这会导致高度不正确的问题。

#left, #middle, #right {
    /* height: 26px; */
}

您的自定义 CSS 高度将覆盖默认引导程序按钮的高度

请注意: 因此,您无法在中间看到您的内容,但 (实际上在中间)。

引导参考链接中的按钮大小

尝试这可能会对您有所帮助,并更改图标的大小使用font-size

#left, #middle, #right
{
height:26px;
}
.material-icons 
{
vertical-align: middle;
font-size:13px;
}
.btn
{
padding:2px 12px;
}

最新更新