Adjusting the width of a container div occurs when additional floating children are inserted, causing it to expand

When there are two rows, the width of the container div in the following jsFiddle is set to 100%, but it's less than 100% when there is only one row (meaning it wraps around the children).

You can check out the jsFiddle here.

I am looking for a way to make the container div photo_stream_wrapper as close fitting around the children as possible. Any suggestions on achieving this?

Here is how it looks with one row: https://i.sstatic.net/wBj2l.png

And here it is with two rows: https://i.sstatic.net/lSsUv.png

Instead of the last image, I want to remove the additional grey space and achieve this look: https://i.sstatic.net/cVlPy.png

Answer №1

I encountered a similar issue and shared my solution in this post:

How can I make a container div the same width as its floating children and center it when there are multiple rows of floating children?

In my scenario, I had a structure like this:

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

Upon analysis, jQuery seemed to offer a viable solution. By dynamically adjusting the width of the containing div based on window size and child elements, we were able to achieve the desired outcome.

For implementation details, refer to this fiddle:

var resizeContainerDiv = function() {
  var screenWidth = $(window).width();
  var childWidth = $(".child").outerWidth();
  var innerContainerWidth = Math.floor(screenWidth / childWidth) * childWidth;

  $('.container').css('width', innerContainerWidth);
}

$(window).on("load", resizeContainerDiv);

$(window).on("resize", resizeContainerDiv).resize();


.centered {
  text-align: center;
}

.container {
  background: red;
  padding: 10px;
  display: inline-block;
}

.child {
  width: 100px;
  height: 100px;
  float: left;
}

.child:nth-child(even) {
  background: green;
}

.child:nth-child(odd) {
  background: blue;
}


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

https://jsfiddle.net/02arvnLx/1/

Answer №2

That's a thought-provoking question. One would assume that the floated container div should not exceed the width of the children (as it does with only one row).

If you are aware of the number of items in each row, enclosing the items with a row div appears to be effective:

<div class="photo_stream_wrapper">
    <div>   <!-- "row" div -->
        <div class="photo"></div>
        <div class="photo"></div>
        <div class="photo"></div>
    </div>
    <div>   <!-- "row" div -->
        <div class="photo"></div>
    </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

Animate the transition effect as soon as the block is displayed

Looking to create a smooth page transition using CSS changes with the help of Javascript (jQuery). Initially, one of the pages is set to display none. page1 = $('.page1'); page2 = $('.page2'); page1.removeClass('animate').cs ...

"Discover a unique online platform showcasing videos and images with a creative

I recently updated my personal website to showcase some of the tools I've developed through short looping videos. Initially, everything was working well with just images, but when I switched to videos, I encountered some unexpected behaviors. Interest ...

Utilizing Jquery for toggling HTML elements and styling with CSS

I'm trying to implement a jQuery toggle button that switches between two states below; When I click the Toggle Button/link, everything in picture A (above the red line) should be hidden. Clicking the toggle button again would make it reappear. I&apo ...

Are there any alternative solutions to the onunload event in Chrome, or any other ways to achieve the

After extensive searching for a solution to this issue, including scouring Google Chrome support sites and the Chrome Bug Issues page where the problem has not yet been resolved. Currently, I find myself in a situation where I need to use the onload or onb ...

Is it possible to use HTML elements to trigger actions in order to display or conceal divs using JavaScript?

Beginner - I am currently working on a webpage using TinyMCE wysiwyg and I would like to implement a functionality where I can display or hide certain divs upon clicking a link/button. Unfortunately, due to the structure of the page, it seems that I ca ...

Looking to change the background color when a table row is selected

Task : Implement a highlighted background color for selected table rows using CSS or other methods Current Table HTML : <table id="ember11999" class="ember-view content-table focus-group ovalview object-table container-view highlighted"> <tbod ...

The CSS styling does not seem to be affecting the text button's appearance

I am currently creating a website using Bootstrap 5, and I'm facing an issue with making the header image responsive on mobile devices. While it looks great on desktops, laptops, and tablets, on mobile screens, the text and button alignment is off. I& ...

Guide on deleting an item from an array stored in local storage

I'm currently facing an issue with removing a note from local storage based on its title. Despite referencing resources like w3schools and forums, I haven't been successful in achieving this task. Even after attempting various methods, s ...

Transferring information from user input to a table using AngularJS

I need assistance with AngularJS to transfer data from 2 input fields (name and last name) into a table row. <input type="text" ng-model="name"> <input type="text" ng-model="lastname"> Here is the structure of my table: <tabl ...

Utilize JavaScript to eliminate tags surrounding a text node

If my HTML code looks something like this: <div id="text"> This is some text that has an area <span class="highlight">highlighted with different formatting</span> and then some more text. </div> I am trying to figure ...

Encountering an issue with an "unknown character encoding" error while using Firefox

Currently, I am going through the JS SPA book and have observed that I am encountering different outcomes while testing in Chrome as compared to Firefox. Here's the code snippet... <!doctype html> <html> <head> <title>SPA Ch ...

What is the Javascript equivalent to "new Audio()" but for video files?

When working in Javascript, accessing the HTML-5 audio object can be done like so: var audio = new Audio('nameOfFile.mp3'); However, it seems that using a similar syntax for the video element does not work (at least on Chrome). var video = new ...

The anchor tag fails to function properly when nested within an image element inside an animated container

As I am currently developing a solar system webpage, my aim is to redirect users to a specific page when they click on a planet. Unfortunately, the anchor tag does not seem to be functioning as intended. I attempted to animate the anchor tag with an image, ...

Move the scroll bar away from the scrollable div and reposition it on a different part of the page using CSS

Is it possible to use CSS to position the scrollbar of a div that takes up the left half of my page with overflow-y: scroll to be where the page scrollbar would normally be (far right side of the page)? I've experimented with the ::-webkit-scrollbar ...

Is there a way to modify the size and color of a specific word in a CSS animation?

As a newcomer to CSS, I am exploring ways to change the font size and color of a specific word in my sentence after an animation. My initial attempt was to enclose the word within a span class like this: <h2>I'm <span style = "font-size: ...

Element is not perfectly aligned with the bottom; Slightly below the bottom edge

Why does an element positioned absolutely within a div that has relative positioning end up slightly lower than the bottom of the parent div? What can be done to fix this issue without just adjusting the bottom value on .hoverAction? function showFileUp ...

On click, the current image should be replaced with the thumbnail image

I have been working on a project and came across a codepen link https://codepen.io/robgolbeck/pen/bVGmop that is similar to what I need. However, I am trying to achieve a specific functionality where clicking on a thumbs image replaces the large image with ...

Utilizing itemprop as a CSS focus

After implementing the custom.css code provided below, I have successfully overridden the CSS files of a commercial Joomla 3 Template. div[itemprop="articleBody"] h1, h2, h3, h4, h5, h6 { margin-top: 18px!important; margin-bottom: 0px!important; ...

Display the status in the textbox once a dropdown value has been selected

I have a function that allows me to add shops. Within this function, I am able to select whether the shop is OPEN or CLOSED. The goal is for the status of the shop, either OPEN or CLOSED, to appear in a textbox on another modal. When creating a user and ...

If the input field in PHP is left empty, it will not produce an echo due to the Post/Redirect/Get

Struggling to implement the Post/Redirect/Get pattern in PHP. Despite adding the header function after form submission, the validation check for empty input fields does not display any message. Seeking guidance on integrating this pattern securely as it&ap ...