2017-07-08 89 views
1

我有一个圆形按钮,需要在其中心有一个加号图像。事情是,我似乎无法垂直对齐该图像,而不诉诸边际。Bootstrap 4:在圆形按钮中垂直对齐图像

有没有更简单的Bootstrap-y方法来做到这一点?

这里是我的代码:

.btn-circle { 
 
    background-color: blue; 
 
    height: 60px; 
 
    width: 60px; 
 
    border-radius: 50%; 
 
} 
 

 
/* the button is supposed to be a circle, but for some reason it's not showing up like that in this snippet */
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
<a class="btn btn-circle mr-3" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"><img src="https://res.cloudinary.com/dt9b7pad3/image/upload/v1499509391/58a2031e6af142ce619ba2a2_plus-symbol_y6vqca.svg" height="20"></a>

回答

1

的一种方法是使<a..></a>显示:弯曲使用包括d-flex类。

<a class="btn btn-circle mr-3 d-flex justify-content-center align-items-center" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"><img src="https://res.cloudinary.com/dt9b7pad3/image/upload/v1499509391/58a2031e6af142ce619ba2a2_plus-symbol_y6vqca.svg" class="" height="20"></a> 

使用适当的UTIL类中心: justify-content-center align-items-center

https://www.codeply.com/go/BE0Jh6NqrL

+0

这就是它!谢谢,Zim! – Retros

1

这是你想达到什么样的?我垂直居中的<a>包含<img>

position: relative; 
top: 50%; 
-webkit-transform: translateY(-50%); 
-ms-transform: translateY(-50%); 
transform: translateY(-50%) 

引导的border-radius似乎超越你,所以我加了!important它。

.btn-circle { 
 
    background-color: blue; 
 
    height: 60px; 
 
    width: 60px; 
 
    border-radius: 50% !important; 
 
} 
 
.btn-circle a{ 
 
    display: block; 
 
    width: 20px; 
 
    height: 20px; 
 
    position: relative; 
 
    top: 50%; 
 
    -webkit-transform: translateY(-50%); 
 
    -ms-transform: translateY(-50%); 
 
    transform: translateY(-50%); 
 
} 
 
.btn-circle img{ 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
/* the button is supposed to be a circle, but for some reason it's not showing up like that in this snippet */
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
<a class="btn btn-circle mr-3" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"><img src="https://res.cloudinary.com/dt9b7pad3/image/upload/v1499509391/58a2031e6af142ce619ba2a2_plus-symbol_y6vqca.svg"></a>

0

.btn-circle { 
 
    background-color: blue; 
 
    height: 60px; 
 
    width: 60px; 
 
    border-radius: 50%; 
 
    display: flex; 
 
    align-items: center; 
 
    
 
    
 
}
<div class="container"> 
 
    <a class="btn btn-circle mr-3 d-flex justify-content-center align-items-center" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> 
 
     <img style="height: 30px;padding-left:15px;" src="https://res.cloudinary.com/dt9b7pad3/image/upload/v1499509391/58a2031e6af142ce619ba2a2_plus-symbol_y6vqca.svg" class="" height="20"> 
 
    </a> 
 
</div>