How can I align an image to the center in a Bootstrap column?

点点圈 提交于 2019-12-24 17:39:47

问题


Stupid question: I have three images in a row, but they are aligned to the left in their column. How can I align them to the center of the column?

This is my code:

 <div class="container partners">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="swz.png" alt="streetwize" class="img-circle" class="img-responsive" style="max-height:180px">
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="ogon.png" alt="ogon" class="img-circle" class="img-responsive" style="max-height:180px">  
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="congaz.png" alt="ogon" class="img-circle" class="img-responsive" style="max-height:180px">    
</div>
</div>

And CSS:

.partners {
display:block;
margin: 0 auto;
vertical-align:middle;
}

回答1:


Add the text-center class to your column divs

<div class="col-md-4 col-sm-4 col-xs-4 text-center">



回答2:


First of all you can not write 2 class tag on same element

<img src="swz.png" alt="streetwize" class="img-circle" class="img-responsive" style="max-height:180px">

instead of this you can write class="img-circle img-responsive"

for center aligning u can use "text-center" class to parent div.. or you can also apply style="float:none; margin:0 auto;" to img.

thanks, hope this will help you.




回答3:


The idea is to make image block element and margin of auto. but as the margin:auto won't work except you assign some width of the elemet.

.partners img{
    display: block;
    margin: auto;
    text-align: center;
    width: 150px;
}

.partners {
display:block;
margin: 0 auto;
vertical-align:middle;
}
.partners img{
    display: block;
    margin: auto;
    text-align: center;
    width: 150px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
 <div class="container partners">
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="swz.png" alt="streetwize" class="img-circle" class="img-responsive" style="max-height:180px">
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="ogon.png" alt="ogon" class="img-circle" class="img-responsive" style="max-height:180px">  
</div>
<div class="col-md-4 col-sm-4 col-xs-4">
<img src="congaz.png" alt="ogon" class="img-circle" class="img-responsive" style="max-height:180px">    
</div>
</div>



回答4:


<div class="text-center">
<img src="swz.png" alt="streetwize" class="img-circle" class="img-responsive" style="max-height:180px">
</div>

it would work defiantly.



来源:https://stackoverflow.com/questions/31855571/how-can-i-align-an-image-to-the-center-in-a-bootstrap-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!