jquery - 将 border 属性设置为 li - html 中的标记会将最后一个 li 元素降到下一行

标签 jquery html css html-lists border

我要求 html 标题页中的导航项具有虚线边框。我在 css 文件中设置了 border 属性。我得到了 li 元素的虚线边框,但问题是最后一个 li 元素出现在下一行。

这是我的导航栏 html 代码:

<div class="navbardiv"> <ul class="topnavbarlist"> <li> <a>home</a> </li> <li> <a>store</a> </li> <li> <a>gallery</a> </li> <li> <a>about us</a> </li> <li> <a>sign in</a> </li> </ul> </div>

下面是我的 CSS:

.topnavbarlist
{
 list-style: none;
display:inline-block;
width:100%;
height:100%;
background:#cdeb8e;
list-style-type: none;
}
.topnavbarlist li{
float:left;
width:20%;
height:100%;
display:table-cell;
text-align: center;
list-style-type: none;
border: 1px dashed rgba(84,84,84,0.8);
}

最佳答案

这里的问题是 box-model , 默认情况下,宽度分配给内容框,因此 20% 宽度用于内容,然后边框在内容框之外,这将导致宽度超过 100%。

您可以使用 box-sizing 控制此行为css 属性。

.topnavbarlist {
    list-style: none;
    display:inline-block;
    width:100%;
    height:100%;
    background:#cdeb8e;
    list-style-type: none;
}
.topnavbarlist li {
    float:left;
    width:20%;
    height:100%;
    display:table-cell;
    text-align: center;
    list-style-type: none;
    border: 1px dashed rgba(84, 84, 84, 0.8);
    box-sizing: border-box;
}
<div class="navbardiv">
    <ul class="topnavbarlist">
        <li> <a>home</a> </li>
        <li> <a>store</a> </li>
        <li> <a>gallery</a> </li>
        <li> <a>about us</a> </li>
        <li> <a>sign in</a> </li>
    </ul>
</div>

关于jquery - 将 border 属性设置为 li - html 中的标记会将最后一个 li 元素降到下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27285019/

相关文章:

javascript - 使用 select2 时出错 Uncaught Error : No select2/compat/containerCss

javascript - 想要在弹出窗口中显示日历?

html - uiwebview 也不读取 css 文件或图像

html - 为音乐或视频创建下载链接

html - 将图像置于中心,左右边距最小

html - 在 CSS 中居中图像

html - 在父 div 的底部将两个 div 居中

javascript - 将 var 作为数字增加

javascript - 将 ReactJS 对象下载为文件

html - 按下按钮时如何显示图像?