html - 在 CSS 导航样式方面需要帮助

标签 html css

我正在尝试构建 CSS 导航栏,但遇到了一些麻烦。在我的代码中,背景框正在折叠其中的内容。我的问题是它为什么会折叠,是否可以通过不给框高度来解决。这是我的代码。 HTML

<div class="item">
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
            <li>Item 4</li>
        </ul>
    </div>

CSS

body {
    color: #648;
}
.item ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

* {
} 
.item {
    padding: 20px;
    width: 70%;
    /* height: 65px; */
    background-color: blanchedalmond;
    margin: 50px auto;
    border-radius: 10px;
}

.item li {
    float: left;
    width: 45px;
    margin: 10px;
    border-radius: 10px;
    padding: 20px;
    background-color: aqua;
}

最佳答案

使用 display:inlinedisplay:inline-block 而不是 float:left

http://jsfiddle.net/x2ubrrh3/

更新 当使用 display:flex 时,您必须在列表完成后停止元素 float (clear:both)

参见此处:http://jsfiddle.net/x2ubrrh3/1/

关于html - 在 CSS 导航样式方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27472986/

相关文章:

javascript - 在子元素上应用悬停效果

javascript - 一次加载母版页

javascript - 如何使用 JavaScript 更改字符串中特定字符的颜色?

javascript - 滚动事件永远不会触发

javascript - 带有 html 内容的工具提示出现在右侧,有工作代码,但它仅适用于 1 个提示,我希望它是动态的

html - 图像放大使用 UL 和 LI 不工作

html - CSS垂直菜单栏,防止子菜单超过一定高度

iphone - iPhone 上的额外右边距

javascript - 一个 div 的内容重叠到另一个 div

html - 容器不收取自动 margin