html - 固定菜单宽度 - 元素分布

标签 html css menu items fixed-width

我有列表菜单

<ul>
<li>item1</li>
<li>item2</li>
<li>longitem3</li>
</ul>

菜单样式

ul{
width:500px;
}
li {
float:left;
}

我想在菜单中自动分配我的元素,这样它们之间会有相同的空间,就像这样

{menu}{item}{space}{item}{space}{item}{menu}

是否可以只使用 html 和 css 而不是 javascript 来做到这一点?谢谢

最佳答案

我会使用一个 css 类而不是影响所有

  • ,像这样:

    <li class="menuItem">
    

    至于CSS:

    .menuItem {
        float: left;
        margin-right: <some number of px>;
    }
    .menuItem:last-child {
        margin-right: 0;
    }
    

    “:last-child”选择器覆盖了之前的定义并移除了最后一个菜单项的右侧空间。

  • 关于html - 固定菜单宽度 - 元素分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800400/

    相关文章:

    css - 为什么这个 CSS 立方体 3d 菜单在我的苹果上运行良好,但在我的电脑上却出错了?

    wpf - 在 WPF 中的菜单中放置分割线

    html - 如何用管道分离链接项

    javascript - 我可以从一开始就隐藏一张 table 吗?

    JavaScript- 在页面中查找文本并跳转到页面中的位置

    css - 是否有任何仅适用于现代浏览器的 CSS 重置库?

    html - 仅使用 CSS 的可能解决方案之前的兄弟选择器

    css - Cordova 文本字体有白色轮廓吗?

    html - CSS : set the height of inner div just by using 'top' and 'bottom' propreties

    css - 使用显示 : inline 添加反射(reflect)在父项高度中的填充