父图像中的垂直居中图像

时间:2015-07-21 14:50:44

标签: javascript html css twitter-bootstrap

我试图将图像垂直居中于父液体容器内。

Here is a fiddle that outlines the problem.

n.val()

如您所见,图像水平居中,但不是垂直居中。像设置边距,甚至是父容器这样的事情似乎不起作用。这感觉它应该很容易解决,但我正在努力解决它。

3 个答案:

答案 0 :(得分:1)

有两种方法可以在其他元素中垂直居中元素,这两种方式都取决于父容器的样式。一种是将行高设置为与容器高度相同,另一种是使用表格单元格:

img {
  height: 25px;
  width: 25px:
}
.container {
  height: 50px;
  width: 50px;  
  text-align: center;
  border:1px solid;
}
.one {
  line-height: 50px; /* Same as container height */
}
.container.two {
  display:table-cell;
  vertical-align:middle;
}
<div class="container one">
  <a href="#">
    <img class="img-responsive" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRnfIk3IX1dY-okcqPH5BX_L0XF2z31qwP-MD0nijTdAGc2alNc4Q" style="vertical-align: middle;" alt="logo here" />
  </a>
</div>
<br />
<div class="container two">
  <a href="#">
    <img class="img-responsive" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRnfIk3IX1dY-okcqPH5BX_L0XF2z31qwP-MD0nijTdAGc2alNc4Q" style="vertical-align: middle;" alt="logo here" />
  </a>
</div>

答案 1 :(得分:1)

我通过制作&#39; a&#39;以不同的方式做到了这一点。有背景图片并匹配高度https://jsfiddle.net/tonytansley/gu8b6yxk/1/

#logo { 
    margin: 0 auto;
    background-image: url('https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRnfIk3IX1dY-okcqPH5BX_L0XF2z31qwP-MD0nijTdAGc2alNc4Q');
    display: inline-block;
    width: 100%;
    height: 52px;
    max-height: 100%;
    background-size: 45px;
    background-repeat: no-repeat;
    background-position: 50% 50%;
}

然后将图像从&#39; a&#39;中取出。标签

<a href="#" id="logo"></a>

答案 2 :(得分:0)

这个怎么样? 只需给外部div一个imagecontainer类,使用一个带有display: inline-block;的辅助类,并将此属性赋予它旁边的img,这样它就会在跨度旁边垂直居中。

HTML:

<div class="container-fluid">
    <div class="row">
      <div class="col-md-2 col-sm-4 col-xs-4 col-xxs-4">
        <div class="imagecontainer">
            <span class="helper"></span>
            <a href="#"><img id="logo" class="img-responsive" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRnfIk3IX1dY-okcqPH5BX_L0XF2z31qwP-MD0nijTdAGc2alNc4Q" alt="logo here"/></a>
        </div>
      </div>
      <div class="col-md-10 col-sm-8 col-xs-8 col-xxs-8">
        <div class="row">
          <div class="col-md-12">
            <div class="">
              <nav class="navbar navbar-default" role="navigation">
                <div class="navbar-header">
                  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#example-navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                  </button>
                </div>
                <div class="collapse navbar-collapse" id="example-navbar-collapse">
                  <ul class="nav navbar-nav navbar-right">
                    <li><a href="#" style="color: #1B75BB;">Search</a>
                    </li>
                    <li><a href="#" style="color: #1B75BB;">Archives</a>
                    </li>
                    <li><a href="#" style="color: #1B75BB;">About</a>
                    </li>
                  </ul>
                </div>
              </nav>
            </div>
          </div>
        </div>
          </div>
        </div>
      </div>

CSS:

.imagecontainer {
    height: 25px;
    white-space: nowrap;
    text-align: center;
    margin: 1em 0;
}
.img-responsive {
    vertical-align: middle;
    height: 25px;
    display: inline-block
}
.helper {
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

https://jsfiddle.net/DTcHh/10286/

答案优雅地改编自 How to vertically align an image inside div