0

The webpage has two divs (Box1 and Box2), and Box2 contains two nested divs, Box2-1 and Box2-2.

I expect Box1 and Box2 to align side-by-side, but Box1 moves downwards and aligns with the sub-div at the bottom, Box2-2.

Please teach me why this occurs and how to fix this. Any help would be much appreciated.

#box1{
    width: 200px;
    height: 845px;
    
}
#box1, #box2{
    border: 1px solid black;
    display: inline-block;
    margin: 0px 20px;
}
#box2-1, #box2-2{
    height : 400px;
    width: 200px;
    border : 1px solid black;
    box-sizing: border-box;
    margin: 15px;
    display: block
}
<!DOCTYPE html>
<html>
    <head>
        <title</title>
        <link href="style.css" rel="stylesheet">
    </head>
    <body>
            <div id="box1">
                <p>Box1</p>
            </div>
            <div id="box2">
                <div class="box2" id="box2-1">
                    <p>Box2</p>
                </div>
                <div class="box2" id="box2-2">
                    <p>Box3</p>
                </div>
            </div>
    </body>
</html>
fortunee
  • 3,852
  • 2
  • 17
  • 29
ce lu
  • 3
  • 1

6 Answers6

0

I think this is what you want;

#main-div {
  display: flex;
  justify-content: space-evenly;
}

#box1 {
  border: 1px solid black;
}


#box2 {
  border: 1px solid black;
}
<!DOCTYPE html>
<html>
    <head>
        <title</title>
        <link href="style.css" rel="stylesheet">
    </head>
    <body>
      <div id="main-div">
            <div id="box1">
                <p>Box1</p>
            </div>
            <div id="box2">
                <div class="box2" id="box2-1">
                    <p>Box2</p>
                </div>
                <div class="box2" id="box2-2">
                    <p>Box3</p>
                </div>
            </div>
          </div>
    </body>
</html>

Please comment if you need explanation on this.

Irfan wani
  • 4,084
  • 2
  • 19
  • 34
0

add vertical-align:top; on #box1, #box2

#box1, #box2{
    border: 1px solid black;
    display: inline-block;
    margin: 0px 20px;
    vertical-align:top;
}

#box1{
    width: 200px;
    height: 845px;
    
}
#box1, #box2{
    border: 1px solid black;
    display: inline-block;
    margin: 0px 20px;
    vertical-align:top;
}
#box2-1, #box2-2{
    height : 400px;
    width: 200px;
    border : 1px solid black;
    box-sizing: border-box;
    margin: 15px;
    display: block
}
<!DOCTYPE html>
<html>
    <head>
        <title</title>
        <link href="style.css" rel="stylesheet">
    </head>
    <body>
            <div id="box1">
                <p>Box1</p>
            </div>
            <div id="box2">
                <div class="box2" id="box2-1">
                    <p>Box2</p>
                </div>
                <div class="box2" id="box2-2">
                    <p>Box3</p>
                </div>
            </div>
    </body>
</html>
Lalji Tadhani
  • 14,041
  • 3
  • 23
  • 40
0

You can use css flexbox to easily achieve this.

Wrap box1 and box2 in a div with class of wrapper and in your css set the display property to flex;

You can read more about css flexbox here, it is used for arranging boxes in a html page.

.wrapper {
  display: flex;
}

#box1{
    width: 200px;
    height: 845px;
}

#box1, #box2{
    border: 1px solid black;
    display: inline-block;
    margin: 0px 20px;
}

#box2-1, #box2-2{
    height : 400px;
    width: 200px;
    border : 1px solid black;
    box-sizing: border-box;
    margin: 15px;
    display: block;
}
<!DOCTYPE html>
<html>
    <head>
        <title</title>
        <link href="style.css" rel="stylesheet">
    </head>
    <body>
            <div class="wrapper">
              <div id="box1">
                  <p>Box1</p>
              </div>
              <div id="box2">
                  <div class="box2" id="box2-1">
                      <p>Box2</p>
                  </div>
                  <div class="box2" id="box2-2">
                      <p>Box3</p>
                  </div>
              </div>
            </div>
    </body>
</html>
fortunee
  • 3,852
  • 2
  • 17
  • 29
0

It's because you have block level elements inside inline elements. You can read about it here.

One way around this would be to use

position: relative;
float: left;

instead of display: inline-block for divs box1 and box2.

Try it out:

#box1 {
  width: 200px;
  height: 845px;
}

#box1, #box2 {
  position: relative;
  float: left;
  border: 1px solid black;
  margin: 0px 20px;
}

#box2-1, #box2-2 {
  height : 400px;
  width: 200px;
  border : 1px solid black;
  box-sizing: border-box;
  margin: 15px;
  display: block;
}
<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  
  <body>
    <div id="box1">
      <p>Box1</p>
    </div>
    <div id="box2">
      <div class="box2" id="box2-1">
        <p>Box2</p>
      </div>
      <div class="box2" id="box2-2">
        <p>Box3</p>
      </div>
  </div>
  </body>
</html>
Ivan86
  • 5,695
  • 2
  • 14
  • 30
0

While the other answers have done a brilliant job of fixing the properties and values in CSS. I think we can achieve something similar using the table tag of HTML:

<!DOCTYPE html>
<html>
    <head>
        <title</title>
        <!-- <link href="style.css" rel="stylesheet"> -->
    </head>
    <body id="body">
        <table>
            <tr>
                <td rowspan="2">Box1</td>
                <td>Box2</td>
            </tr>
            <tr><td>Box3</td></tr>
        </table>
    </body>
</html>
Hardik Rajpal
  • 37
  • 1
  • 4
0

Your current output looks like this :

Note : Your Box-1 is shifted below because your Box2-1 and Box2-2 are marked as display: block. Please refer Image-2 (below) with display: inline-block.

Image-1 :

enter image description here

Image-2

enter image description here

Updated style for box2-1 and box2-2 :

#box2-1, #box2-2{
    height : 400px;
    width: 200px;
    border : 1px solid black;
    box-sizing: border-box;
    margin: 15px;
    display: inline-block;
}
Rahul Singh
  • 184
  • 6