【问题标题】:How to vertically align an image inside a div如何垂直对齐div内的图像
【发布时间】:2011-11-12 06:49:33
【问题描述】:

如何在包含div 的内部对齐图像?

示例

在我的示例中,我需要将<div> 中的<img>class ="frame 垂直居中:

<div class="frame" style="height: 25px;">
    <img src="http://jsfiddle.net/img/logo.png" />
</div>

.frame 的高度是固定的,图像的高度是未知的。如果这是唯一的解决方案,我可以在 .frame 中添加新元素。我正在尝试在 Internet Explorer 7 及更高版本、WebKit、Gecko 上执行此操作。

请参阅 jsfiddle here

.frame {
    height: 25px;      /* Equals maximum image height */
    line-height: 25px;
    width: 160px;
    border: 1px solid red;

    text-align: center;
    margin: 1em 0;
}
img {
    background: #3A6F9A;
    vertical-align: middle;
    max-height: 25px;
    max-width: 160px;
}
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
   <img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=15 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=13 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=11 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=9 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=7 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=5 />
 </div>
<div class=frame>
    <img src="http://jsfiddle.net/img/logo.png" height=3 />
 </div>

【问题讨论】:

  • 您好,抱歉,但我不同意在这里使用助手作为最有价值的解决方案。但这不是唯一的方法。其他浏览器同样支持。我在 stackoverflow.com/a/43308414/7733724 和 W3C.org 上提供了有关信息的解决方案。你可以检查一下。干杯
  • 阅读有关 W3C 的 Centering Things 文章会很有用:w3.org/Style/Examples/007/center.en.html
  • 我认为关键是 .frame 中的行高来完成这项工作

标签: css image vertical-alignment


【解决方案1】:

对于更现代的解决方案,如果不需要支持旧版浏览器,您可以这样做:

.frame {
    display: flex;
    /**
    Uncomment 'justify-content' below to center horizontally.
    ✪ Read below for a better way to center vertically and horizontally.
    **/

    /* justify-content: center; */
    align-items: center;
}

img {
    height: auto;

    /**
    ✪ To center this image both vertically and horizontally,
    in the .frame rule above comment the 'justify-content'
    and 'align-items' declarations,
    then uncomment 'margin: auto;' below.
    **/

    /* margin: auto; */
}

/* Styling stuff not needed for demo */
.frame {
    max-width: 900px;
    height: 200px;
    margin: auto;
    background: #222;
}
p {
    max-width: 900px;
    margin: 20px auto 0;
}
img {
    width: 150px;
}
<div class="frame">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/9988/hand-pointing.png">
</div>

这是使用 Flexbox 的 Pen:http://codepen.io/ricardozea/pen/aa0ee8e6021087b6e2460664a0fa3f3e

编辑 1/13/22

使用 CSS Grid 和 place-content 速记有更好的方法:

.frame-text-grid {
    display: grid;
    place-content: center;
    /**
    ✪ "place-content" is the shorthand for "align-content" and "justify-content".    
    ✪ The "place-content" shorthand requires two values, the first one is for "align-content" and the second one for "justify-content". If only one value is present (like in this demo), then that single value is applied to both directions.    
    ✪ Comment the "place-content: center;" declaration above to see how the elements are spread along the height of the container.
    **/
}
<div class="ctnr frame-text-grid">
    <h2>Using Grid and <code>place-content</code></h2>
    <p>Only two lines are needed to center vertically and horizontally.</p>
</div>

这是一支使用 CSS Grid 的 Pen:https://codepen.io/ricardozea/pen/c4e27f1e74542618d73e21f7c2276272?editors=0100

