gpt4 book ai didi

html - 如何将 div 定位为与另一个 div 齐平?

转载 作者:行者123 更新时间:2023-11-28 00:22:19 25 4
gpt4 key购买 nike

我的页面中央有一个主 div,其 id 为“面板”。

我想放置另一个 div“工具栏”,使其顶部对齐并与“面板”div 的侧面齐平。

像这样Layout

我希望面板 div 保持居中。 (目前通过将 margin-left/margin-right 设置为 auto 来实现)

当我调整窗口大小时,工具栏上的绝对定位中断。

我也尝试过将它们 float 在包装器中,但这总是会从中心移动面板...

这感觉应该很简单,我是不是忽略了什么?实现这一目标的最佳方法是什么?

当前的实例在这里: Example

感谢任何建议..

标记:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en-US" lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="toolbar">
Toolbar
</div>
<div id="panel">
Panel
</div>
</body>
</html>

CSS:

#toolbar{
border:1px solid black;
color:red;
width:100px;
height:200px;
}

#panel{
border:1px solid black;
color: blue;
margin-left:auto;
margin-right:auto;
width:500px;
height:500px;
}

最佳答案

参见: http://jsfiddle.net/thirtydot/ywc5f/

在 HTML 中,您可以将 #toolbar 移动到 #panel 中,然后使用绝对定位。

CSS:

#panel {
position: relative;
}
#toolbar {
position: absolute;
left: -102px; /* width of #toolbar + border */
top: -1px; /* border */
}

HTML:

<div id="panel">
Panel

<div id="toolbar">
Toolbar
</div>
</div>

关于html - 如何将 div 定位为与另一个 div 齐平?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8151135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com