Placement of BOX alongside each other

Whenever I try to align the green and orange boxes side by side, it doesn't seem to work properly. Even though 1000 divided by 2 equals 500 (by the way, I am new to HTML). I am struggling with understanding positions and sizes in HTML.

Here is the HTML code:

h1{
        position:relative;
        color:#89CFF0;
        font-family:arial;
        width:1000px;
        margin:0 auto;
        text-align:center;  
    }
    .boxes{
        width:1000px;
        margin:0 auto;
        position:relative;
        text-align:center;
    }
    #box1{
        font-family:arial;
        border:1px solid red;
        position:relative;
        margin:0 auto;
        width:1000px;
        top:40px;
        background-color:red;
    }
    #box2{
        font-family:arial;
        border:1px solid blue;
        position:relative;
        margin:0 auto;
        width:1000px;
        height:150px;       
        top:40px;
        background-color:blue;
    }

    #box3{
        position:relative;
        font-family:arial;
        border:1px solid green;
        position:relative;
        margin:0 auto;
        width:500px;
        height:100px;
        top:40px;
        float:left;
        background-color:green;
    }

    #box4{
        font-family:arial;
        border:1px solid orange;
        margin:0 auto;
        position:relative;
        width:500px;
        height:100px;
        top:40px;
        float:left;
        background-color:orange;
    }

    #box5{
        font-family:arial;
        border:1px solid yellow;
        position:relative;
        margin:0 auto;
        width:1000px;
        top:140px;
        background-color:yellow;
    }
<h1> The Box </h1>
<div class="boxes">
  <div id="box1">
    <p>RED</p>
  </div>
  <div id="box2">
    <p>BLUE</p>
  </div>
  <div id="box3">
    <p>GREEN</p>
  </div>
  <div id="box4">
    <p>ORANGE</p>
  </div>
  <div id="box5">
    <p>YELLOW</p>
  </div>
</div>

Output:

I believe the issue lies with the pixel value of .box3, while I am confident that box5 is correctly set up

Answer №1

Don't forget to take the border size into account when calculating the width of your divs. If you have a parent div with a width of 1000px and you're dividing it into two equal parts of 50%, that would be 500px each. However, if there's a 1px border, you need to adjust the width to 502px (left + right borders). So, in order to maintain the overall width at 500px, you should reduce it by 2px, resulting in a final width of 498px.

For example, consider the following CSS code snippet:

h1 {
  position: relative;
  color: #89CFF0;
  font-family: arial;
  width: 1000px;
  margin: 0 auto;
  text-align: center;
}
/* Additional CSS rules for various colored boxes go here */
<h1> The Box </h1>

Answer №2

Combine both green and orange within a single div

  <div id="box3">
      <div>
       <p>GREEN</p>
      <div>
      <div>
       <p>ORANGE</p>
      </div>
  </div>

To achieve a side-by-side layout, add display:flex to the main div box3. See the example below for using the flex property.

