0

Can anyone explain the logic behind this code?

I want to know why making the div#a1 flex allows to center div#a2 by margin:auto 0; which doesn't work without making div#a1 flex.

#a1{
  display:flex;
  height: 100px;
  width: 100px;
  border:3px solid black;
}

#a2{
    height: 50px;
  width: 50px;
  border:3px solid red;
  margin: auto 0;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
   <div id=a1>
     <div id="a2">
     </div>
  </div>
</body>
</html>
johannchopin
  • 13,720
  • 10
  • 55
  • 101
MartinEG
  • 47
  • 3

1 Answers1

-1

change the #a2 to this

#a2{
  height: 50px;
  width: 50px;
  border:3px solid red;
  margin-left: 25px;
  margin-top: 25px;
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122