【问题标题】:Vertically align inline object without height or width垂直对齐没有高度或宽度的内联对象
【发布时间】:2013-07-25 22:26:58
【问题描述】:

给定以下 html:

<div class="body">    
    <div class="banner">
        <div class="name">
            <h2>
                <a href="http://www.example.com">
                    <span class="bold">Test Link</span><br/>
                </a>
            </h2>
        </div>
        <div class="title">
            <h3>A Connections Learning Partner Program</h3>
            <p>Quality online learning for high school students in Oakland County and surrounding counties.
            </p>
        </div>
        <div class="link">
            <a href="http://www.example.com">Learn More</a>
        </div>
    </div>
</div>

如何在.link 中垂直对齐.link a(按钮)而不给出高度或宽度?像这样……

这是我的fiddle

【问题讨论】:

  • 我更新了答案以允许更多的灵活性和对各种元素的控制。请查阅。 :)

标签: css alignment vertical-alignment


【解决方案1】:

这是您可以做到的一种方法。您的 HTML 很好,无需更改任何内容。

对于 CSS:

.body { width: 920px; }

.banner {
    background-color: #454545;
    border-bottom: 3px solid #F9F9F9;
    height: 100px;
    margin: 0 0 5px;
    padding: 0;
    display: table;
}

.banner > div {
    outline: 1px dotted yellow; /* optional to show cell edges... */
    display: table-cell;
}

.banner .name {
    width: 25%;
    vertical-align: top;
    padding-top: 25px; /* control top white space */
    text-align: center;
}

.banner .name h2 {
    color: #F9F9F9;
    max-height: 55px;
    text-transform: uppercase;
    margin: 0;
    padding: 0;
}

.banner .title {
    width: 50%;
    vertical-align: top;
    padding-top: 25px;
}

.banner .title h3 {
    font-size: 15px;
    font-weight: bold;
    line-height: 15px;
    margin: 0px 0 0 0;
    padding: 0;
}

.banner .title p {
    font-size: 12px;
    max-height: 35px;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

.banner .link {
    width: 25%;
    vertical-align: middle;
    text-align: left; /* set to left, center or right as needed */
}

.banner .link a {
    margin-left: 25px; /* controls left offset */
    background-color: #FA9800;
    border-radius: 5px 5px 5px 5px;
    cursor: pointer;
    display: inline-block; /* use inline-block if you want to center element */
    font-size: 12px;
    font-weight: bold;
    height: 23px;
    line-height: 23px;
    text-align: center;
    text-decoration: none;
    width: 100px;
}

请参阅小提琴:http://jsfiddle.net/audetwebdesign/jsG8F/

这是如何工作的

诀窍是在您的.banner 容器上使用display: table,然后在您的子div 元素上使用display: table-cell,并将.name 的% 宽度分别设置为25%、50%、25%, .title, .link.

然后您可以使用vertical-aligntext-align 来控制各种文本块的垂直和水平位置。

我添加了与使用 padding-top 控制横幅顶部的空白相关的 cmets。

对于.link a元素,您可以根据需要调整左边距(或右边距)。

这些 CSS 规则让您可以很好地控制横幅中各种元素的位置。

向后兼容性

display: table-cell 属性向后兼容回 IE8。

参考:https://developer.mozilla.org/en-US/docs/Web/CSS/display

【讨论】:

  • 对不起,你将如何水平居中?
  • display: table-cell 不适用于旧版 IE 浏览器。在这种情况下,您会松开垂直居中。至于水平居中,链接会居中的空间是多少?
  • 由于div.title 是一个固定宽度,我希望它水平居中在它和父div div.banner 的末尾之间。上图在一定程度上显示了它应该是什么样子,尽管它并没有完全位于我上面提到的两点之间。
  • 你想让.name.title.link一样垂直居中吗?
【解决方案2】:

如果元素和banner的大小是固定的,使用margin-top来偏移元素。

【讨论】:

    【解决方案3】:

    Marc Audet 非常接近,但我最终选择了一条略有不同的路线。

    我给.link a 设置了一个固定的上边距并设置了margin-left: auto;margin-right: auto;,这样就成功了。

    这里是fiddle 供参考。

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2013-02-25
      • 1970-01-01
      • 2015-09-01
      • 2015-03-22
      • 2014-07-21
      • 2012-11-04
      • 1970-01-01
      • 2015-06-01
      相关资源
      最近更新 更多