设置 margin: 0
不在 header{}
或 nav{}
中,但在 ul{} 中按预期工作
。我认为 ul{margin:0}
不会起作用,因为 header
和 nav
是父级。
<head>
<meta charset="UTF-8">
<title>Compañía</title>
<link rel="stylesheet" href="estilo.css"> </head>
<body>
<header>
<nav>
<ul>
<li><a href="#">About</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Purchase</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
</body>
</html>
body {
margin: 0;
padding: 0;
}
header {
background-color: darkseagreen;
height: 420px;
margin: 0;
padding: 0;
}
nav {
margin: 0;
}
最佳答案
那是因为您看到的边距来自 ul
,而不是 header
或 nav
。默认情况下,header
和 nav
的边距都为 0,因此向其中任何一个添加 margin: 0;
都不会执行任何操作。但是,ul
具有浏览器应用的默认边距,因此设置 ul { margin: 0; }
删除默认边距。
由于 margin collapse,您看到 ul
的边距超出了它的父级边界
If there is no border, padding, inline content, block_formatting_context created or clearance to separate the margin-top of a block from the margin-top of its first child block, or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block from the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.
关于html - 标题、导航中的边距为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42183721/