【问题标题】:how can I vertically align a google icon within a button? (ie11)如何垂直对齐按钮内的谷歌图标? (即11)
【发布时间】:2018-03-19 13:06:00
【问题描述】:

我在按钮内的一些谷歌图标的垂直对齐方面遇到了问题。我尝试使用填充和边距,但无法解决问题。

这是问题的截图:如您所见,图标位置略高:

这是html的一部分,每个按钮都差不多:

<div id="mainToolbar">
    <button id="buttonPencil" data-tool="yes" data-type="draw" title="Use pencil" class="button" onclick="changeTool(0);">
        <i class="material-icons">brush</i>
    </button>
</div>

这是按钮的css:

.button {

    margin: auto;
    display: inline-block;
    margin-top: 5px;

    color: black;
    border: 0px solid grey;
    border-radius: 6px;

    background-color: #EFEFEF;

    text-align: center;
    text-decoration: none;
    cursor: pointer;

    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    z-index: 4;
    vertical-align: middle;

}

.button {
    width: 50px;
    height: 40px;
}

.button:hover {
    background-color: #aaaaaa !important;
    color: white !important;
}

最后是 div 的 css:

#mainToolbar {
    position: absolute;
    top: 0px;
    left: 0px;
    height: 520px;
    width: 60px;
    z-index: 10;
    text-align: center;

}

如何将图标放在按钮的中间(垂直和水平)?谢谢。

【问题讨论】:

    标签: html css button


    【解决方案1】:

    您可以使用绝对定位和平移来将图标定位在中间。请务必在button 上添加position:relative,以便图标位于按钮的右侧。

    .button {
    
        margin: auto;
        display: inline-block;
        margin-top: 5px;
    
        color: black;
        border: 0px solid grey;
        border-radius: 6px;
    
        background-color: #EFEFEF;
    
        text-align: center;
        text-decoration: none;
        cursor: pointer;
    
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        z-index: 4;
        vertical-align: middle;
        
    }
    
    .button {
        width: 50px;
        height: 40px;
        position: relative;
    }
    
    .button:hover {
        background-color: #aaaaaa !important;
        color: white !important;
    }
    
    .button:active i{
        /*for push effect on click*/
        transform: translate(-45%, -45%);
    }
    
    .button i {
      /*horizontal and vertical centering*/
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    
    #mainToolbar {
        position: absolute;
        top: 0px;
        left: 0px;
        height: 520px;
        width: 60px;
        z-index: 10;
        text-align: center;
    
    }
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons"
          rel="stylesheet">
    <div id="mainToolbar">
        <button id="buttonPencil" data-tool="yes" data-type="draw" title="Use pencil" class="button" onclick="changeTool(0);">
            <i class="material-icons">brush</i>
        </button>
    </div>

    解释: top:50%position: absolute 会将图标向下移动到父级高度的 50%。带有 -50% 的 translateY 会将图标向上移动一半高度,使其在中间对齐。与水平居中类似。

    【讨论】:

    • 感谢您的示例,它运行良好。不幸的是,现在还有另一个问题:当我按下按钮时,图标不再具有“按下”效果。和我在这篇文章中提到的基本相同的问题(如果你有时间请看一下):stackoverflow.com/questions/49356458/…
    • 你说的“推”效应是什么意思?是JS做的吗?能不能在你上面提到的问题里加那个代码?
    • 不,这是按钮的默认行为,没有添加 js。默认情况下,当您按下按钮时,内容会显示为已按下(它基本上会向右和向下移动几个像素)。
    • 我制作了一个效果的 gif,我的意思是:media.giphy.com/media/pyhAVLYHxdIfM2Go2o/giphy.gif 添加变换后,它只是保持平坦,没有动画。
    • 我也默认看不到效果。如果您可以上传工作代码 sn-p,我也许可以弄清楚为什么它不起作用。
    【解决方案2】:

    它的工作正常见 sn-p

    .button {
    
        margin: auto;
        display: inline-block;
        margin-top: 5px;
    
        color: black;
        border: 0px solid grey;
        border-radius: 6px;
    
        background-color: #EFEFEF;
    
        text-align: center;
        text-decoration: none;
        cursor: pointer;
    
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        z-index: 4;
        vertical-align: middle;
    
    }
    
    .button {
        width: 50px;
        height: 40px;
    }
    
    .button:hover {
        background-color: #aaaaaa !important;
        color: white !important;
    }
    And finally the css for the div:
    
    #mainToolbar {
        position: absolute;
        top: 0px;
        left: 0px;
        height: 520px;
        width: 60px;
        z-index: 10;
        text-align: center;
    
    }
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    
    <div id="mainToolbar">
        <button id="buttonPencil" data-tool="yes" data-type="draw" title="Use pencil" class="button" onclick="changeTool(0);">
            <i class="fa fa-leaf"></i>
        </button>
    </div>

    【讨论】:

    • 我看到你使用的是发发图标;谷歌图标可能是这里的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2015-04-20
    • 2013-07-02
    • 1970-01-01
    • 2018-05-07
    相关资源
    最近更新 更多