【问题标题】:Align an image center, bottom of a responsive div对齐图像中心,响应式 div 的底部
【发布时间】:2015-04-05 07:39:58
【问题描述】:
<div id="banner" class="cf">
        <div class="banner container">
            <hr/>
            <p class="banner-text">
            Some text here
            <span class="sub-bt">That's it.</span>
            </p>
            <a href="#" rel="nofollow" class="learn-btn">Learn More</a>

            <img src="<?php echo get_template_directory_uri(); ?>/library/images/responsive_ex.png" class="res-ex">
        </div>
    </div>

到目前为止,这是问题的代码。我需要将带有“res-ex”类的图像定位到“banner”div类的底部。

我尝试了多个表格单元格和位置代码,但似乎无法正常工作。

设计也是响应式的。

【问题讨论】:

    标签: html css alignment center


    【解决方案1】:

    您可以使用定位将图像放置在 div 的底部,然后使用 CSS 变换使其居中。

    .wrap {
        height: 400px;
        position: relative;
        background: #f09d09;
    }
    
    .wrap img{
        display: block;
        max-width: 100%;
        position: absolute;
        bottom:0;
        left:50%;
        transform:translateX(-50%);
        
    }
    <div class="wrap">
        <img src="http://lorempixel.com/400/200/" alt=""/>
    </div>

    JSFiddle Demo

    【讨论】:

    • 你如何让这个垂直居中?
    • 魔法就在这里:left:50%; transform:translateX(-50%); 你能解释一下吗?尤其是变换。
    【解决方案2】:

    代码如下:

    .res-ex {
        position:absolute;
        min-width:100px;
        min-height:100px;
        bottom:0;
        display:inline-block;
    }
    .banner.container {
        position:relative;
    }
    

    您需要为图像设置position:absolutedisplay:inline-block(或display:block),并为其容器设置position:relative。 然后为图片设置bottom:0

    【讨论】:

    • 我想这会奏效,但 Paulie_D 的方法是我首先实现的。不过谢谢。
    • 如何居中?
    • 从帖子的描述中,我了解到 Spencer 想将 div 定位在容器的底部。如果它需要居中,那么这就是解决方案:jsfiddle.net/2ax92smb
    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 2013-08-31
    • 1970-01-01
    • 2015-11-12
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多