Setting the parent width to adjust dynamically with the elements inside it is a

Can anyone help me figure out why my parent div's children are breaking onto two lines instead of displaying horizontally in one line? I've been using jQuery's outerWidth(true) function to calculate the total necessary width, but something seems off. I've tested it in Chrome, FF, Safari, and IE9. Any ideas on what could be going wrong?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8>
    <title>test</title>
    <style>
        #outer{
            width: 100px;//setting up width for outer parent div
            height: 200px;
            padding: 0px;

          }
         .inner{ //represent a child element here with inner attribute like age , name etc
            display: inline-block;
            width: 20px;
            height: 20px;
            margin-right: 5px;
            background-color: yellow;
        }

     </style>
</head>
<body>
    <div id="outer">
        <div class="inner">0</div>
        <div class="inner">1</div>
        <div class="inner">2</div>
        <div class="inner">3</div>
        <div class="inner">4lt;/div>
        <div class="inner">5</div>
        <div class="inner">6</div>
        <div class="inner">7</div>
        <div class="inner">8lt;/div>
        <div class="inner">9</div>//end of child elements
      
    </div>;

       <script src="../js/jquery.1.11.0.js"></script>;//link to jquery file 

    <script>
    $(document).ready(function() {
        var itemsWidthWithMargin = 0;
        $(".inner").each( function(){
                itemsWidthWithMargin += $(this).outerWidth(true);
        });
       
       $("#outer").width(itemsWidthWithMargin);   //final calculation for setting witdth dynamically

      alert('itemsWidthWithMargin');
    
});

    </script>
      
</body>//close the body  
</html> 

Answer №1

swap out inline-block for float:left;

.inner{
            float:left;
            width: 20px;
            height: 20px;
            margin-right: 5px;
            margin-bottom: 5px;
            box-sizing:border-box;
            background-color: yellow;
        }

Check out the code on jsfiddle

When using inline-block, you'll notice some unnecessary white space: See screenshot here.

With float: left everything aligns nicely: View the clean layout in this screenshot.

If you're curious about the extra spacing issue, you can find more information 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

Is there a way to insert a row into a datatable without needing to perform an Ajax reload or using the

When using row.add(…) on a datatable, I encounter an issue where it refreshes via an ajax call when draw() is activated. This leads to the new row not being visible because the data is reloaded from the database. The UX flow behind this scenario is as f ...

Adjusting the zoom feature in CSS3

I'm looking to have my React app display as if the user has changed the zoom level to 90%. I attempted to tackle this issue using CSS. body { transform: scale(0.9); } However, this method did not result in the screen appearing similar to wh ...

What is the process for loading a webpage into another webpage with google app engine?

Hey there, I'm a newbie to app engine and could really use some help, I'm looking to replicate the functionality of the jquery load function on the server side within a RequestHandler's get() method. Currently, I have a page that looks some ...

In Angular 4, the Bootstrap modal now only opens after a double click instead of opening on the first click

Working on an eCommerce application, there is a cart icon that triggers a modal screen displaying user-selected product data when clicked. However, the issue I'm facing is that upon initial page load, the modal screen opens only after double-clicking; ...

Mastering the art of handling errors with Javascript Promises

Through thorough research and some assistance, I have managed to create and annotate code that I believe will enhance the comprehension of Javascript Promises for individuals. However, I encountered a puzzling issue when attempting to trigger an error by ...

Confusion between Codeigniter's URL and base_url() function

$config['base_url'] = 'localhost/project'; <link href="<?= base_url(); ?>/css/bootstrap.min.css" rel="stylesheet"> An issue arises with the CSS not loading on the view due to a double '/' in the URL. This occur ...

leveraging ng-bind-html for dynamic content manipulation and fallback content

Currently, I am experimenting with Angular to reload or 'livechange' content. The default structure of the content is not generated by Angular itself. Here is what I have: <div>Default Value</div> I am looking for a way to dynamical ...

Style.css remains invisible to NetBeans

My webapp is built using Servlets and JSP. However, I am currently facing an issue with directing NetBeans to my style.css file. Whenever the stylesheet is placed in WEB-INF or META-INF or even outside them within the Web Pages directory, everything func ...

Steps for enabling ckeditor plugin for all textarea on a webpage:

Having trouble applying CKEditor to multiple textarea elements? Look no further. If you're not familiar with JavaScript, don't worry - I've got you covered. Check out the HTML below: <div class="service-fields mb-3"> ...

The width of an HTML input and button elements do not match

Currently, I am facing an unusual issue where my input and button tags seem to have the same width assigned to them (width: 341.5px; calculated from $(window).width() / 4) in the code, but visually they appear to be of different widths. Here is a visual re ...

Setting up language negotiation in apache---How to set up language negotiation

In an effort to provide different files based on the user's preferred language setting in their browser, I have implemented the steps outlined here. Options +MultiViews AddLanguage de .de ...

Shift the inline form towards the right within the navigation bar

I have the code located at this link. How can I align the inline form to the right? I am using Bootstrap 4 and have tried using the float-right class, but it doesn't seem to be working. Is there something I am missing here? <!DOCTYPE html> < ...

AJAX request bypassed form validation

My form utilizes an AJAX call to submit information to Google Sheets, which works well until I try to incorporate form validation. At that point, only the AJAX call seems to run. Below is the HTML Form: <!DOCTYPE html> <html class="no-js" lang=" ...

`Incompatibility with background-position on y-axis in Mozilla Firefox`

I am having trouble with this code. It appears to work well in Chrome, but not in Mozilla Firefox. Could it be that this property is not supported in Firefox? Is there an alternative solution? .background { background:url('icon.png') no-repeat ...

Is there a performance boost when using queue() and filter() together in jQuery?

I'm currently refactoring some Jquery code and I've heard that chaining can improve performance. Question: Does this also apply to chains involving queue() and filter()? For instance, here is the un-chained version: var self = this, co = ...

Fixing the issue of a div exceeding the height of its parent div in Tailwindcss

One issue I'm encountering is with my card element, which has a fixed height of h-80 in Tailwind. Typically, I use this card within a grid. However, I recently added a div inside the card that is larger than its parent div, and I want it to scroll ve ...

Issue with click event firing multiple times following history.back() in Phonegap

In my PhoneGap project, I have two HTML pages: index.html and second.html. All of my scripts are placed in the head section. On the index.html page, all my jQuery and Ajax codes are included. Here is a snippet of the code used to navigate between pages: I ...

Obtain Image in PNG Format using an AJAX Call

I received an Image/PNG from my python service, and it looks like the following: PNG IHDRX,(ã=¤tEXtSoftwareAdobe ImageReadyqÉe< iTXtXML:com.adobe.xmp<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:met ...

Revamping repetitive JavaScript code for the pagination of Instagram Stories

I'm currently developing a website that utilizes fullpage.js and includes a section with multiple slides. These slides automatically transition every 10 seconds. I wanted to implement progress bars similar to those on Instagram, where each bar fills ...

Retrieving form output number using JQuery

Hello, I'm new to this site and have no prior experience with javascript coding. Here is what I have in my javascript file: $(document).ready(function() { $("input").click(function(event) { updateTotal(); }); }); function updateTotal() { var to ...