1

So I applied clear:left to a div and tried changing its top margin but it did not affect anything. Here is the code:

<!DOCTYPE html>

<html>

<head>
  <title>Float</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .div1 {
      border: 2px solid black;
      float: left;
    }
    
    .div2 {
      clear: left;
      border: 2px solid red;
      width: 120px;
      height: 120px;
      margin-top: 320px; /* Why is margin not working?*/
    }
  </style>
</head>

<body>
  <div class="div1">This is suppose to be a first div</div>
  <div class="div2">Div2</div>
</body>

</html>

Here is the result :

Here

Now the problem is that the result is same even if I add or remove the top margin. It would be great if you can show me the right answer and also explain why this is happening.

Community
  • 1
  • 1

1 Answers1

1

Use of one container like this :

<div class="con" style="overflow: hidden">
    <div class="div1">This is suppose to be a first div</div>
</div>

.div1 {
    border: 2px solid black;
    float: left;
}
        
.div2 {
    border: 2px solid red;
    width: 120px;
    height: 120px;
    margin-top: 100px;
}
<div class="con" style="overflow: hidden">
    <div class="div1">This is suppose to be a first div</div>
</div>
<div class="div2">Div2</div>
Ehsan
  • 12,655
  • 3
  • 25
  • 44