垂直和水平对齐元素,同时左右浮动

发布于 2024-12-24 23:24:31 字数 1019 浏览 0 评论 0 原文

我想将四个链接集中在一个 div 中。

这就是我到目前为止所做的: jsfiddle

Html:

<div id="menu">
    <section>
        <a class="top" href="#">Top</a>
        <a class="left" href="#">Left</a>
        <a class="right" href="#">Right</a>
        <a class="bottom" href="#">Bottom</a>
    </section>
</div>

Css:

#menu {
    width: 200px;
    height: 200px;
    border: 1px solid #000;
    background: #eee;
    position: relative;
    padding: 10px;
}

#menu>section {
    position: absolute;
    text-align: center;
    width: 200px;
}

#menu a {
    display: block;
    vertical-align: middle;
    height: 20px;
}

#menu .left {
    float: left;
    height: 160px;
}

#menu .right {
    float: right;
}

#menu .bottom {
    clear: both;
}

问题是浮动元素不垂直正如他们应该的那样居中。我希望 leftright 元素位于中间而不是顶部。

I want to center four links in a div.

This is what I did so far: jsfiddle

Html:

<div id="menu">
    <section>
        <a class="top" href="#">Top</a>
        <a class="left" href="#">Left</a>
        <a class="right" href="#">Right</a>
        <a class="bottom" href="#">Bottom</a>
    </section>
</div>

Css:

#menu {
    width: 200px;
    height: 200px;
    border: 1px solid #000;
    background: #eee;
    position: relative;
    padding: 10px;
}

#menu>section {
    position: absolute;
    text-align: center;
    width: 200px;
}

#menu a {
    display: block;
    vertical-align: middle;
    height: 20px;
}

#menu .left {
    float: left;
    height: 160px;
}

#menu .right {
    float: right;
}

#menu .bottom {
    clear: both;
}

The problem is that the floated elements do not vertically centered as they should. I want the left and right elements to be in the middle and not at the top.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉殇 2024-12-31 23:24:31

也许您可以使用 line-height 属性来实现此目的。像这样:

#menu .left, #menu .right {
    height: 160px;
    line-height:160px;
}

http://jsfiddle.net/YdPzP/13/

May be you can use line-height property for this. Like this:

#menu .left, #menu .right {
    height: 160px;
    line-height:160px;
}

http://jsfiddle.net/YdPzP/13/

烛影斜 2024-12-31 23:24:31

尝试添加

 .left, .right { line-height: 160px; }

try adding

 .left, .right { line-height: 160px; }
不喜欢何必死缠烂打 2024-12-31 23:24:31

由于您已经在使用 html5,我想说 aside 标记可能会在您的情况下派上用场:

这里是一个示例

演示

Since you are already using html5, I would say aside tag would probably come in handy in your case:

here is a Sample

DEMO

栖迟 2024-12-31 23:24:31

您可以使用 padding 垂直对齐链接,

<div id="menu">
    <section>
        <a class="top" href="#">Top</a>
        <div class="middle">
        <a class="left" href="#">Left</a>
        <a class="right" href="#">Right</a>
        </div>
        <a class="bottom" href="#">Bottom</a>
    </section>
</div>

添加以下 css:

#menu div.middle{
    height:90px;
    padding-top:60px;

}

You can use padding for vertically aligning the links

<div id="menu">
    <section>
        <a class="top" href="#">Top</a>
        <div class="middle">
        <a class="left" href="#">Left</a>
        <a class="right" href="#">Right</a>
        </div>
        <a class="bottom" href="#">Bottom</a>
    </section>
</div>

add the below css:

#menu div.middle{
    height:90px;
    padding-top:60px;

}
逆蝶 2024-12-31 23:24:31

我遇到了 float:left;vertical-align: middle; 之间的冲突。

在我的例子中,我希望几个小图像水平排列。其中一些垂直尺寸正确,而另一些则不够高,所以我想将那些垂直尺寸错误的垂直排列。

使用

    /

  • 结构,我保留了 float:left;dispay:inline; 在 FF 和 IE 7,8,9 中没问题,但在 Chrome 和 Safari 中不正确。由于某种原因,最后一张图像转到了下一行,尽管有足够的空间。

完全消除 float: left; 后,vertical-align: middle; 就可以工作了。然后只是添加填充以使它们水平排列。现在适用于所有浏览器。

不确定为什么 float:left;vertical-align:middle; 之间存在冲突,

您可能想尝试消除 float:left;代码>

I ran into a seeming conflict between float:left; and vertical-align: middle;

In my case, I wanted several small images to line up horizontally. Some of them were the right size vertically and the others weren't tall enough, so i wanted to line up just those vertically that were the wrong vertical size.

Using a <ul>/<li> structure, I kept float:left; and dispay:inline; and it was ok in FF and IE 7,8,9, but not correct in Chrome and Safari. For some reason, the last image went to the next line even though there was ample room for it.

After eliminating completely float: left; the vertical-align: middle; then worked. Then it was just adding padding to line them up horizontally. Works now in all browsers.

Not sure why there is a conflict between float:left; and vertical-align:middle;

You may want to play with eliminating your float:left;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文