带有浮动元素的css边距操作

时间:2011-08-26 23:59:43

标签: html css

在以下简单的html代码中:

<body>
   <div style="float:left">text 1</div>
   <div style="margin-left:10px">text 2</div>
</body>

为什么不将“文本1”与“文本2”分开?即这最终会产生“text 1text 2”。显然,保证金是针对身体的,而不是第一个div。所以,如果我把“margin-left:100px”,那么它们会显示分离,因为text1的宽度小于100px。

谢谢

1 个答案:

答案 0 :(得分:2)

首先尝试以下操作,因为您将看到100px是不够的,因为您只是浮动第一个div,但第二个div将紧挨着第一个div。

<div style="float:left">text 1 text 1 text 1 text 1 text 1 text 1 text 1</div>
<div style="margin-left:100px">text 2</div>

你也必须浮动第二个div。

<div style="float:left">text 1</div>
<div style="float:left;margin-left:10px">text 2</div>