html - css - Div 没有以正确的方式出现

标签 html css

我的页面上有许多 div,但有两种类型。第一种是“主项”,第二种是“子项”。问题是子项与主项重叠。

这是一个 jsfiddle:http://jsfiddle.net/CcPyG/

更多权限这里是我的代码:

HTML

<div class="item">
    Item 1
    <div class="sub-item">  
        Sub Item 1
    </div>
</div>
<div class="item">
    Item 2
    <div class="sub-item">  
        Sub Item 2
    </div>
</div>
<div class="item">
    Item 3
    <div class="sub-item">  
        Sub Item 3
    </div>
</div>
<div class="item">
    Item 4
    <div class="sub-item">  
        Sub Item 4
    </div>
</div>
<div class="item">
    Item 5
    <div class="sub-item">  
         Sub Item 5
    </div>
</div>

CSS

.item {
    font-family: Menu;
    color: White;
    width: 250px;
    height: 35px;
    text-align: center;
    vertical-align: middle;
    line-height: 35px;
    color:black;
}
.sub-item {
    margin-top: 40px;
}

最佳答案

您的 item 包含 sub-item。问题是 item 的高度固定为 35px。 sub-item 的上边距为 40px。这意味着从 item 顶部到 sub-item 顶部的距离是 40 px -- 这比 item 本身的整个尺寸还大!

要解决此问题,请从 CSS 中的 .item 中删除 height: 35px。参见 this updated JSFiddle .

关于html - css - Div 没有以正确的方式出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17027430/

相关文章:

javascript - 将一 block Canvas 转换为 blob 类型

javascript - 单击提交按钮时,在下拉列表中显示值的问题和用户输入的输入字段值不会被清除 - Jquery

javascript - 在 quill-image-resize-vue 中对齐时出错

html - 为什么将鼠标悬停在一个 CSS 上会影响我的其他 CSS 元素?

html - 如何根据单词的长度在单词之间创建等距空间?

javascript - 使用 CSS 缩放时,jquery-ui slider 行为错误

css - 将页面分成象限

html - 标题居中但副标题居中 "right"

html - Gmail 阻止内联 css 属性

html - Css3 计算函数 : issue with mod operator