html - div 中的元素未保持原位

标签 html css

我创建了一个菜单 div。在这个 div 中,我想在左侧添加几个列表项和一个 Logo 。
但是,例如,当我给菜单项填充或高度时,它们会像 js fiddle 中所示那样超出菜单 div。这些元素不应超出其父级。

最佳答案

你应该使用 float 元素而不是你给菜单元素的边距

#logo {
    height:50px;
    width:80px;
    background-color:red;
    float:left; // add this
}
#menu ul {
    // margin-left:70%; // remove this
    float:right; // add this
    position: relative;
    line-height: 100px;
}

https://jsfiddle.net/c6tp9zmx/1/

关于html - div 中的元素未保持原位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37569073/

相关文章:

css - 具有不同颜色行的背景 - 仅限 css

html - 如何为禁用的复选框设置 CSS?

html - 使用 inline-block 和 float left 垂直和水平堆叠内容

javascript - CSS 中的自动增长模态对话框不应超出屏幕

javascript - 根据用户输入的另一行的值在表中插入一行

html - 如何减小此 div 的宽度以便另一个 div 可以紧挨着它?

javascript - ng-required,如何在输入之间使用它?

php - 在 PHP 中重定向回上一页

html - 仅使用 CSS 的等高多列/多元素布局

html - 单击 URL 'bumps' 文本/图像一点