Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically center responsive image in a bootstrap column

I have reviewed all of the existing questions / answers, and still cannot get a working solution, so I would appreciate any comments on my particular code below.

I would prefer not to use hard-coded image sizes (pixel counts) in my code, and I am willing to use a combination of Less, CSS, or Javascript in the solution.

The image should be centered vertically and horizontally within the Bootstrap column, and remain centered even when the screen is resized.

My HTML:

<div class="container-fluid">
    <div class="container">
    <div class="row">
        <div class="col-md-5">
            <div class="panel">
                Some content
            </div>
        </div>

        <div class="col-md-2">
            <img src="images/myimage.png" class="img-responsive center-block">
        </div>

        <div class="col-md-5">
            <div class="panel>
               Some content
            </div>
        </div>
     </div>
     </div>
 </div>
like image 892
user84756 Avatar asked Dec 15 '14 17:12

user84756


People also ask

How to center align the responsive image in Bootstrap?

You can solve this problem to some extent by center aligning the responsive image through applying an addition class .center-block on it, along with the .img-responsive class in Bootstrap 3.

How to center a column in Bootstrap/Sass prev next?

How to Center a Column in Bootstrap. Topic: Bootstrap / Sass Prev|Next. Answer: Use the mx-auto Class. If you are using the Bootstrap 4 version, you can horizontally center a grid column by applying the class .mx-auto on it. Let's try out the following example to see how it works:

How to center a grid column horizontally in Bootstrap?

If you are using the Bootstrap 4 version, you can horizontally center a grid column by applying the class .mx-auto on it. Let's try out the following example to see how it works: <div class="container"> <div class="row"> <div class="col-sm-6 mx-auto"> <!--

What is vertical alignment of Div in Bootstrap?

Vertical Alignment of div is one of the most basic requirements of a responsive web page. This can be achieved through CSS but the Bootstrap library has some classes specifically built for this purpose. In this article, we will learn the available classes & methods used for vertical-align in Bootstrap.


1 Answers

Here's an option using transform:

html,body,.container-fluid,.container,.row,.row div {height:100%;;} /* full height for demo purposes */
.vertical-center {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}
.col-md-2 {background-color:#ccc;} /* just to demonstrate the size of column */

http://www.bootply.com/YG8vpg1rIf

like image 193
Patrick Avatar answered Oct 19 '22 22:10

Patrick