Enclosing two smaller boxes within a larger box

I'm attempting to replicate the layout shown in the desired.png file (specifically with an outer box surrounding two inner boxes).

Currently, my code doesn't display the expected outer box around the two inner boxes. How can I modify it to achieve the desired result?

Sandbox

            <style type="text/css">
            #wrap {
               //width:600px;
               //margin:0 auto;
               border:1px solid red;
               //height:100px;
            }
            #left_col {
               float:left;
               width:300px;
               border:1px solid black;
            }
            #right_col {
               float:right;
               width:300px;
               border:1px solid orange;
            }
    </style>
</head>

<body>



    <div id="wrap">
        <span id="left_col">
            ...
        </span>
        <span id="right_col">
            ...
        </span>
    </div>

</body>

Answer №1

Unlike some programming languages, CSS does not support the // comment format; instead, you can only use /* */. One common mistake is forgetting to specify the height of the parent container when using floating elements, as they do not affect the outer container's height. It's also crucial not to overlook setting the height of the inner container. Here's an example of how I would adjust the CSS:

#wrap {
    border: 1px solid red;
    height: 100px;
    width: 600px;
    padding: 5px;
}
#left_col {
    float: left;
    display: inline-block;
    width: 295px;
    height: 100px;
    border: 1px solid black;
}
#right_col {
    float: right;
    display: inline-block;
    width: 295px;
    height: 100px;
    border: 1px solid orange;
}

Answer №2

To resolve the issue, a simple solution would be to float the #wrap element. By doing this, it will wrap around any floated elements inside of it and adjust its height accordingly.

If floating the #wrap element is not viable for your layout, implementing a clearfix method can also solve the problem. There are various methods available to achieve this, as demonstrated previously.

From my perspective, if it makes sense semantically, using an hr element with clear:both in a CSS class could be a suitable option.

.clearer {
    clear:both; 
    visibility:hidden;
}

However, every developer has their own preference. Nowadays, utilizing content:after is considered a safe approach.

Answer №3

<div id="container">
    <div id="sidebar_left">
        ...
    </div>
    <div id="sidebar_right">
        ...
    </div>
    <div style="clear:both;"></div>
</div>​

Styles

#container {
           width:620px;

           border:1px solid blue;
          padding:10px;
        }
        #sidebar_left {
           float:left;
           width:290px;
           border:1px solid gray;
        }
        #sidebar_right {
           float:right;
           width:290px;
           border:1px solid purple;
        }​

Answer №4

When using floats, remember that the height of an object will adjust to its content. If the inner divs have no content, nothing will be visible except for the borders.

It's important to make sure you understand .clearfix because floating elements within the #wrap without proper clearing can cause issues with height calculations in #wrap.


Here is a solution:

CSS:

#wrap {
    border:1px solid red;
    width:604px
}
.col {
    float:left;
    width:300px;
    border:1px solid black;
}

/* The Amazing clearfix */
.clearfix:before,
.clearfix:after {
    content: " ";
    display: table;
}
.clearfix:after {
    clear: both;
}

HTML

<div id="wrap" class="clearfix">
    <span class="col">
        Content!
    </span>
    <span class="col">
        Content!
    </span>
</div>

Answer №5

Check out this demonstration and customize the dimensions to suit your requirements:

View My Demo

Below is the HTML markup for reference:

<div id="wrap">
    <div id="left_col">
        ...
    </div>
    <div id="right_col">
        ...
    </div>
    <div style="clear:both;"></div>
</div>​

CSS:
------

#wrap {
    width:420px;
    border:2px solid red;
    padding:5px;
    margin: 10px;
}

#left_col { 
    float:left;
    width:200px;
    border:2px solid #FFA9C5;
    overflow: hidden;
    margin-right: 10px;
}

#right_col {
    float:left;
    width:200px;
    border:2px solid blue;
}​

Answer №6

To fix the issue, simply add overflow:auto to the CSS rule for #wrap

#wrap {
               //width:600px;
               //margin:0 auto;
               border:1px solid red;
               //height:100px; overflow:auto
            }

Check out the demonstration here

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

Creating the appearance of a white sheet of paper on a computer screen

I appreciate all the comments provided earlier. Looking back, I realize I should have been more thorough in my explanation. Hopefully, the updated version will make things clearer. On a broad level, my goal is to create a digital screen background that re ...

Creating a blurred background effect when a React portal is presented

I have implemented React portals to display a modal popup after the form is submitted. However, I am facing an issue with blurring only the background while showing the modal, similar to what is shown in picture 2. Initially, I tried using document.body.st ...

What could be causing the malfunction in my JavaScript random selector?

