【问题标题】:Align an Image Centrally within a Div在 Div 中居中对齐图像
【发布时间】:2014-09-05 14:55:38
【问题描述】:

我想在 div 中居中放置一张图片 (fiddle)。因为我希望那个 div 继承另一个漂浮在它旁边的 div 的高度,所以我不得不使用this trick.

因此,here 描述的解决方案似乎不起作用。

要求是不修改其他行为,但代码只要达到的效果相同即可。如有必要,我也愿意接受涉及 javascript 的解决方案。

<div class="container"> 
<div class="logo-div">
    <img class="logo" src="http://bit.ly/1qCKrtJ" />
</div>
<div class="text-div">
    <h4 style="display: inline;">Because Sometimes It Takes a Village</h4><br />
    What about robots the size of tea cups that scoot around on tiny wheels, snapping pictures with miniature cameras and keeping track of where they are in relation to dozens of others?
</div>

.container {
    background: green;
    overflow: hidden;
}

.logo-div {
    background: yellow;
    width: 150px;
    float: left;
    padding-bottom: 1000px;
    margin-bottom: -1000px;
}

.text-div {
    background: blue;
    float: left;
    max-width: 350px;
    padding-bottom: 1000px;
    margin-bottom: -1000px;
}

.logo {
    width: 100px;
}

【问题讨论】:

    标签: javascript html css layout web


    【解决方案1】:

    我已经修改了代码,以便徽标图像可以水平和垂直居中对齐。

    JSFiddle

    HTML 代码:

    <div class="container"> 
        <div class="image-div">
            <div class="logo-div">
                <img class="logo" src="http://bit.ly/1qCKrtJ" />
            </div>
        </div>
        <div class="text-div">
            <h4 style="display: inline;">Because Sometimes It Takes a Village</h4><br />
            What about robots the size of tea cups that scoot around on tiny wheels, snapping pictures with miniature cameras and keeping track of where they are in relation to dozens of others?
        </div>
    </div>
    

    CSS代码:

    .container {
        background: green;
        overflow: hidden;
    }
    
    .logo-div {
        background: #FFFF00;
        display: table-cell;
        height: 100px;
        text-align: center;
        vertical-align: middle;
        width: 150px;
    }
    
    .text-div {
        background: blue;
        float: left;
        max-width: 350px;
    }
    .image-div {
        float: left;
    }
    .logo {
        width: 100px;
    }
    

    如果还有问题,请评论代码,修改jsfiddle。

    问候 D.

    【讨论】:

    • 是的,你是对的,但是对于同样的问题,我添加了一个新的 jquery,New JS Edited 我希望这可以解决问题。但是如果添加了一些动态文本,那么这个jquery代码将在函数中定义,在布局更改时调用......问候D.
    【解决方案2】:

    有两种方法。如果您希望它在中间对齐,您可以将任何组件的 Margin 属性设置为“自动”。当然你可以在 CSS 中设置这个属性,而不是使用 style 标签。

    <img src="http://bit.ly/1qCKrtJ" style="margin:auto;"/>
    

    另一个是使用中心标签 (因为 'margin:auto' 可能不适用于某些浏览器的图像,但它适用于 div 标签。)

    <center>
        <img src="http://bit.ly/1qCKrtJ" alt="Logo">
    </center>
    

    【讨论】:

      【解决方案3】:

      如果您只需要水平居中,请尝试:

      .logo-div {text-align: center;}
      img {margin: 0 auto;}
      

      http://jsfiddle.net/yXNnd/18/


      JS 版本

      使用jQuery(我太懒了:))
      http://jsfiddle.net/yXNnd/25/

      添加这个js

      $(document).ready(function(){
          var img = $('.logo-div img');
          var top = ($('.container').height() / 2) - (img.height() / 2);
          img.css('margin-top', top + 'px');
      });
      

      【讨论】:

      • 如果可能的话,我想要水平和垂直。另一种解决方案是使 div 表现得像表格单元格,但是我失去了右 div 下降到下面的好方面。但在最坏的情况下,我宁愿拥有它也不愿没有垂直居中。
      • 添加了使用 jQuery 的 JS 版本。它所做的一切 - 计算容器和图像的高度,并添加上边距以垂直对齐内容的中间。您还可以使用 Devang 编辑的答案与另外一个 div 和 display: table-cell;
      • @Devang 谢谢大家。一个元问题:我不是专业的网络开发人员,我只是建立我的主页,所以我边走边学。是否有任何严重的理由避免使用 JavaScript/jQuery? (例如 CRONUS 说“我太懒了”无法证明使用 jQuery 的合理性,这到底有什么问题?)
      • 在这种情况下,也许纯 JS 是一个更好的选择,但由于我在我正在使用的所有项目中都使用 jQuery,我什至不记得如何获得元素的高度: D 这就是为什么我懒得去搜索如何使用纯 JS 实现你所需要的 ;)
      猜你喜欢
      • 2011-06-20
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 2023-03-14
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多