简体   繁体   English

Bootstrap 3垂直对齐缩略图中的图像

[英]Bootstrap 3 vertical align image in thumbnail

I am trying to vertical align an image within a bootstrap thumbnail. 我正在尝试在引导缩略图中垂直对齐图像。 Thanks for any help! 谢谢你的帮助!

<div class="pull-left" style="margin-right: 10px;">
                <div class="thumbnail">
                    <div class="caption" style="background-color: #ccc;">
                        <a style="color: black;" href="/product/?id=@product.Id">@product.UPC12</a>
                    </div>

                    <div style="width: 150px; height: 150px; "> <!-- Center this -->
                        <a style="" href="#" onclick="showProduct('@product.Id')">
                            <img class="" src="~/Asset.ashx?id=1253&type=small" />
                        </a>
                    </div>

                    <div style="padding-left: 5px;" class="checkbox">
                        <label>
                            @Html.Partial("~/Views/Shared/_ProductImageCheckboxPartial.cshtml", new Logix3.TDC.Exchange.Web.Models.ProductImageModel() { Product = product, Image = defaultImage })
                        </label>
                    </div>
                </div>
            </div>

I assume you wanted to achieve something like this, using different-sized images? 我想您想使用不同大小的图像来实现这样的目标? 在此处输入图片说明

I've looked and looked for a simple answer to this and nothing ever seemed to work well, so I managed to take a few bits and hack something together. 我一直在寻找并寻求一个简单的答案,似乎没有任何工作能很好地完成工作,因此我设法花了点力气并一起破解了一些东西。

It's a little convoluted but it works really well for me, and also resizes and centers both landscape and portrait images. 这有点令人费解,但对我来说确实很好,并且可以调整横向和纵向图像的大小并使其居中。 It also lets me set the dimension ratios I want the image to be (adjust the padding-top percentage in ".thumb:before"). 它还可以让我设置图像的尺寸比例(调整“ .thumb:before”中的padding-top百分比)。

Bootply Example at 1/1 ratio (square). Bootply示例以1/1的比率(平方)。 (click on the image to see the original) (点击图片查看原图)

Bootply Example at slight portrait ratio (125%) 轻微肖像比例(125%)的Bootply示例

This needs two custom css classes. 这需要两个自定义的CSS类。

The 'thumb' class is assigned to the div and the image url is set as a background. 将'thumb'类分配给div,并将图像网址设置为背景。

Since it's poor form to embed a div inside an anchor tag, I also created a 'clickable' class, which takes the inside anchor tag, sizes it to the parent container, and floats it above the parent so that it mimics clicking the image. 由于将div嵌入锚标记中的形式很差,因此我还创建了一个'clickable'类,该类将内部锚标记放入内部容器中,将其调整到父容器的大小,然后将其浮动在父容器上方,以便模仿单击图像。

HTML: HTML:

<div class="col-xs-1">
  <div class="clickable thumb" style="background-image: url('http://auduno.github.io/clmtrackr/media/audrey.jpg')">
    <a href="http://auduno.github.io/clmtrackr/media/audrey.jpg">
    </a>
  </div>
  <div class="text-center"><small>Product 15</small></div>
</div> 

CSS: CSS:

.thumb{
  background-position: 50% 50%;
  background-repeat: no-repeat;
  background-size:cover;
  position:relative;
  width:100%;
  border:1px solid #BBB;
}
.thumb:before {
    content: "";
    display: block;
    padding-top: 100%;
}

.clickable > a{
  position:absolute; 
  width:100%; 
  height:100%; 
  top:0; 
  left:0; 
  text-decoration:none; /* Makes sure the link doesn't get underlined */ 
  z-index:10; /* raises anchor tag above everything else in div */

  /* For IE */
  background-color:white; /*workaround to make clickable in IE */ 
  opacity: 0; /*workaround to make clickable in IE */ 
  filter: alpha(opacity=1); /*workaround to make clickable in IE */
  //from http://blog.avtex.com/2012/01/27/how-to-make-an-entire-div-clickable-with-css/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM