Unable to align divs horizontally

On my webpage, I have set up two divs - Box1 and Box2. Within Box2, there are two nested divs called Box2-1 and Box2-2.

However, when I view the page, Box1 and Box2 do not align side-by-side as expected. Instead, Box1 moves downwards and lines up with the sub-div at the bottom (Box2-2).

I am trying to understand why this is happening and how I can fix it. Any guidance or assistance on resolving this issue would be greatly 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>

Answer №1

make sure to include vertical-align:top; in the styling for both #box1 and #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>

Answer №2

Here is the solution you are looking for:

#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>

If you require further clarification, feel free to leave a comment.

Answer №3

To achieve the desired layout, you can utilize CSS flexbox.

Simply wrap both box1 and box2 within a div element with a class of wrapper, then set the display property to flex in your CSS.

For more information on using CSS flexbox for organizing elements on an HTML page, check out this resource here.

.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>

Answer №4

The reason for this issue is the presence of block level elements within inline elements. For more information, refer to this source.

To resolve this, you can consider using:

position: relative;
float: left;

instead of applying display: inline-block to divs with IDs box1 and box2.

Give it a try:

#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>

Answer №5

Although the previous responses have effectively addressed how to manipulate properties and values in CSS, I believe a similar result can be achieved by utilizing the table element in 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>

Answer №6

Here is how your current output appears:

Please take note: Your Box-1 has shifted downwards because both Box2-1 and Box2-2 are set to display: block. Refer to Image-2 (below) where they are set to display: inline-block.

Image-1 :

https://i.sstatic.net/zS5It.png

Image-2

https://i.sstatic.net/woTBi.png

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;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Comparing input size attribute to div width attribute

I have a situation in my html page where I need an input component and a div component to be the same width. The input has a size attribute of 30, but using the style attribute with "width: 30ch" or "width: 30em" for the div doesn't seem to work as ex ...

How to deselect a checkbox in next.js when another one is selected

I'm looking to create a navigation system where clicking on a button scrolls to a specific section. However, I'm wondering how to deselect three inputs when one is selected in next.js using the code below. Do I need to modify each menu item indiv ...

Is your Material UI Responsive Appbar overlapping the main content? Looking for a solution to address this issue?

Currently, I am in the process of developing a website that incorporates a responsive-based app bar with an attached drawer. The design concept I am following can be located at this link, which I have been modifying to suit my needs. However, I have encoun ...

Dynamically showing a div using JavaScript and AJAX after changing the window location

After successfully fetching data from the server through AJAX, I am redirecting to the same screen/URL. Is it possible to show/display a div after redirecting using jQuery? $.ajax({ type: "POST", url: action, data: form_data, success: func ...

"Internet Explorer: Unleashing the Magic of Card Fl

I'm encountering a problem that I can't seem to solve. Everything works perfectly in Chrome, FF, Safari, etc., but in Internet Explorer, I see the image on the front side instead of the text on the backside. Can anyone please assist me with this ...

Automatically conceal a div or flash message after a short period of time by utilizing CSS3 in a React

I am relatively new to the React environment and recently created a basic component for a bookmarking feature. Essentially, when the favourite icon is clicked, an ajax call is sent > a record is created in the database > on ajax success, a flash me ...

Tips for including information in a chart

I'm facing an issue with my current code as it is not functioning properly. Here is the link to the current output: http://jsfiddle.net/4GP2h/50/ ...

Avoid automatic background resizing to match the full width of the header text

I've been trying to use CSS to style my <h1> element in a specific way, but it seems the default browser behavior is getting in the way: Despite my efforts, the browser continues to display it like this: Anyone have any suggestions on how I ca ...

Having trouble connecting to CSS in a node.js HTML document

My website is encountering an issue where the CSS fails to load, and I am receiving the following error message: Refused to apply style from 'http://localhost:5000/server/nodeClient/public/css' because its MIME type ('text/html') is not ...

The div content is aligning at the baseline rather than the middle

The social icons on my website are currently displaying at 40x40, with the text appearing below them. I am looking to center the text within the icons themselves. Any tips on how I can achieve this? Your help is much appreciated! Check out the fiddle here ...

JavaScript salary calculation function not functioning properly

Once the user inputs the employee's name and the number of hours they worked on the HTML form, the submit button captures this information and stores it in variables for calculating their pay. The script also factors in overtime pay. Despite feeling l ...

Ways to establish a default search outcome in a search box

Looking to create a search bar with JavaScript and JSON to display default search results for visitors. Currently, the search bar only shows results after text is removed. How can I show predefined search results when the page is loaded? const search = ...

What techniques can be used to avoid blinking while forcefully scrolling to the left?

In my previous inquiry about dynamically adding and removing divs on scroll, I was unable to find a satisfactory solution among the responses provided. However, I decided to take matters into my own hands and attempted to implement it myself. My approach ...

Exploring Rails 6: Enhancing Web Design with CSS Image Display

I've been attempting to update my webpage background with an image through CSS, but I'm struggling to reference one of my Rails 6 images in the process. Despite trying options like asset-url and image-url, I haven't been successful. However, ...

Challenge with collapsing a button within a Bootstrap panel

Check out this snippet of code: <div class="panel panel-default"> <div class="panel-heading clearfix" role="tab" id="heading-details" data-toggle="collapse" data-target="#details" data-parent="#panel-group" href="#details"> <h4 c ...

Optimizing images for various screen sizes with responsive design

As I delve into using responsive images, a question has arisen. On my website, there is an icon that comes in two sizes: a small version measuring 74 pixels wide and a larger version at 94 pixels wide. <img alt="Section Gino" style="background-color: ...

having trouble retrieving information from the input field

I'm having trouble retrieving the user's input from the input field, can someone assist me with this issue? I can't seem to identify what the problem is here. var ftemp = document.getElementById("Farenheit").value; <td> <i ...

Exploring the power of Javascript for number lookup

I am currently working on a coding project using TypeScript and JavaScript to locate a specific number provided by the user within a list. The goal is to display whether or not the number is present in the list when the 'search' button is pressed ...

Searching within a container using jQuery's `find` method can sometimes cause jQuery to lose control

I am trying to extract information from an input field within a table in a specific row. Here is the code I am using: var myElements = $('#myTable tbody').find('tr'); console.log(myElements); This correctly displays the items in the ...

Application of Customized Bootstrap CSS Not Successful

I have noticed that my custom CSS file does not override the bootstrap.min.css unless I include the !important tag in the class. Specifically, when I try to change the color of my background header. .bg-grey{ background-color:#333 !important; } The ...