【讨论】:

    【解决方案2】:

    CSS 网格

    如果您想在图像容器内垂直对齐单个图像,您可以使用:

    .img-container {
      display: grid;
    }
    
    img { 
      align-self: center;
    }
    

    .img-container {
      display: grid;
      grid-auto-flow: column; 
      background: #BADA55;
      width: 1200px;
      height: 500px;
    }
    
    img.vertical-align {
      align-self: center;
    }
    <div class="img-container">
      <img src="https://picsum.photos/300/300" />
      <img class="vertical-align" src="https://picsum.photos/300/300" />
      <img src="https://picsum.photos/300/300" />
    </div>

    如果你想在一个图像容器中对齐多个图像,你可以使用这个:

    .img-container {
      display: grid;
      align-items: center;
    }
    

    .img-container {
      display: grid;
      grid-auto-flow: column;
      align-items: center;
      background: #BADA55;
      width: 1200px;
      height: 500px;
    }
    <div class="img-container">
      <img src="https://picsum.photos/300/300" />
      <img src="https://picsum.photos/300/300" />
      <img src="https://picsum.photos/300/300" />
    </div>

    请注意,我在这两种情况下都使用了grid-auto-flow: column,否则元素会在指定显式网格项的情况下换行。在问题代码中,我也看到该项目水平居中。在这种情况下,只需使用place-items: center 而不是align-items: center

    【讨论】:

      【解决方案3】:

      据我所知,唯一(也是最好的跨浏览器)方法是在两个元素上使用带有 height: 100%vertical-align: middleinline-block 助手。

      所以有一个解决办法:http://jsfiddle.net/kizu/4RPFa/4570/

      .frame {
          height: 25px;      /* Equals maximum image height */
          width: 160px;
          border: 1px solid red;
          white-space: nowrap; /* This is required unless you put the helper span closely near the img */
      
          text-align: center;
          margin: 1em 0;
      }
      
      .helper {
          display: inline-block;
          height: 100%;
          vertical-align: middle;
      }
      
      img {
          background: #3A6F9A;
          vertical-align: middle;
          max-height: 25px;
          max-width: 160px;
      }
      <div class="frame">
          <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=250px />
      </div>
      <div class="frame">
          <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=25px />
      </div>
      <div class="frame">
          <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=23px />
      </div>
      <div class="frame">
          <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=21px />
      </div>
      <div class="frame">
          <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=19px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=17px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=15px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=13px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=11px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=9px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=7px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=5px />
      </div>
      <div class="frame">
          <span class="helper"></span>
          <img src="http://jsfiddle.net/img/logo.png" height=3px />
      </div>

      或者,如果您不想在现代浏览器中添加额外元素并且不介意使用 Internet Explorer 表达式,则可以使用伪元素并使用方便的表达式将其添加到 Internet Explorer,该表达式仅运行每个元素一次,因此不会有任何性能问题:

      :beforeexpression() 用于 Internet Explorer 的解决方案:http://jsfiddle.net/kizu/4RPFa/4571/

      .frame {
          height: 25px;      /* Equals maximum image height */
          width: 160px;
          border: 1px solid red;
          white-space: nowrap;
      
          text-align: center;
          margin: 1em 0;
      }
      
      .frame:before,
      .frame_before {
          content: "";
          display: inline-block;
          height: 100%;
          vertical-align: middle;
      }
      
      img {
          background: #3A6F9A;
          vertical-align: middle;
          max-height: 25px;
          max-width: 160px;
      }
      
      /* Move this to conditional comments */
      .frame {
          list-style:none;
          behavior: expression(
              function(t){
                  t.insertAdjacentHTML('afterBegin','<span class="frame_before"></span>');
                  t.runtimeStyle.behavior = 'none';
              }(this)
          );
      }
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=250px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=25px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=23px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=21px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=19px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=17px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=15px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=13px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=11px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=9px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=7px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=5px /></div>
      <div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=3px /></div>

      它是如何工作的:

      1. 当您有两个彼此靠近的inline-block 元素时,您可以彼此对齐,因此使用vertical-align: middle 您将得到如下结果:

      2. 当你有一个固定高度的块(pxem或其他绝对单位)时,你可以在%中设置内部块的高度。

      3. 因此,在具有固定高度的块中添加一个 inline-blockheight: 100% 会使其中的另一个 inline-block 元素(在您的情况下为 &lt;img/&gt;)在它附近垂直对齐。

      【讨论】:

      • 请注意:&lt;img src=""/&gt; 不在添加的 &lt;span&gt;&lt;/span&gt; 助手内。它在外面。我几乎没有意识到这一点,把我的每一根头发都扯掉了。
      • 看来你还需要添加“white-space: nowrap;”到框架,否则如果您的图像和助手跨度之间有换行符,您将遇到问题。我花了一个小时才发现这个解决方案对我不起作用。
      • 我不确定这是不是真的。我对没有.helperimg 应用了相同的方法,并且效果很好。只需将imginline-block 设置为垂直对齐即可。
      • 请注意,这仅适用于 img 的高度低于框架的情况。如果 img 高于 frame,则它不与中间对齐。
      【解决方案4】:

      这可能有用:

      div {
          position: relative;
          width: 200px;
          height: 200px;
      }
      img {
          position: absolute;
          top: 0;
          bottom: 0;
          margin: auto;
      }
      .image {
          min-height: 50px
      }
      

      【讨论】:

      • 如果您还希望水平对齐图像,请添加 left:0;右:0;到img
      • 如果你事先不知道图片的大小,那就复制一张图片:.img1 { position: absolute; top: 0; bottom: 0; margin: auto; } .img2 { visibility: hidden; }
      【解决方案5】:

      一个 CSS 解决方案:

      .frame {
        margin: 1em 0;
        height: 35px;
        width: 160px;
        border: 1px solid red;
        position: relative;
      }
      
      img {
        max-height: 25px;
        max-width: 160px;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        background: #3A6F9A;
      }
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=250 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=25 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=23 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=21 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=19 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=17 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=15 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=13 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=11 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=9 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=7 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=5 />
      </div>
      <div class=frame>
        <img src="http://jsfiddle.net/img/logo.png" height=3 />
      </div>

      关键的东西

      // position: relative; - in .frame holds the absolute element within the frame
      // top: 0; bottom: 0; left: 0; right: 0; - this is key for centering a component
      // margin: auto; - centers the image horizontally & vertically
      

      【讨论】:

        【解决方案6】:

        想象一下你有

        <div class="wrap">
            <img src="#">
        </div>
        

        还有css:

        .wrap {
            display: flex;
        }
        .wrap img {
            object-fit: contain;
        }
        

        【讨论】:

          【解决方案7】:

          只是为了这是一个有效的答案,我仍然想再次发布 jQuery 解决方案。这适用于每个应用了v_align 类的元素。它将在父级中垂直居中,并且在调整窗口大小时将重新计算。

          Link to the JSFiddle

          $(document).ready(function() {
            // Define the class that vertically aligns stuff
            var objects = '.v_align';
            // initial setup
            verticalAlign(objects);
          
            // Register resize event listener
            $(window).resize(function() {
              verticalAlign(objects);
            });
          });
          
          function verticalAlign(selector) {
            $(selector).each(function(val) {
              var parent_height = $(this).parent().height();
              var dif = parent_height - $(this).height()
              // Should only work if the parent is larger than the element
              var margin_top = dif > 0 ? dif/2 : 0;
              $(this).css("margin-top", margin_top + "px");
            });
          }
          

          【讨论】:

          • 太棒了!有时我们依赖代码而不是 CSS!
          【解决方案8】:

          我一直在寻找类似的答案,并且一些建议将图像向左对齐或垂直比例压扁了图像,所以我想出了这个解决方案......它将内容垂直和水平居中。

           .div{
              position: relative;
              overflow: hidden;
           }
           .bantop img {
              position: absolute;
              margin: auto;
              width: 103%;
              height: auto;
              top: -50%;
              bottom: -50%;
              left: -50%;
              right: -50%;
            }
          

          我在几个项目中使用它,它就像一个魅力。

          【讨论】:

            【解决方案9】:

            你可以用这个:

             .loaderimage {
                position: absolute;
                top: 50%;
                left: 50%;
                width: 60px;
                height: 60px;
                margin-top: -30px; /* 50% of the height */
                margin-left: -30px;
             }
            

            【讨论】:

              【解决方案10】:

              另外,您可以使用 Flexbox 来获得正确的结果:

              .parent {
                align-items: center; /* For vertical align */
                background: red;
                display: flex;
                height: 250px;
                /* justify-content: center; <- for horizontal align */
                width: 250px;
              }
              <div class="parent">
                <img class="child" src="https://cdn2.iconfinder.com/data/icons/social-icons-circular-black/512/stackoverflow-128.png" />
              </div>

              【讨论】:

                【解决方案11】:

                这段代码很适合我。

                <style>
                    .listing_container{width:300px; height:300px;font: 0/0 a;}
                    .listing_container:before { content: ' ';display: inline-block;vertical-align: bottom;height: 100%;}
                    .listing_container img{ display: inline-block; vertical-align: middle;font: 16px/1 Arial sans-serif; max-height:100%; max-width:100%;}
                <style>
                
                <div class="listing_container">
                    <img src="http://www.advancewebsites.com/img/theme1/logo.png">
                </div>
                

                【讨论】:

                  【解决方案12】:

                  除了像这样的一些文本之外,用于在容器中居中图像(可能是徽标):

                  基本上你包装图像

                  .outer-frame {
                    border: 1px solid red;
                    min-height: 200px;
                    text-align: center; /* Only to align horizontally */
                  }
                  
                  .wrapper{
                    line-height: 200px;
                    border: 2px dashed blue;
                    border-radius: 20px;
                    margin: 50px
                  }
                  
                  img {
                    /* height: auto; */
                    vertical-align: middle;   /* Only to align vertically */
                  }
                  <div class="outer-frame">
                    <div class="wrapper">
                      some text
                      <img src="http://via.placeholder.com/150x150">
                    </div>
                  </div>

                  【讨论】:

                    【解决方案13】:

                    想要对齐在文本/标题之后且都在 div 内的图像?

                    查看JSfiddle 或运行代码片段。

                    请确保您的所有元素(div、img、title 等)都有一个 ID 或一个类。

                    对我来说,此解决方案适用于所有浏览器(对于移动设备,您必须使用以下代码调整代码:@media)。

                    h2.h2red {
                        color: red;
                        font-size: 14px;
                    }
                    .mydivclass {
                        margin-top: 30px;
                        display: block;
                    }
                    img.mydesiredclass {
                        margin-right: 10px;
                        display: block;
                        float: left; /* If you want to allign the text with an image on the same row */
                        width: 100px;
                        heght: 100px;
                        margin-top: -40px /* Change this value to adapt to your page */;
                    }
                    <div class="mydivclass">
                        <br />
                        <img class="mydesiredclass" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b3/Wikipedia-logo-v2-en.svg/2000px-Wikipedia-logo-v2-en.svg.png">
                        <h2 class="h2red">Text aligned after image inside a div by negative manipulate the img position</h2>
                    </div>

                    【讨论】:

                      【解决方案14】:

                      使用这个:

                      position: absolute;
                      top: calc(50% - 0.5em);
                      left: calc(50% - 0.5em);
                      line-height: 1em;
                      

                      你可以改变font-size

                      【讨论】:

                        【解决方案15】:

                        使用表格和表格单元格的解决方案

                        有时应该通过显示为表格/表格单元格来解决。例如,快速标题屏幕。这也是 W3 推荐的方式。我建议您查看来自 W3C.org 的名为 Centering a block or image 的链接。

                        这里使用的提示是:

                        • 绝对定位容器显示为表格
                        • 垂直对齐以显示为表格单元格的中心内容

                        .container {
                            position: absolute;
                            display: table;
                            width: 100%;
                            height: 100%;
                        }
                        .content {
                            display: table-cell;
                            vertical-align: middle;
                        }
                        <div class="container">
                          <div class="content">
                            <h1 style="text-align:center">Peace in the world</h1>
                         </div>
                        </div>

                        我个人实际上不同意为此目的使用助手。

                        【讨论】:

                          【解决方案16】:

                          你可以试试下面的代码:

                          .frame{
                              display: flex;
                              justify-content: center;
                              align-items: center;
                              width: 100%;
                          }
                          <div class="frame" style="height: 25px;">
                              <img src="http://jsfiddle.net/img/logo.png" />
                          </div>

                          【讨论】:

                            【解决方案17】:

                            一种对我有用的简单方法:

                            img {
                                vertical-align: middle;
                                display: inline-block;
                                position: relative;
                            }
                            

                            它非常适用于谷歌浏览器。在不同的浏览器中试试这个。

                            【讨论】:

                              【解决方案18】:

                              三行解决方案:

                              position: relative;
                              top: 50%;
                              transform: translateY(-50%);
                              

                              这适用于任何事情。

                              来自here

                              【讨论】:

                              【解决方案19】:

                              最好的解决办法是

                              .block{
                                  /* Decor */
                                  padding:0 20px;
                                  background: #666;
                                  border: 2px solid #fff;
                                  text-align: center;
                                  /* Important */
                                  min-height: 220px;
                                  width: 260px;
                                  white-space: nowrap;
                              }
                              .block:after{
                                  content: '';
                                  display: inline-block;
                                  height: 220px; /* The same as min-height */
                                  width: 1px;
                                  overflow: hidden;
                                  margin: 0 0 0 -5px;
                                  vertical-align: middle;
                              }
                              .block span{
                                  vertical-align: middle;
                                  display: inline-block;
                                  white-space: normal;
                              }
                              

                              【讨论】:

                                【解决方案20】:

                                我不确定 Internet Explorer,但在 Firefox 和 Chrome 下,如果您在 div 容器中有 img,则以下 CSS 内容应该可以工作。至少对我来说效果很好:

                                div.img-container {
                                    display: table-cell;
                                    vertical-align: middle;
                                    height: 450px;
                                    width: 490px;
                                }
                                
                                div.img-container img {
                                    max-height: 450px;
                                    max-width: 490px;
                                }
                                

                                【讨论】:

                                • IE8+,很遗憾。对我来说很好,但可能不适合其他人。
                                • 还要注意 display:table-cell; 不接受 % 作为宽度
                                【解决方案21】:

                                这样您可以将图像垂直居中 (demo):

                                div{
                                  height: 150px; // Internet Explorer 7 fix
                                  line-height: 150px;
                                }
                                img{
                                  vertical-align: middle;
                                  margin-bottom: 0.25em;
                                }
                                

                                【讨论】:

                                  【解决方案22】:

                                  ma​​tejkramny 的解决方案是一个好的开始,但过大的图片比例错误。

                                  这是我的叉子:

                                  演示:https://jsbin.com/lidebapomi/edit?html,css,output


                                  HTML:

                                  <div class="frame">
                                    <img src="foo"/>
                                  </div>
                                  

                                  CSS:

                                  .frame {
                                      height: 160px; /* Can be anything */
                                      width: 160px; /* Can be anything */
                                      position: relative;
                                  }
                                  img {
                                      max-height: 100%;
                                      max-width: 100%;
                                      width: auto;
                                      height: auto;
                                      position: absolute;
                                      top: 0;
                                      bottom: 0;
                                      left: 0;
                                      right: 0;
                                      margin: auto;
                                  }
                                  

                                  【讨论】:

                                  • 我试过了,效果很好,谢谢
                                  【解决方案23】:

                                  我遇到了同样的问题。这对我有用:

                                  <style type="text/css">
                                      div.parent {
                                          position: relative;
                                      }
                                  
                                      img.child {
                                          bottom: 0;
                                          left: 0;
                                          margin: auto;
                                          position: absolute;
                                          right: 0;
                                          top: 0;
                                      }
                                  </style>
                                  
                                  <div class="parent">
                                      <img class="child">
                                  </div>
                                  

                                  【讨论】:

                                  • 实际上图像上的“位置:固定”对我有用.使用 algreat 建议的方法,您也不需要额外的容器。
                                  【解决方案24】:

                                  如果您可以忍受像素大小的边距,只需将font-size: 1px; 添加到.frame。但请记住,现在.frame 1em = 1px,这意味着,您还需要以像素为单位设置边距。

                                  http://jsfiddle.net/feeela/4RPFa/96/

                                  现在它不再以 Opera 为中心了……

                                  【讨论】:

                                    【解决方案25】:

                                    用纯 CSS 试试这个解决方案http://jsfiddle.net/sandeep/4RPFa/72/

                                    也许这是您的 HTML 的主要问题。当您在 HTML 中定义 classimage height 时,您没有使用引号。

                                    CSS:

                                    .frame {
                                        height: 25px;      /* Equals maximum image height */
                                        width: 160px;
                                        border: 1px solid red;
                                        position: relative;
                                        margin: 1em 0;
                                        top: 50%;
                                        text-align: center;
                                        line-height: 24px;
                                        margin-bottom: 20px;
                                    }
                                    
                                    img {
                                        background: #3A6F9A;
                                        vertical-align: middle;
                                        line-height: 0;
                                        margin: 0 auto;
                                        max-height: 25px;
                                    }
                                    

                                    当我使用 img 标记时,它会从 top 留下 3 像素到 2 像素的空间。现在我减少line-height,它正在工作。

                                    CSS:

                                        .frame {
                                            height: 25px;      /* Equals maximum image height */
                                            width: 160px;
                                            border: 1px solid red;
                                            margin: 1em 0;
                                            text-align: center;
                                            line-height: 22px;
                                            *:first-child+html line-height:24px; /* For Internet Explorer 7 */
                                        }
                                    
                                        img {
                                            background: #3A6F9A;
                                            vertical-align: middle;
                                            line-height: 0;    
                                            max-height: 25px;
                                            max-width: 160px;
                                        }
                                    @media screen and (-webkit-min-device-pixel-ratio:0) {
                                        .frame {
                                            line-height:20px; /* WebKit browsers */
                                        }
                                    

                                    line-height 属性在不同浏览器中的呈现方式不同。所以,我们必须定义不同的line-height属性浏览器。

                                    检查这个例子:http://jsfiddle.net/sandeep/4be8t/11/

                                    查看这个关于line-height在不同浏览器中不同的例子:input height differences in Firefox and Chrome

                                    【讨论】:

                                    • 不起作用(至少在 Firefox 中)。对于缺少的引号,这在 HTML5 中是允许的(虽然不知道 jsfiddle 中使用了哪种文档类型)
                                    【解决方案26】:

                                    http://jsfiddle.net/MBs64/

                                    .frame {
                                        height: 35px;      /* Equals maximum image height */
                                        width: 160px;
                                        border: 1px solid red;
                                        text-align: center;
                                        margin: 1em 0;
                                        display: table-cell;
                                        vertical-align: middle;
                                    }
                                    img {
                                        background: #3A6F9A;
                                        display: block;
                                        max-height: 35px;
                                        max-width: 160px;
                                    }
                                    

                                    .frame 的键属性是 display: table-cell;Div.frame 与 this 显示为内联,因此您需要将其包装在块元素中。

                                    这适用于 Firefox、Opera、Chrome、Safari 和 Internet Explorer 8(及更高版本)。

                                    更新

                                    对于 Internet Explorer 7,我们需要添加一个 CSS 表达式:

                                    *:first-child+html img {
                                        position: relative;
                                        top: expression((this.parentNode.clientHeight-this.clientHeight)/2+"px");
                                    }
                                    

                                    【讨论】:

                                      【解决方案27】:

                                      你可以这样做:

                                      演示

                                      http://jsfiddle.net/DZ8vW/1

                                      CSS

                                      .frame {
                                          height: 25px;      /* Equals maximum image height */
                                          line-height: 25px;
                                          width: 160px;
                                          border: 1px solid red;
                                          
                                          text-align: center; 
                                          margin: 1em 0;
                                          position: relative; /* Changes here... */
                                      }
                                      img {
                                          background: #3A6F9A;
                                          max-height: 25px;
                                          max-width: 160px;
                                          top: 50%;           /* Here.. */
                                          left: 50%;          /* Here... */
                                          position: absolute; /* And here */
                                      }    
                                      

                                      JavaScript

                                      $("img").each(function(){
                                          this.style.marginTop = $(this).height() / -2 + "px";
                                      })
                                      

                                      【讨论】:

                                      • 不错。纯 CSS 解决方案的任何机会?
                                      • 很遗憾没有,除非您想使用表格或放弃与旧版本 IE 的兼容性。 :( 如果您事先知道图像的确切大小,那会有所不同,但并非没有这一点,单独使用 CSS 是不可能的。
                                      • @Joseph 你会如何用表格来解决它?
                                      • @Benjamin Udink 十个 Cate 这很乱,但可以这样做:jsfiddle.net/DZ8vW/2(不建议这样做,但应该可以)
                                      • 它非常适合 arnauds 的需求。没有 JS,只有 CSS 和 HTML。
                                      【解决方案28】:

                                      背景图片解决方案

                                      我完全删除了图像元素并将其设置为具有.frame类的div的背景

                                      http://jsfiddle.net/URVKa/2/

                                      这至少在 Internet Explorer 8、Firefox 6 和 Chrome 13 上运行良好。

                                      我检查过,此解决方案无法缩小大于 25 像素高度的图像。有一个名为 background-size 的属性确实设置了元素的大小,但它是 CSS 3,它会与 Internet Explorer 7 的要求相冲突。

                                      如果您想使用此解决方案,我建议您重做浏览器优先级并设计最佳可用浏览器,或者获取一些服务器端代码来调整图像大小。

                                      【讨论】:

                                        【解决方案29】:

                                        您可以使用grid 布局将图像垂直居中。

                                        .frame {
                                          display: grid;
                                          grid-template-areas: "."
                                                               "img"
                                                               "."
                                          grid-template-rows: 1fr auto 1fr;
                                          grid-template-columns: auto;
                                        }
                                        .frame img {
                                          grid-area: img;
                                        }
                                        

                                        这将创建一个 3 行 1 列的网格布局,在顶部和底部有一个未命名的 1 分数宽的间隔,以及一个名为 img 且高度为 auto 的区域(内容大小)。

                                        img 将使用它喜欢的空间,顶部和底部的间隔将使用剩余的高度分成两个相等的分数,从而使 img 元素垂直居中。

                                        http://jsfiddle.net/Kissaki0/4RPFa/20713/

                                        【讨论】:

                                          【解决方案30】:

                                          使用tabletable-cell 方法完成这项工作,特别是因为您也针对一些旧浏览器,我为您创建了一个sn-p,您可以运行它并检查结果:

                                          .wrapper {
                                            position: relative;
                                            display: table;
                                            width: 300px;
                                            height: 200px;
                                          }
                                          
                                          .inside {
                                            vertical-align: middle;
                                            display: table-cell;
                                          }
                                          <div class="wrapper">
                                            <div class="inside">
                                              <p>Centre me please!!!</p>
                                            </div>
                                            <div class="inside">
                                              <img src="https://cdn2.iconfinder.com/data/icons/social-icons-circular-black/512/stackoverflow-128.png" />
                                            </div>
                                          </div> 

                                          【讨论】:

                                            猜你喜欢
                                            • 2011-07-22
                                            • 1970-01-01
                                            • 2013-03-06
                                            • 2019-07-14
                                            • 2012-04-02
                                            • 2023-04-04
                                            • 2012-10-21
                                            • 1970-01-01
                                            相关资源
                                            最近更新 更多