【发布时间】:2017-03-17 06:26:03
【问题描述】:
我在绝对定位的 div(标题)下方设置了一个左浮动 div(导航)。除了左侧浮动 div 之外,我还有另一个 div(文章),其边距略大于浮动 div 的宽度(通常用于两列布局)。导航和文章的上边距均为 4em(标题的高度为 3em) 导航 div 以 4em 的额外边距定位。我可以解决这个问题,但我的问题是:为什么要额外的保证金。 如果我省略标题的绝对定位,看起来我会期望:标题 - 边距 - 导航和文章在一行。 代码:
* {
box-sizing: border-box;
}
body { /*schalte voreinstellung Browser aus */
margin: 0px;
padding: 0px;
}
header {
position: absolute;
top: 1px;
left: 0px;
height: 3em;
width: 100%;
padding: 10px;
border: 1px solid blue;
border-radius: 0.5em;
background-color: silver;
}
nav {
float: left;
width: 17em; /*breite relativ zum Buchstaben "m" in der aktuellen Schriftgroesse */
border: 1px dashed red; /*Rand und padding jedoch in pixel */
margin-top: 4em;
padding: 5px;
}
article {
margin: 4em 1em 0em 18em; /* top right bottom left */
padding: 0px 10px;
border: 1px dashed red;
min-width: 15em;
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Layout</title>
</head>
<body>
<header>
headline - position fixed würde sie fest halten
</header>
<nav>
<h2> Navigation </h2>
<ul>
<li> hier die links </li>
</ul>
</nav>
<article>
<h1>CSS-basierte Layouts</h1>
<h2>2 Spalten</h2>
die ziemlich leer sind
</article>
</body>
</html>
【问题讨论】: