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 :

Image-2

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

The CSS overlay effect refuses to shut down

Greetings everyone, I'm new around here so please bear with me. I am encountering an issue wherein the overlay only works in one direction - it appears when the button is clicked, but nothing happens when attempting to close it. I have tried adjustin ...

What is the best way to determine the height of a DIV element set to "auto"?

When setting a fixed height on a div using jQuery, such as $('div').height(200);, the value of $('div').height() will always be 200. This remains true even if the content within the div exceeds that height and overflow is hidden. Is th ...

Utilize Jquery's "find" function to showcase an image

I am attempting to showcase an image using jQuery. I have a function that accepts ID and PATH as parameters. The ID indicates the section (each section is an HTML page that loads upon user action). Additionally, there is a text area where I am displaying t ...

What is the correct method to sanitize the data obtained from a text area before inserting it back into the same text area?

When a user enters text into a textarea, it is directly inserted into a mySQL database. To ensure its security, I apply trim, htmlentities, mysql_real_escape_string functions to sanitize the input. Additionally, magic quotes are enabled. However, when it c ...

Implement a grid layout for columns within the profile section

Each user on my website has a profile tab that displays their submitted posts. To showcase these posts, I utilize the following code: <?php while ($ultimatemember->shortcodes->loop->have_posts()) { $ultimatemember->shortcodes->loop-> ...

"Embed a div element over a full-screen iframe or image

Is it possible to create a div within a fullscreen iframe without using JavaScript? I want to achieve a similar layout to the Netflix player, with static buttons and functions in PHP. The div should have a cut background positioned on top of the iframe. ...

What is the best way to toggle the visibility of a background image within a carousel?

As a beginner in j-query, I am struggling with creating a slider image carousel using a full background image. Most of the solutions I found online require a fixed width for all pictures to slide smoothly. However, I believe there might be a way to use an ...

The progress bar in Java Script is static and cannot be customized to change colors

Trying to use HTML for image uploads and I've created Java code to monitor the progress of the upload. However, I'm facing an issue where I cannot change the color of the progress loading bar. Below is the code I am currently using for uploading ...

Place one flex item in the center and align another item to the bottom

I am attempting to center one flex item both vertically and horizontally. At the same time, I want some text to remain fixed at the bottom of the flex container. When I use margin-top:auto on the text, it just moves the inner box to the top. Any suggesti ...

I am currently working on creating a timer and I am looking to position these three buttons in a circular formation

I want to create a stopwatch with 3 responsive buttons arranged in a circular layout. I have successfully built a responsive circle and now need help adding the buttons inside the circle next to the timer. To view the code, click here: https://jsfiddle.ne ...

Is there a way to easily copy the content within the <code> tag to the clipboard?

I've attempted to use the following code, but unfortunately, it's not functioning properly. Is there a way for me to copy the contents inside the <code> tag to my clipboard? <pre id="myCode"> <code> console.lo ...

Tips for including an external babel JS (ES6) file in an HTML document:

To include the babel js file in an HTML file, take a look at the code snippet below: <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudfl ...

Mysterious failure of JavaScript regular expression when encountering the term "tennis"

We developed a JavaScript script to detect duplicates or potential duplicates, but it seems to have some issues with certain words like "tennis." The script functions correctly in most cases, but fails when analyzing phrases related to the word "tennis" fo ...

Tips for adjusting the content direction of a Popover

I am currently working on a component that includes the following code: import { Popover, PopoverTrigger, PopoverContent, PopoverBody, Portal, Button, } from "@chakra-ui/react"; import { ReactNode } from "react"; import { Co ...

Tips on enhancing the appearance of your numberbox with vibrant colors

Hello everyone! I am working with some devextreme code blocks and need help in changing the border color of a numberbox to red. Can anyone guide me on how to achieve this? import React, { useState } from 'react'; import { NumberBox } from 'd ...

JavaScript Equivalent of jQuery's removeClass and addClass Functions

I am faced with the challenge of rewriting the following code without using jQuery: document.querySelector('.loading-overlay').classList.remove('hidden'); Can anyone provide guidance on how this can be achieved? ...

Hiding the C3 tooltip after engaging with it

I'm currently expanding my knowledge on utilizing C3.js for creating charts, and one aspect I'm focusing on is enhancing the tooltip functionality. Typically, C3 tooltips only appear when you hover over data points as demonstrated in this example ...

IE encounters an absolute z-index problem when expanding concealed content

I'm having trouble with the positioning of a block (div). I am new to CSS, so any help would be greatly appreciated. Here is the code snippet: http://jsfiddle.net/9rEmt/ (please check it out) for viewing online in IE. When I use absolute for positio ...

Creating a full width and height slider with a header menu that is responsive to all device screen sizes

Looking to create a full-width slider with a header that adjusts for all screen sizes, especially larger devices. I've been searching online but haven't found a satisfactory solution yet. If anyone has any ideas or tips on how to achieve this, pl ...

Determine the starting position of a div's CSS bottom using jQuery before the hover animation begins

Currently, I am in search of the CSS value 'bottom' for each div that belongs to the 'shelf-info-text' class. These particular divs can be found inside a parent div called 'shelf-item'. The bottom value is determined automati ...