Can you provide guidance on adjusting the HTML/CSS code in order to have the container expand horizontally to accommodate the floated children?

This arrangement causes the child divs to line up vertically.

HTML

<div class="outer">
    (other content goes here)

    <div class="containingBox">
        <div class="container">
            <div class="child"></div>
            <div class="child"></div>
        </div>
    </div>
</div>

CSS

.outer
{
    position: relative;
    top: 0px;
    left: 0px;
    width: 700px;
    margin-top: 15px;
}

.containingBox
{
    position: relative;
    top: 0px;
    left: 0px;
    width: 700px;
    height: 300px;
    background-color: red;

}

.container
{
    position: absolute;
}

.child
{
    position: relative;
    width: 700px;
    height: 300px;
    margin-right: 25px;
    float: left;
    background-color: blue;
}

http://jsfiddle.net/Ed3qL/

(I have removed the overflow-x: hidden on both containingBox and outer so you can see the issue.)

EDIT: The layout functions properly until a parent container is added outside of the existing structure!!

This setup works: http://jsfiddle.net/seB5F

This configuration does not work: http://jsfiddle.net/seB5F/1014/

Answer №1

These are the steps I took to make it function properly:

  • .outer - Excluded any position properties.

  • .containerBox - Completely removed this class.

  • .container

    • Removed all position properties.

    • Introduced white-space: nowrap; and overflow-x: scroll.

  • .child

    • Eliminated position and float properties.

    • Added display: inline-block.

  • Incorporated a style for .child:last-child that eliminates the margin-right: 25px; at the end.

Take a look at the functional jsFiddle demonstration


HTML

<div class="outer">(other content here as well)
    <div class="container">
        <div class="child">test 111</div>
        <div class="child">test 222</div>
    </div>
    <div class="container">
        <div class="child">test 333</div>
        <div class="child">test 444</div>
    </div>
</div>


CSS

.outer {
    width: 900px;
    height: 400px;
    margin-top: 20px;
}
.container {
    width: 100%;
    background-color: red;
    overflow-x: scroll;
    white-space: nowrap;
}
.child {
    display: inline-block;
    width: 900px;
    height: 400px;
    margin-right: 25px;
    background-color: blue;
}
.child:last-child {
    margin-right: 0;
}

Answer №2

The container has a width of 700px, but each child also has a width of 700px causing them to stack vertically due to size constraints. To avoid this, you can either decrease the width of the children or increase the width of the parent container so that multiple children can fit in one row. The use of float: left will only align the children horizontally if they fit within the container's width, otherwise they will be placed on separate lines.

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

Navigating high quality images

I am currently working on the development of a basic web app for iPhone and Android using phonegap. I am facing some challenges with providing high-resolution images for newer Android devices. My initial thought was to use the same images created for the i ...

Easily ajaxify your website without the need for hash or hashbang URLs

Currently enrolled in a web design course, I am eager to explore the world of ajax and how it can enhance our projects. Unfortunately, our instructor focuses solely on design and HTML, leaving us to figure out the more technical aspects on our own. If I m ...

Gather data from a variety of HTML5 video seminars

I am encountering an issue where I have multiple videos and I want to receive events from each of them. However, I am only able to get the event from one video (as tested in Chrome). Why is this happening? Here is the HTML code: <video id="video1" pre ...

Using JQuery to animate a div tag and automatically hide it after a specific duration

I'm attempting to create an animation where a div tag moves left 300px, hides after 3 seconds, and then repeats the process every 8 seconds. However, my current code only runs the animation once and doesn't repeat the entire process. Below is the ...

How to insert text into a text input using onKeyPress in react.js

I am looking to simulate a typing effect where the phrase 'Surveillance Capitalism' is inputted letter by letter into a text input field as a user types. However, I encounter an issue when trying to add each individual letter into the input; I re ...

What is the best method to make an email address distinct?

<body> <p>Please input your email address:</p> <input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email" placeholder="Email Address"> <input onclick= "validateEmail(email)" type="su ...

Tips for inserting an element with a background color into an email using variables

I have encountered an issue while trying to send an email that includes an element from my component. The element is passed from Angular to C# and then sent to the customer. Here is the element: https://i.sstatic.net/2ky1b.png Everything looks good in ...

Could you tell me how to determine the height of a page when converting it to a PDF?

I am facing a challenge trying to render a dynamic content PDF using puppeteer and react. The layouting is handled in Material UI for react, so using page-break-before:always is not feasible as it conflicts with the flex grid layout. My goal is to determin ...

Maintain parental visibility with children when navigating to a different page

I am currently working on a vertical accordion menu that opens on hover, stays open, and closes when other items are hovered. The great assistance I received from @JDandChips has been instrumental in getting this feature up and running. Now, my main focus ...

In search of a solution to ensure equal width for all items in a 2x2 grid

I have a CSS grid with two rows and two columns (refer to the code snippet). Is there a way to make all the items in the grid maintain the same width without specifying it individually? Are there any css-grid properties that can ensure consistent widths ...

What is the best way to align an image and text in a grid?

As a novice in the world of HTML and CSS, I am attempting to master the 1fr 1fr grid layout for my "welcome" section. My goal is to have an image on the left side and text on the right side. However, when I switch to Responsive mode in Inspect, my grid col ...

Steps for making a dropdown input field:1. Start by defining a

Is there a way to design a dropdown input using CSS or any other method? Take a look at this image for reference: Any suggestions on how to achieve it? ...

Expanding the width of horizontal checkboxes to 100% using jQuery Mobile

I'm attempting to expand three horizontal checkboxes across the entire width of the page. <!DOCTYPE html> <html> <head> <title>My Page</title> <meta name="viewport" content="width=device-width, initial-s ...

Having trouble with the tooltip not showing up on the fontawesome icon button?

I am experiencing an issue where the tooltip is not appearing for a fontawesome icon nested inside a element. <i class="fa fa-random" title="some tooltip"></i> Here is the CSS style being applied: i.fa { display: inline-block; } ...

Attach an event listener to a particular textarea element

Currently, I am developing a project in Next.js13 and my focus is on creating a custom textarea component. The goal is to have this component add an event listener to itself for auto-adjusting its height as the user types. Below is the relevant section of ...

implementing a vertical separator in the main navigation bar

Is there a way to insert a vertical divider after each li element on the main menu? <ul class="nav navbar-nav"> <li><a href="#"><i class="fa fa-home"></i> <span class="sr-only">(current)</span></a>< ...

JavaScript code encounters a math calculator script error

Am I overlooking something in my script? Everything seems to work fine when I click the minus (-) button, but when I press the plus (+) button, the calculator malfunctions. It displays 1, 11, 21, and so on, all ending with 1... Here is my code: functi ...

Conceal div when reaching specified screen size by utilizing pre-defined grid classes

I currently have three div elements. When viewed on a full-screen device, each column takes up 33% of the parent div 'row', which meets my expectations. However, when the screen size is reduced, I would like div B to disappear so that div A and C ...

Exploring the capabilities of Angular 9 with mat-table's multiTemplateDataRows feature to enhance the user experience by enabling simultaneous hover effects on multiple rows

Seeking advice as a newcomer to Angular, I am encountering an issue with mat-table and multiTemplateDataRows. Essentially, each element from my data source is displayed on two rows in the table. When I hover over row 1, only row 1 is highlighted, and the s ...

Is there a way to access the badge hidden behind the collapsible menu in bootstrap 4?

After moving from bootstrap 3 to bootstrap 4, my items no longer align properly. I've scoured the entire Internet for a solution, but I've run out of options (and patience.. haha) This is how it currently looks: https://i.sstatic.net/ra22j.png ...