我正在创建一个带有侧边栏的 html 布局。但我的标题和内容出现在侧边栏下方,而不是旁边。
.container { position:relative; padding:10px; top:0px; right: 0; left: 0; height: 1200px;}
#sidebar {
position:relative;
top:0; bottom:0; left:0;
width:200px;
height: 1000px;
background: gray;
}
#header { border:1px solid #000; height:300px;
padding:10px; margin-left: 200px;
}
#content { border:1px solid #000; height:700px; margin-left: 200px;;
padding:10px;
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div id="sidebar">
<a href="#"> Link1 </a>
</div>
<div id="header">
<h2 class="title">Title</h2>
<h3>Header content</h3>
</div>
<div id="content">
<center>
<p>Hello</p>
</center>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
谢谢
您应该尝试将您的更改position: relative;为position: absolute;. 然后,您可以使用边距调整 div 的位置。
.container {
position:relative;
padding:10px;
top:0px;
right: 0;
left: 0;
height: 1200px;
}
#sidebar {
position:absolute;
top:0; bottom:0; left:0;
width:200px;
height: 1000px;
background: gray;
}
#header { border:1px solid #000; height:300px;
padding:10px; margin-left: 200px;
margin-top:-10px;
}
#content {
border:1px solid #000;
height:700px;
margin-left: 200px;
padding:10px;
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div id="sidebar">
<a href="#"> Link1 </a>
</div>
<div id="header">
<h2 class="title">Title</h2>
<h3>Header content</h3>
</div>
<div id="content">
<center>
<p>Hello</p>
</center>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
工作小提琴:https://jsfiddle.net/khs8j3gu/2/
祝你好运!