0

I was practising positioning divs by CSS and I found out margin of div being ignored. I cannot understand why margin-top of b2 is being ignored. When I modified margin-bottom of b1, there was a space between two divs.

.b1 {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  float: left;
}

.b2 {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  clear: both;
  margin-top: 100px;
}
<div class="b1"></div>
<div class="b2"></div>
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
doku
  • 1
  • 2
    Does this answer your question? [Why top margin of html element is ignored after floated element?](https://stackoverflow.com/questions/1883414/why-top-margin-of-html-element-is-ignored-after-floated-element) – Run_Script Jul 17 '20 at 17:15
  • You don't need to use float now. Use FLEXBOX, https://css-tricks.com/snippets/css/a-guide-to-flexbox/ or for more complicated grid layout use GRID, https://css-tricks.com/snippets/css/complete-guide-grid/ – Shyam Jul 17 '20 at 17:26

1 Answers1

1

The problem is that only one of the divs had the float property. If you add float: left; to .b2 then it works as expected.

.b1 {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  float: left;
}

.b2 {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  clear: both;
  margin-top: 100px;
  float: left;
}
<div class="b1"></div>
<div class="b2"></div>
imtheman
  • 4,713
  • 1
  • 30
  • 30