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

Vue.js component communication issue causing rendering problems

When it comes to the Parent component, I have this snippet of code: <todo-item v-for="(todo, index) in todos" :key="todo.id" :todo="todo" :index="index"> </todo-item> This piece simply loops through the todos array, retrieves each todo obj ...

ng-required is ineffective when used with number inputs that have a minimum value requirement

In my form, I have implemented a checkbox that, when checked, toggles the visibility of a div using AngularJS's ng-show. Within this div, there is an input field of type "number" with a validation setting of min="10000". I am trying to prevent the f ...

Update text displayed on radio button selection using jQuery

Is there a way to change the label text on a selected radio button from "Set default" to just "default" using jQuery? I think I need to use the class radio-default as the selector, but I'm not very familiar with jQuery. <div class="radio-default ...

Why won't the display: block CSS style apply to my div element?

Take a look at my code: https://jsfiddle.net/r62p4209/ I created a search bar with company brand name and some buttons on the right, positioned in the center. My aim is for the search bar (from "search by:" to the "Go!" button) to move onto the next lin ...

What is the proper way to showcase thumbnails in different sizes?

Currently, this is what I have: The display looks optimal with high-resolution landscape photos. This is the HTML and CSS code in use: <div class="upload-thumb ng-scope visible"> <span class="delete-media"> <span class="icon-bin ...

How can I show an item repeatedly with each button click in ReactJS?

Currently, I have a setup where I can display a checkbox and its label upon clicking a button. However, the limitation is that only one checkbox can be displayed at a time. Is there a way to modify this so that I can show multiple checkboxes simultaneous ...

Is there a way to lead to a password-protected page without making it accessible through the URL?

I'm currently honing my skills in web development and embarking on a project to create an interactive puzzle website. The premise is simple - the homepage will feature just an answer input field where users can enter the correct solution to progress t ...

The text feature for the Google Places API is not displaying properly within the MUI TextField

For address auto-generation, I am currently utilizing the Google Places API. I have a regular input tag in my HTML where users can type their input and see Google Places suggestions in the dropdown - all working perfectly. However, when I switch to using m ...

Verification of the data entered in the input box

I am looking to develop an Input Box where users can enter the private details of a person. The first character must be either A or E, and the rest can be alphanumeric with no special characters allowed. I need to implement validation on the client side to ...

"Emulate the social sharing capabilities of CNET with interactive share

I remember a while ago, CNET had these amazing Social Share buttons that, when clicked, would reveal a "dropdown box" with the social network share dialog. Does anyone know how they achieved that? I've searched but couldn't find any information ...

Show the cost on the HTML page according to the chosen currency option

I am currently dealing with two primary currencies in my business. The product page is created using HTML. There are 4 products on a single page, and I wish to display two prices for each product based on the selected currency (USD or EUR) without having t ...

Prevent the hover() effect from affecting all div elements at once

I am aiming to achieve a function where hovering over a div with the "rectangle" class will display another div with the "description" class. Initially, the description div will have a display value of "none", but upon hovering, it should become visible. ...

Extraction of data post logging into the webpage (modem authentication page)

I am currently working on a project that involves extracting data from a specific webpage. However, in order to access the data I need, I first have to log in to the website. After trying out different scripts and searching for solutions online, I managed ...

Align two columns using the vertical alignment feature in the Bootstrap grid system

Is there a way to horizontally align an element within a bootstrap grid? In the code snippet below, I have a col-md-4 and another col-md-8 that I would like to vertically align in the middle, as shown in the image. Click here for a demonstration. This is ...

The focusable attribute in SVG is malfunctioning

I've utilized the focusable attribute to ensure SVG elements receive focus within an HTML document. My goal is to navigate through SVG elements in the SVG tag using the TAB key, as indicated in this resource (http://www.w3.org/TR/SVGTiny12/interact.h ...

Customizing active-class in Vuetify

How do I customize the appearance of my v-list-item-group when it is in the active state? <v-list> <v-list-item-group v-model="day" color="green"> <v-list-item v-for="(item, i) in items" :key="i"> <v-list-item-content& ...

Tips for closing the navbar by clicking elsewhere on the page

Is there a way to modify my code in order for the navbar to close when clicking outside of it, while staying open if something inside it is clicked? $(document).ready(function() { $('.nav-btn').on('click', function() { $('.na ...

Reducing image size using CSS can result in blurry images on multiple web browsers

Encountering challenges with downscaled images in multiple browsers... The images must be downscaled to adapt to the browser size. Here is the code snippet: #pic-holder img { -moz-transform: rotate(0deg); /*image-rendering:-webkit-optimize-contras ...

`Can Beautiful Soup extract data (text) from elements that share the same class identifier?`

Currently working on a personal project focused on web scraping, using beautiful soup to extract data from a website. Encountered an issue where data with the same class but different attributes is present. For instance: <div class="pi--secondary-price ...

Harnessing the power of JSON within an HTML document to display dynamic data

Check out the JSON data through this link: The JSON file contains information about the weather. I need assistance with incorporating the data from the provided link into my HTML document as I am new to utilizing JSON for this purpose. <html> < ...