我有 3 个 Div,我希望它们在某些图像下精确排列。我试过摆弄 float 它们并使用边距,但我不知道如何到达可以指定从左边距开始的像素数的地步。
<div class="cont1" style="margin-left:100px; float:left;"> label </div>
<div class="cont2" style="margin-left:200px; float:left;"> label </div>
<div class="cont3" style="margin-left:300px;"> label </div>
使用 float:left 似乎搞砸了很多间距。没有它,间距是正确的,但每个 div 都在一个新行而不是单行上。
最佳答案
我认为您希望第二个 div cont2
距窗口左侧 200 像素,而实际上它距第一个 div 左侧 200 像素。
如果那是你想要的,你应该改用绝对定位:
.cont1 {
background-color: red;
position: absolute;
top: 0;
left: 100px;
max-width: 100px;
}
.cont2 {
background-color: blue;
position: absolute;
top: 0;
left: 200px;
max-width: 100px;
}
.cont3 {
background-color: green;
position: absolute;
top: 0;
left: 300px;
max-width: 100px;
}
关于CSS间距div精确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8646216/