html - 导航显示 :table cell where first and last child is a different width

标签 html css css-tables

我有一个“步骤”导航,它使用表格/表格单元格来确保每个元素的宽度相同并占据整个菜单容器。但由于它的设计方式,我希望第一个和最后一个 child 的内容与 parent 的边缘对齐。有任何想法吗?也许我以错误的方式接近它......

用代码更容易解​​释:

http://jsfiddle.net/davidpauljunior/u499gyvd/

enter image description here

HTML:

<ul>
    <li>
        <span class="title">Item 1</span>
        <span class="fa fa-automobile"></span>
    </li>
    <li>
        <span class="title">Item 1</span>
        <span class="fa fa-automobile"></span>
    </li>
    <li>
        <span class="title">Item 1</span>
        <span class="fa fa-automobile"></span>
    </li>
    <li>
        <span class="title">Item 1</span>
        <span class="fa fa-automobile"></span>
    </li>
</ul>

CSS:

body {
    margin: 0;
}

ul {
    display: table;
    table-layout: fixed;
    width: 100%;
    margin: 0;
    padding: 0;
    list-style: none;
}

li {
    display: table-cell;
    width: 100%;
    text-align: center;
    position: relative;
}

li:after {
    content: '';
    display: block;
    width: 100%;
    height: 2px;
    background-color: gray;
    position: relative;
    top: -10px;
}

li:first-child:after {
    width: 50%;
    left: 50%;
}

li:last-child:after {
    width: 50%;
}

.title {
    display: block;
    margin-bottom: 10px;
}

.fa {
    position: relative;
    z-index: 1;
}

.fa:before {
    padding: 4px;
    background: white;
    border: 1px solid gray;
    border-radius: 50%;
}

编辑:

我基本上追求的是第一个和最后一个元素具有 width: auto(因此它们仅与实际内容一样宽),然后是介于两者之间的元素(例如 2, 3, 4) 将剩余的空间平分。

最佳答案

删除这2个样式;

li:first-child:after {
    left: 50%;
    width: 50%;
}
li:last-child:after {
    width: 50%;
}

如果您需要第一项和最后一项接触 ul 的左侧和右侧,则不需要它们。

然后您需要将第一项设置为 text-align: left;和文本对齐的正确元素:正确; (而不是像现在这样居中对齐)

关于html - 导航显示 :table cell where first and last child is a different width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25312597/

相关文章:

javascript - 将类设置为在表行中处于事件状态

css - 添加边距以显示 : table-cell child element

css - td 边框在 IE8 中隐藏在 tr 背景颜色后面

javascript - CKEditor(5) - 如何删除选择表小部件?

html - max-width 属性不适用于 flex 元素的子项

javascript - 使用简单数组的 ng-options 将索引放入模型

jquery - 在使 div 可见之前如何知道它的高度?

html - 悬停后如何继续CSS动画?

javascript - 更新输入数字变量 onclick

background - 在 :hover element 上设置背景的高度和宽度