raysefo
raysefo

Reputation: 472

Bootstrap row how to center images with different width/height?

I have a web page. I would like to add 6 images in a row with different width and height. I want to place those images inside border with equal margins from both sides/borders. I think I have to get the biggest image width and height and set to the others. I don't want to spread out the image outside the border.

Here is my demo page: http://s14.postimg.org/5mh3d728x/sample.png

EdiT 1: here is sample html

 <div class="row" style="padding-left: 40px;">
        <div class="col-md-1 thumbnailImage">
            <img src="img/bulutistan.jpg" style="width: 78px; padding-top: 11px; padding-bottom: 12px;">
        </div>
        <div class="col-md-1 thumbnailImage">
            <img src="img/datamarket.jpg" style="width: 105px; padding-top: 32px; padding-bottom: 25px;">
        </div>
        <div class="col-md-1 thumbnailImage">
            <img src="img/esq.jpg" style="width: 104px; padding-top: 15px; padding-bottom: 17px;">
        </div>
        <div class="col-md-1 thumbnailImage">
            <img src="img/libelium.jpg" style="width: 103px; padding-bottom: 24px;">
        </div>
        <div class="col-md-1 thumbnailImage">
            <img src="img/microsoft.jpg" style="width: 53px; padding-bottom: 8px;">
        </div>
        <div class="col-md-1 thumbnailImage">
            <img src="img/turkcell.jpg" style="width: 102px; padding-top: 26px; padding-bottom: 27px;">
        </div>
    </div>

Here is CSS:

.thumbnailImage {
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
            padding-top: 7px;
        }

EdiT 2: If I changed it to the below, images are getting bigger. I want to maintain the original image sizes.

Here is the HTML:

<div class="row text-center" style="padding-left: 30em;">
                <div class="col-md-1 thumbnailImage">
                    <img src="images/bulutistan.png" >
                </div>
                <div class="col-md-1 thumbnailImage">
                    <img src="images/datamarket.png" >
                </div>
                <div class="col-md-1 thumbnailImage">
                    <img src="images/esq.png" >
                </div>
                <div class="col-md-1 thumbnailImage">
                    <img src="images/libelium.png" >
                </div>
                <div class="col-md-1 thumbnailImage">
                    <img src="images/microsoft.png" >
                </div>
                <div class="col-md-1 thumbnailImage">
                    <img src="images/turkcell.png" >
                </div>
            </div>

Here is the CSS:

.thumbnailImage {
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
            padding: 7px;
        }
.thumbnailImage img {
      max-width: 100%;
      height: auto;
      min-height: 150px;
    }

Here is the screenshot:http://s21.postimg.org/gslnddahj/sample2.png

EdiT 3: I tried the javascript solution but images get bigger, not centered and div exceeds the screen.

Here is the screenshot:http://s7.postimg.org/m7mxh36qj/sample3.png

Here is the final HTML: **

<div class="row">
                <div class="col-md-2 ">
                    <img src="images/bulutistan.png" class="img-responsive">
                </div>
                <div class="col-md-2 ">
                    <img src="images/datamarket.png" class="img-responsive">
                </div>
                <div class="col-md-2 ">
                    <img src="images/esq.png" class="img-responsive">
                </div>
                <div class="col-md-2 ">
                    <img src="images/libelium.png" class="img-responsive">
                </div>
                <div class="col-md-2 ">
                    <img src="images/microsoft.png" class="img-responsive">
                </div>
                <div class="col-md-2 ">
                    <img src="images/turkcell.png" class="img-responsive">
                </div>
            </div>

**

EdiT 4: Here is the final markup and CSS. I wonder if there is a way to make images fit in one row.

The sample demo: http://s12.postimg.org/647dighkt/sample.png

HTML:

 <div class="row equal" >

                <div class="col-md-1 thumbnailImage imgc">
                    <img src="images/bulutistan.png" >
                </div>
                <div class="col-md-1 thumbnailImage imgc">
                    <img src="images/datamarket.png" >
                </div>
                <div class="col-md-1 thumbnailImage imgc">
                    <img src="images/esq.png" >
                </div>
                <div class="col-md-1 thumbnailImage imgc">
                    <img src="images/libelium.png" >
                </div>
                <div class="col-md-1 thumbnailImage imgc">
                    <img src="images/microsoft.png" >
                </div>
                <div class="col-md-1 thumbnailImage imgc">
                    <img src="images/turkcell.png" >
                </div>
            </div>

CSS:

.equal, .equal > div[class*='col-'] {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    flex: 1 0 auto;
}
div.imgc img{
   display:block;
   margin:auto;
}