Alternatively, you can utilize the `Bootstrap Grid

#boxes{
    display:flex;
 }


#box3{
      background-color:green;
      width:450px;
    }
 #box4{
        background-color:orange;
        width:450px;
    }
    

    
<div id="boxes">
          <div id="box3">
           <p>GREEN</p>
          </div>
          <div id="box4">
           <p>ORANGE</p>
          </div>
 </div>

Answer №3

To create a row layout, you can utilize a parent flexbox container and nest the green and orange boxes within it as children. By applying display: flex, you can achieve this arrangement. To fill the empty space at the bottom with the yellow box, adjust its top positioning or increase its height.

There seems to be some unnecessary additional space on the right side of the orange box, which disappears when the border is removed from the blue box. I noticed that borders were being used in place of padding, so I added appropriate padding for alignment.

.row {
  display: flex;
}

h1{
  position:relative;
  color:#89CFF0;
  font-family:arial;
  width:1000px;
  margin:0 auto;
  text-align:center;  
}

.boxes{
  width:1000px;
  margin:0 auto;
  position:relative;
  text-align:center;
}

#box1{
  font-family:arial;
  /* border:1px solid red; */
  padding: .5rem 0;
  position:relative;
  margin:0 auto;
  width:1000px;
  top:40px;
  background-color:red;
}

#box2{
  font-family:arial;
  /* border:1px solid blue; */
  padding-top: .5rem;
  position:relative;
  margin:0 auto;
  width:1000px;
  height:150px;       
  top:40px;
  background-color:blue;
}

#box3{
  position:relative;
  font-family:arial;
  border:1px solid green;
  position:relative;
  margin:0 auto;
  width:500px;
  height:100px;
  top:40px;
  float:left;
  background-color:green;
}

#box4{
  font-family:arial;
  border:1px solid orange;
  margin:0 auto;
  position:relative;
  width:500px;
  height:100px;
  top:40px;
  float:left;
  background-color:orange;
}

#box5{
  font-family:arial;
  /* border:1px solid yellow; */
  padding: .5rem 0;
  position:relative;
  margin:0 auto;
  width:1000px;
  top:40px;
  background-color:yellow;
}
<h1> The Box </h1>
<div class="boxes">
<div id="box1">
<p>RED</p>
</div>
<div id="box2">
<p>BLUE</p>
</div>
<div class="row">
<div id="box3">
<p>GREEN</p>
</div>
<div id="box4">
<p>ORANGE</p>
</div>
</div>
<div id="box5">
<p>YELLOW</p>
</div>
</div>

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

Disabling a checkbox within an onClick event handler

I'm facing an issue where I have a checkbox with the type "checkbox" and I'm using JAWS to read it. The problem is that in IE11, JAWS reads a disabled checked checkbox as unchecked, which I believe is a bug in IE. To work around this, I need to r ...

Having difficulty replicating the same effect across all five canvas elements

After following the code provided by j08691 here for canvas, everything worked flawlessly. However, I encountered an error in JavaScript when trying to implement it on multiple canvas elements. As a beginner in JavaScript, I would greatly appreciate any as ...

Is there a way to align both a list icon and its contents in the center?

Can someone please assist me? I am attempting to replicate a website, but I am unsure how to center align an icon list and content. Currently, it appears like this; https://i.stack.imgur.com/6FZCr.png I would like it to look like this; https://i.stack.im ...

Can you tell me if there is a switch available for hover or mouse enter/mouse leave functions?

I have a series of div elements, each containing several nested div elements. I would like to add hover effects to these div elements. However, I am unsure whether to use the hover or mouseenter function. For instance, when hovering over a div, I want it t ...

Creating multiple blocks with a 100% height in HTML

Is it true that a parent element must have a fixed height to use percentage-based height? For example, 500px. I'm designing a grid with divs arranged hierarchically. The parent has a fixed height of 500px, and the children have percentage heights, ea ...

Implement Offcanvas feature with Bootstrap 3 Navbar set to Fixed Top position

I am in the process of creating a website that will feature a large menu. However, I am not a fan of the collapsed menu that comes with BS3. Instead, I would like to implement a drawer or off-canvas menu similar to the one showcased in BS3's offcanvas ...

What might be causing the status problem to not display correctly?

My toggle feature to switch between no issue and issue is not displaying the status correctly. Despite trying to troubleshoot, I can't figure out why the issue section is not showing up. <!DOCTYPE html> <html> <script src="https://aj ...

Maximizing the potential of image srcset in HTML5

After configuring the img srcset as shown below: <img srcset="http://via.placeholder.com/320x150 320w, http://via.placeholder.com/480x150 480w, http://via.placeholder.com/800x150 800w" src="http://via.placeholder.com/8 ...

Tips for setting the tabindex on an element that has an opacity of 0

I have created a drag and drop image upload field with a hidden input element to style the upload button according to the design. However, I am concerned about accessibility and want the upload functionality to trigger when the user presses 'Enter&apo ...

CSS Table columns are set to have a fixed width for the first and last columns, while the middle two columns have dynamic widths

I was able to create a table layout like this: fixed - dynamic(50%) - dynamic(50%) - fixed http://jsfiddle.net/ihtus/ksucU/ Now, I'm wondering how to achieve this layout instead: fixed - dynamic(30%) - dynamic(70%) - fixed This is the CSS code I c ...

Is it possible for the JavaScript DOM API to retrieve the exact casing of attribute and tag names?

Is it possible to retrieve the original casing of an attribute name or tag name from the source? The attribute name is in lowercase The tag name is in uppercase The local element name is in lowercase I am looking for a solution that doesn't require ...

What is the best way to determine the length of an array using input from an HTML form?

Currently, I am in the process of developing a PHP file and one feature that I would like to implement is creating an array as illustrated in Example 1. As far as my understanding goes, an Array functions like a list where items can be added as shown in Ex ...

Manipulate the color of the parent text using a button nested within

I am working on a script that adds a button to div elements with the class name "colors". When the button is clicked, the text color of the parent element will change to a specified color. However, I'm facing an issue where it only changes the last el ...

How to Efficiently Organize OpenAI AI Responses Using TypeScript/JavaScript and CSS

I'm using a Next.js framework to connect to the OpenAI API, and I've integrated it seamlessly with an AI npm package. The functionality is incredible, but I've encountered an issue regarding line breaks in the responses. You can find the AI ...

Achieve a full page layout with content and attach the footer to the bottom using flexbox in Tailwindcss and Nextjs

How can I use flex-grow to make the body section fill the screen and keep the nav fixed at the bottom? I'm having trouble figuring out which elements to apply these properties to. Can anyone provide guidance on which element should have these styles? ...

Using jQuery to drag a div and drop it to swap out an image in a different

I am looking to implement a drag and drop function where I can move elements from one div to another, each containing different images. However, I want the image displayed in the second div to change based on the element being dragged. For example: When d ...

Avoid prolonging lines in React Styled Components

I am encountering an issue with the lines between the OR. I need to ensure that they do not extend beyond their specified boundaries. To view the code on codesandbox, please visit HERE EXPECTED OUTPUT https://i.sstatic.net/IIslv.png CODE const OR = sty ...

Content will not render when JavaScript generates SVGs

As I delve into the world of JavaScript and SVG to create interactive graphics for a website, I've run into a puzzling issue with programmatically generated SVG paths not being drawn. Below is a sample code that highlights this problem: <!DOCTYPE ...

Utilizing JQuery to track DIV clicks

I am encountering an issue with DIV clicks while using JQuery. I need guidance on this problem. Within my HTML, there are three designated DIV elements: <html> <body> <div id="overviewContainer"> <div id="firstContainer"></ ...

Searching for a way to sort out role-specific usernames in a dropdown menu using AngularJS?

Is there a way to effectively filter usernames based on specific roles in a dropdown using AngularJS? Hello, I am trying to filter the usernames with the 'KP' role in the dropdown list. Please refer to My plunker for more information. Init ...