【问题标题】:How do I vertically center an image with a text block?如何使用文本块垂直居中图像?
【发布时间】:2014-05-09 01:35:39
【问题描述】:

我有一个文本块和图像,您可以看到here(该站点尚未上线)。

我想将右侧的图像居中在文本块的垂直中间。

我目前使用的代码如下。正如您将看到的,我尝试在两个divs 上使用display-inline:block; vertical-align:middle,但它似乎没有达到预期的效果。

    <div class="x-column vc one-half" style="font-size:128%; display-inline:block; vertical-align:middle;">
    <p class="p1">The electronics we pick up are transported to our state-of-the-art recycling facility in Brampton,Ontario. As the trucks are unloaded, each company’s material is independently weighed, allowing us to issue a customized Certificate of Recycling for diverted material. Electronics are then sorted into various grades that are either mechanically shredded or manually dismantled. Shift Recycling is an OES-approved primary processor, meaning that our recycling facility is fully compliant with all environmental regulations. Any hazardous materials such as lead glass are handled in a manner safe for the environment and our employees.</p>
    <p class="p2">At the end of our recycling process, we send our raw commodities such as plastic and metal<em> (where?)</em>. These items are sent to approved downstream refiners who melt them down to create new products including plastic outdoor furniture and metal construction rebar.</p>
</div>
<div class="x-column vc one-half" style="display-inline:block; vertical-align:middle;">
    <img class="x-img center-text x-img-rounded none" style=" margin-left: auto; margin-right: auto; " src="http://click2recycle.ca/wp-content/uploads/2014/05/plantSmall.png" alt="The Recycling Facility">
</div>

编辑

我已经尝试了以下两个建议,但不幸的是,页面现在看起来像 this,图片位于文本列下方。我应该提到我正在使用 WordPress 主题(称为 X)来构建这个站点,我现在开始担心它是问题的一部分。是否有任何我可以提供的更多信息可能会有所帮助?

【问题讨论】:

    标签: css alignment vertical-alignment


    【解决方案1】:

    你使用了错误的选择器名称,应该是

    display: inline-block;
    

    此外,您应该指定两个块的宽度,例如。 G。 40% 和 50%

    PS:我建议您避免在 HTML 中使用内联样式,最好将样式指定到单独的 .css 文件中,如下所示:

    .x-column {
        display: inline-block;
        vertical-align: middle;
        width: 49%;
    }
    
    .x-column img {
        width: 90%;
    }
    

    【讨论】:

    • 我对显示标签进行了更改,但它似乎仍然无法正常工作。我添加了宽度标签作为测试,它可以正常工作。还有其他想法吗?
    • 感谢您的提琴。我试过了,但它不像你那样工作。你能看看我的编辑吗?谢谢!
    • @zgall1 我可以建议你用你自己的替换 x 类:jsfiddle.net/3B7PV
    • 好!我很乐意提供帮助。
    【解决方案2】:

    您需要对显示属性进行快速更正:

    display: inline-block;
    

    这是您想要完成的任务。 http://jsfiddle.net/pZb3Y/1/

    【讨论】:

    • 感谢您的提琴。我试过了,但它不像你那样工作。你能看看我的编辑吗?
    • 我已经把样式内联了,试试我所做的编辑的宽度。 jsfiddle.net/pZb3Y/1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2014-06-25
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2013-10-15
    相关资源
    最近更新 更多