Thanks in advance & Best Regards.

Upvotes: 2

Views: 4717

Answers (2)

Bruno J. S. Lesieur
Bruno J. S. Lesieur

Reputation: 3885

You could try this method as exposed on

All your images will be Responsive thanks to Bootstrap .img-responsive OOCSS Class Pattern. That mean will be have the real size if your column have enought space. In Mobile, this images will be horizontaly center and each image to be displayed to a new line.

But above 992px (as you setted with md Bootstrap), the behavior you want is applied and each image will be verticaly centered into column (and each column will be have the same height).

On this HTML:

<div class="container">
  <div class="row">
    <div class="col-md-2">
      <img class="img-responsive" src="https://lh6.ggpht.com/SeHZRXds3ossNP6yOrniffP0m7LcfONuzuy4uHQGfe1PT9B0SXIlGP0vExImCo38MM0=w300">
    </div>
    <div class="col-md-2">
      <img class="img-responsive" src="https://s-media-cache-ak0.pinimg.com/736x/1d/89/3e/1d893e8180f8b57988f4ffa1188bb015.jpg">
    </div>
    <div class="col-md-2">
      <img class="img-responsive" src="https://s-media-cache-ak0.pinimg.com/736x/1d/89/3e/1d893e8180f8b57988f4ffa1188bb015.jpg">
    </div>
    <div class="col-md-2">
      <img class="img-responsive" src="https://lh6.ggpht.com/SeHZRXds3ossNP6yOrniffP0m7LcfONuzuy4uHQGfe1PT9B0SXIlGP0vExImCo38MM0=w300">
    </div>
    <div class="col-md-2">
      <img class="img-responsive" src="https://lh4.ggpht.com/RxProdoIP2t3XhpSjNlJix9rL23mKTA7APWYoV-D9W3wLXP3S4jiM7X4Z5_M5gVA31k=h900">
    </div>
    <div class="col-md-2">
      <img class="img-responsive" src="https://lh4.ggpht.com/RxProdoIP2t3XhpSjNlJix9rL23mKTA7APWYoV-D9W3wLXP3S4jiM7X4Z5_M5gVA31k=h900">
    </div>
  </div>
</div>

with this CSS:

.col-md-2 {
  position: relative;
  border: 1px solid #f00;
}

.img-responsive {
  margin-left: auto;
  margin-right: auto;
}

@media (min-width: 992px) {
  .img-responsive {
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}

Create this JavaScript function:

/**
 * Maintain the height of all object in htmlCollection.
 * @function maintainedSameHeight
 * @param  {HTMLCollection} htmlCollection - All HTMLElement need to kept the same height.
 * @param  {string}         mediaQueries   - Under what responsive condition the calcul is apply.
 */
function maintainedSameHeight(htmlCollection, mediaQueries) {
  var maxHeight = 0;

  [].forEach.call(htmlCollection, function(htmlElement) {
    htmlElement.style.height = "";
    maxHeight = (htmlElement.offsetHeight > maxHeight) ? htmlElement.offsetHeight : maxHeight;
  });

  if (!mediaQueries || window.matchMedia(mediaQueries).matches) {
    [].forEach.call(htmlCollection, function(htmlElement) {
      htmlElement.style.height = maxHeight + "px";
    });
  }
};

And apply it:

/* Create a function to apply on the `.col-md-1` already in the DOM the previous created function. */
function setHeightOnImages() {
  maintainedSameHeight(
        // Target all column you want inspect to find the most height.
        document.querySelectorAll(".col-md-2"),
        // Because under 992px, you will be have one column, there are no necessity to maintain same height in this case.
        "(min-width: 992px)"
  );
}

/* Apply at first time. */
window.addEventListener("load", setHeightOnImages);
/* And update in case of Responsive. */
window.addEventListener("resize", setHeightOnImages);

Upvotes: 1

yeouuu
yeouuu

Reputation: 1913

You can use the class text-center on your row div.

<div class="row text-center" style="padding-left: 40px;">

and in css:

.thumbnailImage img {
      max-width: 100%;
      height: auto;
    }

Working jsfiddle: https://jsfiddle.net/gvpy9egs/1/

if you want the height fixed on a certain amount, you could just do it like this:

.thumbnailImage {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 7px;
}

.thumbnailImage img {
  max-width: 100%;
  max-height: 25px;
  min-height: 25px;
}

https://jsfiddle.net/gvpy9egs/3/

I personally think it's overkill to do this in javascript, and update it onresize.

Upvotes: 1

Related Questions