Html 如何设置第二个div';s左上角仅基于CSS的第一个div
我可以一个接一个地定位两个分区Html 如何设置第二个div';s左上角仅基于CSS的第一个div,html,css,Html,Css,我可以一个接一个地定位两个分区 #包装器{ 左边距:自动; 右边距:自动; 高度:自动; 宽度:自动; } #内部1{ 浮动:左; 宽度:200px; 高度:200px; 背景色:红色; } #内部2{ 宽度:200px; 高度:200px; 浮动:左; 清除:左; 背景颜色:绿色; } 第一个 第二个 您不能仅使用css引用其他元素计算样式 但是 有很多技巧可以实现您只使用css的行为 例1- 例2- 你为什么不在第二个div中使用margintop呢 #inner2{ margin
#包装器{
左边距:自动;
右边距:自动;
高度:自动;
宽度:自动;
}
#内部1{
浮动:左;
宽度:200px;
高度:200px;
背景色:红色;
}
#内部2{
宽度:200px;
高度:200px;
浮动:左;
清除:左;
背景颜色:绿色;
}
第一个
第二个
您不能仅使用css引用其他元素计算样式强>
但是
有很多技巧可以实现您只使用css的行为
例1-
例2-
你为什么不在第二个div中使用margintop呢
#inner2{
margin-top: 20px;
}
那么您就不能再使用float了。所以您说,无论第二个div是什么,您都希望与第一个div完全相同?第二个div left=第一个div left他们已经有了相同的左值(0)-2nd div top=1st div height+20px一个简单的边距top:20px到2nd div就可以解决问题了?我不想硬编码
边距top:-200px用于#inner2。还有其他的解决方案吗?你的第二个方案完全不同,因为它们彼此相邻,但我想要一个接一个。
#wrapper{
margin-left:auto;
margin-right:auto;
height:auto;
width:auto;
position:relative; //makeing the wrapper "relative"
}
#inner1{
position:absolute; // setting position absolute
top:0; // and top 0 so they have the same top
float:left;
width:200px;
height:200px;
background-color:red;
}
#inner2{
position:absolute; // setting position absolute
top:0; // and top 0 so they have the same top
width:200px;
height:200px;
float:left;
clear: left;
background-color:green;
}
#inner2{
margin-top: 20px;
}