【问题标题】:what is the difference between margin-left and left, in my code margin-left:90px moves my box but left does not move the box [duplicate]margin-left 和 left 有什么区别,在我的代码中 margin-left:90px 移动我的盒子,但 left 不移动盒子 [重复]
【发布时间】:2021-05-18 19:18:42
【问题描述】:

我有 box1,我用 margin-left och left 进行测试。 margin-left 可以移动框,但 left:90px 没有任何反应。这两者之间到底有什么区别?

#box1 {
    width: 250px;
   padding: 25px;
   border: 15px solid;
  top:30px;
 //margin-left:90px;
left:90px;

}

#box1 {
        width: 250px;
       padding: 25px;
       border: 15px solid;
      top:30px;
     margin-left:90px;
    //left:90px;
}
        
<div id="box1">

Box1
</div>

【问题讨论】:

  • 你需要 position:relative 才能使用 left
  • left 需要定位上下文

标签: css


【解决方案1】:

margin-left 在元素左侧添加边距。

left 改变元素的位置,只能与position 属性一起使用,或者更确切地说,与默认的position: static 值一起使用时无关紧要。

p
{
  margin-left: 10px;
}

div
{
  position: absolute;
  left: 100px;
}
<p>I have a margin</p>
<div>I'm positioned with left</div>

【讨论】:

  • 谢谢大卫!如我所见:margin-left、margin-right 等可用于所有位置,但 left、right、top .. 可用于绝对位置?
猜你喜欢
  • 1970-01-01
  • 2011-09-19
  • 2013-09-17
  • 1970-01-01
  • 2012-10-05
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多