gpt4 book ai didi

css - 使 div 成为内联元素

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:19 25 4
gpt4 key购买 nike

我正在尝试在 CSS 中进行定位,但我注意到一些让我感到困惑的事情。这可能是个愚蠢的问题……但我无法理解它。

我创建了三个宽度和高度为 50px 且背景颜色不同的 div。当我使用 margin-top 将第三个 div 向上定位 -60px 时,第三个 div 位于第二个 div 的顶部。但是,当我制作第一个和第二个 div 内联元素时,前两个 div 现在都位于第三个 div 之上。

有人可以向我解释一下这个概念吗?

<head>
<style>
<!-- Divs with the same width and height. Second one where both the first two divs are inline below this one. -->
#one{
background-color:red;
width:50px;
height:50px;

}

#two{background-color:blue;
width:50px;
height:50px;
}

#three{background-color:green;
width:50px;
height:50px;
margin-top:-60px;
}
</style>
</head>
<body>
<div id="one">
</div>
<div id="two">
</div>
<div id="three">
</div>
</body>

<head>
<style>
<!-- First two divs are inline -->
#one{
background-color:red;
width:50px;
height:50px;

}

#two{background-color:blue;
width:50px;
height:50px;
}

#one, #two{display:inline-block;}

#three{background-color:green;
width:50px;
height:50px;
margin-top:-60px;
}
</style>
</head>
<body>
<div id="one">
</div>
<div id="two">
</div>
<div id="three">
</div>
</body>

最佳答案

我认为这里有两个问题。一个是通过留下第三个 div display:block 它自然会包裹前两个 div。快速入门 blockinline-block see here .

第二部分是你有 margin-top: -60px 而有 height: 50px。负边距基本上是将它向上移动到页面上。这意味着绿色框将比红色框高 10 像素 (-60 + 50 = -10)。

Example Fiddle - 我添加了 margin-left: -5px; 并且您可以看到第三个 div 从第一个 div 的下方窥视。

关于css - 使 div 成为内联元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21492902/

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