css - 使用显示 : table 的垂直对齐导航

标签 css navigation vertical-alignment

这是我要实现的目标:

enter image description here

Here's a fiddle显示我的样式(使用 display: table 因为我不想固定每个元素的宽度并且它应该是响应式的)。

我很接近,这是样式(为简洁起见,使用 SASS)及其外观:

.nav--main {
    ul {
        display: table;
        table-layout: fixed;
        width: 100%;
        border-collapse: collapse;
        li {
            display: table-cell;
            border: 1px solid #333;
            text-align: center;
            line-height: 1.2;
            vertical-align: top;
            overflow: hidden;
        }
    }
    a {
        display: inline-block;
        vertical-align: middle;
        width: 100%;
        height: 200%;
        padding: .5em;
        background-color: rgba(105,158,197,1);
        color: #fff;
        text-decoration: none;
    }
}

但我无法让 a 占据整个高度并在中间保持垂直居中。 This fiddle显示了一种有点 hacky 的尝试,但仅通过将 vertical-align 设置为 top

enter image description here

我不想:

  • 设置固定高度
  • 将背景颜色放在 li 而不是 a 上:我讨厌可点击区域没有利用所有可用空间

最佳答案

我不知道,在那种情况下这可能会中断,但一个有点肮脏的技巧可能是这样的:

仅更新了 CSS 部分

ul {
    overflow: hidden;
}

a {
    margin: -5em 0;
    padding: 5.5em 0;
}

演示

Try before buy

关于css - 使用显示 : table 的垂直对齐导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18576984/

相关文章:

html - 沿着带有居中文本的父级拉伸(stretch)显示 flex 或表格内部 div

html - 标题中的垂直对齐导航图标/按钮

css - Z-Index 不适用于表格元素

css - 如何为 DIV 的第三次出现定义异常?

php - 如何在我的网站上禁用 "Airtable"的移动 View

java - 将抽屉导航 Activity 扩展到其他 Activity

javascript - jQuery Mobile,垂直导航条,就像 iphone 联系人上的字母列表

html - div 左侧不需要的空间

jquery - 使用 jQuery 取消 CSS 悬停行为

css - 如何垂直对齐无序 html 列表中的元素?