2017-05-06 78 views
0

无论我做什么,按钮都会变得噱头。太复杂了。它在较小的屏幕上分解,并不表现自然。特别是当我将它包含在一列中时。有人可以告诉我正确和干净的方式来做到这一点吗?我真的需要这样做,尽可能简单,当我将它包含在引导程序4中的不同组件中时不会崩溃。Bootstrap 4:左侧带有图标的响应式按钮

无论如何,这是我的代码。我正在使用Bootstrap 4和FontAwesome。我故意将它放在一列中,所以你可以看到文本没有正确对齐。这是正确的方法吗?

.btn { 
 
\t border: none; 
 
\t padding: 20px; 
 
     text-align: center; 
 
     font-size: 10px; 
 
\t display: inline-block; 
 
\t text-transform: uppercase; 
 
\t position: relative; 
 
     background: #42473d; 
 
\t color: #fff; 
 
    } 
 

 
.btn:before { 
 
\t font-family: 'FontAwesome'; 
 
    position: absolute; 
 
\t left: 0; 
 
\t top: 0; 
 
\t line-height: 2.9; 
 
\t font-size: 18px; 
 
\t width: 50px; 
 
    content: "\f09a"; 
 
    background-color: #292c26; 
 
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" rel="stylesheet"/> 
 

 
<div class="col-md-3 mb-2"> 
 
    <button class="btn btn-block rounded-0">View Using Facebook</button> 
 
</div> 
 

 
<div class="col-md-3"> 
 
    <button class="btn btn-block rounded-0">Sign in with Facebook</button> 
 
</div>

回答

1

绝对定位在这种情况下为.btn:before不是必需的。而不是line-height您应该使用padding来定义区域。调整margin-leftmargin-right而不是leftright

我为.btn课增加了额外的margin-bottom。如果你不需要它,你可以删除它。

.btn { 
 
    border: none; 
 
    padding: 20px; 
 
    text-align: center; 
 
    font-size: 10px; 
 
    display: inline-block; 
 
    text-transform: uppercase; 
 
    position: relative; 
 
    background: #42473d; 
 
    color: #fff; 
 
    margin-bottom: 5px; 
 
} 
 

 
.btn:before { 
 
    font-family: 'FontAwesome'; 
 
    font-size: 18px; 
 
    padding: 20px; 
 
    margin-left: -20px; 
 
    margin-right: 20px; 
 
    content: "\f09a"; 
 
    background-color: #292c26; 
 
}
<div class="col-md-3 mb-2"> 
 
    <button class="btn btn-block rounded-0">View Using Facebook</button> 
 
</div> 
 

 
<div class="col-md-3"> 
 
    <button class="btn btn-block rounded-0">Sign in with Facebook</button> 
 
</div>

[更新] 万一按钮是btn-block;最好使用em单元而不是pxhttps://jsfiddle.net/mr6nt5no/4/

+0

是的,但我的按钮是.btn块。它们延伸到容器的整个宽度。如果我使用这种风格,我可以得到:http://pokit.org/get/img/b30c61b36780f970c48a038712a361e4.jpg – Retros

+0

另外,如果我使用不同的图标,并且宽度不同,会发生什么?我是否必须单独设计容器的样式,以匹配上面的容器:http://pokit.org/get/img/c805673f992c67d63055b76321b933f9.jpg有没有更简单的方法可以做到这一点? – Retros

+0

如果按钮是'btn-block',那么你最好使用'em'而不是'px'作为单位。 https://jsfiddle.net/mr6nt5no/3/ –