html - 对齐水平菜单 li 元素

标签 html css css-float

我在尝试对齐水平菜单时遇到了真正的麻烦。到目前为止,我的菜单看起来像 enter image description here

我有 2 个居中元素来构成图像中的菜单,您可以看到页面中居中的灰色边框(幻灯片导航类)。现在我正在尝试对菜单做同样的事情

我不得不对 li 宽度进行硬编码,但理想情况下我希望它们自动适应。是否可以在没有 javascript 的情况下将菜单项居中对齐?

我的html

   <nav class="slide-nav">
            <ul class="slider">
                <li class="selected">
                    <div>
                        <span class="heart"></span>
                        <div>
                            Get Started</div>
                    </div>
                </li>
                <li>
                    <div>
                        <span class="price-tag"></span>
                        <div>
                            Get Results</div>
                    </div>
                </li>
                <li>
                    <div>
                        <span class="star"></span>
                        <div>
                            Track & Engage</div>
                    </div>
                </li>
                <li>
                    <div>
                        <span class="people"></span>
                        <div>
                            Features</div>
                    </div>
                </li>
            </ul>
        </nav>

CSS

.slide-nav
{
border-bottom: solid 1px #f2f2f2;
margin: 0 auto;
width: 856px;
}
.slider
{
list-style: none;
height: 38px; 
margin: 0 auto;
width: 722px;
}
.slider li
{
border-bottom: solid 7px transparent;
cursor: pointer;

display: inline-block;
width: 150px;
}
.slider li div 
{
line-height: 31px; 
}
.slider li div div 
{
text-indent: 6px;
}
.slider li.selected > div
{
border-bottom: solid 7px #592970;

}

最佳答案

这是你必须更改的 CSS

.slide-nav
{
border-bottom: solid 1px #f2f2f2;
margin: 0 auto;
width: 856px;
text-align:center;
}
.slider
{
list-style: none;
height: 38px; 
margin: 0;
padding: 0;
}
.slider li
{
border-bottom: solid 7px transparent;
cursor: pointer;
display: inline-block;
padding:0 10px;
}

关于html - 对齐水平菜单 li 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10793821/

相关文章:

html - 无法显示 tinymce

中心的 HTML 对齐图例元素在 Internet Explorer 和 Firefox 中不起作用

css - 带有表格边框的 Chrome css 问题

html - 它是 Chrome 中 em DIV 的 secret 最小尺寸(宽度/高度)吗?

css - 隐藏复选框和单选按钮并使用 CSS

html - 为什么我的文本框在不同的浏览器中定位不同

javascript - 如何在 div 元素中将带有图像的文本垂直居中?

jquery - 带有可滚动主体和导轨固定标题的表格

html - 无法使用 css、html 在正确的位置对齐 div

javascript - 添加类时 CSS 不透明度如何工作?