Can anyone assist me with this issue? I'm attempting to implement JavaScript functionality to highlight randomly selected picks that I input, but it's not functioning correctly. Every time I inspect my JS code on the webpage, I encounter an erro ...

Troubleshooting head.js and jQuery problems in Chrome/Firefox 5

After rendering the page, it looks something like this: <!DOCTYPE html> <html> <head> <script> head.js("js/jquery.js", "js/jquery.autocomplete.js"); </script> </head> <body> ... content ...

Arrange multiple divs on top of each other by utilizing the Bootstrap flexbox system

Apologies for my lack of experience with Bootstrap, specifically version 4.0. I have been researching extensively but am struggling to accomplish the simple task of stacking two divs on top of each other. <div class="d-flex flex-column"> <div ...

The issue with the max-height transition not functioning properly arises when there are dynamic changes to the max-height

document.querySelectorAll('.sidebarCategory').forEach(el =>{ el.addEventListener('click', e =>{ let sub = el.nextElementSibling if(sub.style.maxHeight){ el.classList.remove('opened&apos ...

Display the JSON result from a RESTful API call within an Ionic framework

I am facing a challenge in displaying a JSON response retrieved from a RESTful API request. Below is the code snippet: products:Observable<any>; constructor(public navCtrl: NavController, private backgroundGeolocation: BackgroundGeolocation, public ...

Customize Typography style variations in Material-UI version 5

I have encountered a similar issue, but with a twist from Can't override root styles of Typography from Materil-UI Back in v4, I had a theme set up like this: import { createTheme } from '@material-ui/core/styles'; export const theme = cre ...

When attempting to shift the label above the input field upon focusing, it unexpectedly becomes concealed

I am attempting to reposition the label above the input field when it is focused, but instead it becomes hidden. label > span::focus { top: -20px; font-size: 15px; color: blue; } Check out the complete Html & CSS code. Update: ...

Resolving the Material-UI NextJS Button Styling Dilemma

I've encountered an issue with the styling of a few buttons in JS. When I apply a styling class using className, the formatting works fine on the initial render but loses its styling on subsequent refreshes. This problem is specific to two individual ...

When implementing auto-generated IDs in HTML forms, rely on randomly generated values for increased uniqueness

When developing a form with multiple complex controls built as Backbone views, one wants to ensure that the labels are correctly linked to the input elements. This is typically done using the "for" attribute. However, in cases where the same control needs ...

Find the Nearest Value with Jquery and Swap Out the Div

When users complete a form and click "continue," they move on to the next section. At the top of the page, a heading indicates that the previous form is finished. If users click on the heading, it expands the completed form so they can edit it. For instan ...

Ensuring proper alignment of images and text in HTML when resizing

Trying to show an image and text side by side, I'm using the following code: div.out { background-color: silver; border: 1px solid black; padding: 10px; display: flex; } div.right { order: 1; background-color: white; border: 1px soli ...

Text with Icon Fonts Does Not Align Vertically

I'm using icon fonts in a nav list and I want to place text between two of the icons. However, there's an issue with alignment - the icons are positioned higher than the text, causing them to not match up well. Is there a solution to this problem ...

Resetting the Countdown Clock: A Transformation Process

I have implemented a countdown timer script that I found online and made some adjustments to fit my website's needs. While the current setup effectively counts down to a specific date and time, I now require the timer to reset back to a 24-hour countd ...

When attempting to host a web application built using the Impact JavaScript game engine on a local server, Chrome throws CORS errors

Currently, I am working on a webapp created with the Impact JS game engine that needs to run locally without using a localhost (using file:///C:/...). The issue I am facing is with Chrome, as it is blocking the loading of images (mainly png/jpg) from the m ...

What is the most effective method to implement a toggle function using only JavaScript, without any reliance on jQuery?

Recently, I had a navigation bar in the form of an unordered list (ul) with multiple list items (li), and the last li element was labeled 'more' which had its own sub-menu in the form of another ul. To toggle this sub-menu between visible and hid ...

Trouble with displaying my three.js 3D model

I've tried multiple solutions I found, but none have fixed the issue of not being able to display 3D models. Many have suggested that it could be a problem with the camera, but so far I haven't been able to resolve it. Any assistance would be gre ...

JSDOM failing to retrieve complete list of elements from webpage

I'm currently struggling with a simple webscraper using JSDOM. Here's the code snippet I've been working on: const axios = require("axios"); const jsdom = require("jsdom"); const { JSDOM } = jsdom; let v = "15" ...

Form input using the div element (when clicked)

I am currently working on a page that connects to a database with user information. For each user, I am creating a separate div element so that there are 6 divs total (each representing a different user). My goal is to have the ability for a user to click ...