简体   繁体   English

使用CSS在图像上垂直对齐

[英]Vertical align on image with css

How can I vertical align content inside absolute div on image? 如何在图片上的absolute div内垂直对齐内容?

http://jsfiddle.net/uxKpn/ http://jsfiddle.net/uxKpn/

You will have to do this : 您将必须这样做:

FIDDLE 小提琴

you nedd to display .article-comments as table and make an inner wraper displayed as table-cell and set to vertical-align:middle . 您需要将.article-comments显示为table并将一个内部包装显示为table-cell并设置为vertical-align:middle

This solution alows you to vertical-align your content even if it changes (ex: a user posts a comment) 此解决方案使您即使内容发生变化也可以使内容垂直对齐(例如:用户发表评论)

HTML: HTML:

<div class="picture-comments">
    <img class="img-responsive" src="http://www.healthyfoodhouse.com/wp-content/uploads/2012/10/5-healthy-breakfasts-for-fat-burning-eggs.jpg" alt="" title="" />
    <div class="article-comments">
        <div class="wrap">
            <h4>45</h4>
            <h4>comments</h4>
        </div>
    </div>
    <!-- /.comments -->
</div><!-- picture-comments -->

css : css:

.picture-comments {
    position: relative;
}

img {
    width: 100%;
}

.article-comments {
    text-align: center;
    color: #000;
    position: absolute;
    width: 50%;
    height: 100%;
    top: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.5);
    display:table;


    h4 {
        font-family: $body-font;
        font-weight: 600;
    }

    .wrap{
        display:table-cell;
        vertical-align:middle;
    }
}

}

you need to make the picture positioned to absolute and the div to relative. 您需要将图片定位为绝对,将div定位为相对。 relative positioned elements always position themselves to the left top corners of the last absolute positioned element. 相对定位的元素始终将自身定位在最后一个绝对定位的元素的左上角。

eg 例如

img 
{
    position:absolute;
    width: 100%;
}

.article-comments 
{
    position:relative;
}

then you may be able to vertical align your div over the image! 那么您也许可以将div在图像上垂直对齐!

Try to work with display: table-cell and vertical-align: middle; 尝试使用display: table-cellvertical-align: middle; on .picture-comments . .picture-comments

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

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