Here is my code for different situations.
Demo1:Why aren't the red boxes on the same level as the blue boxes.
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body {
margin: 0;
}
#div1 {
display: inline-block;
background-color: red;
width: 50px;
height: 50px;
}
#div2 {
display: inline-block;
background-color: blue;
width: 50px;
height: 50px;
}
#div2-child {
width: 20px;
height: 20px;
background-color: yellow;
display: inline-block;
}
</style>
</head>
<body>
<div id="div1">x</div>
<div id="div2">
<div id="div2-child"></div>
</div>
</body>
Demo2:Based on Demo1, I've removed the x, why is the blue box at the bottom.
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body {
margin: 0;
}
#div1 {
display: inline-block;
background-color: red;
width: 50px;
height: 50px;
}
#div2 {
display: inline-block;
background-color: blue;
width: 50px;
height: 50px;
}
#div2-child {
width: 20px;
height: 20px;
background-color: yellow;
display: inline-block;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2">
<div id="div2-child"></div>
</div>
</body>
Demo3:Why is every div1 and div-child on the same line when x is added.
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body {
margin: 0;
}
#div1 {
display: inline-block;
background-color: red;
width: 50px;
height: 50px;
}
#div2 {
display: inline-block;
background-color: blue;
width: 50px;
height: 50px;
}
#div2-child {
width: 20px;
height: 20px;
background-color: yellow;
display: inline-block;
}
</style>
</head>
<body>
<div id="div1">x</div>
<div id="div2">
<div id="div2-child">x</div>
</div>
</body>
Demo4:Why only div2 had the marginTop, but div1 also felt the marginTop. Is there any way to bring div1 back to the top.
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
body {
margin: 0;
}
#div1 {
display: inline-block;
background-color: red;
width: 50px;
height: 50px;
}
#div2 {
display: inline-block;
background-color: blue;
width: 50px;
height: 50px;
margin-top: 100px;
}
</style>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>