Is there a way to fix the positioning error that occurs when using Jquery-ui Sortable with elements of different heights?

I currently have a working jquery-ui sortable function that can handle elements of different heights and widths. However, I am facing an issue with its positioning as demonstrated in the following example:

http://jsfiddle.net/N52GP/7/

Here is how it should ideally appear:

https://i.sstatic.net/OydUg.jpg

I believe this problem stems from a CSS floating issue, and despite hours of testing and searching, I have not been able to find a suitable solution. Even setting Box 4 to float:right does not resolve the alignment with Box 3.

You can view the HTML / CSS code causing the floating problem below:

<!DOCTYPE html>
<html>
<body>

    <style>
        .portlet {
            float: left;
            margin: 5px;
            background-color: #b5b5b5;
        }

        #box1, #box4 {
            width: 150px;
            height: 220px;
        }

        #box2, #box3 {
            width: 250px;
            height: 100px;
        }
    </style>

    <div style="float: left; width: 600px; padding: 10px;">

        <div id="box1" class="portlet">
            <div class="portlet-header">Box 1</div>
            <div class="portlet-content">Box Desc</div>
        </div>

        <div id="box2" class="portlet">
            <div class="portlet-header">Box 2</div>
            <div class="portlet-content">Box Desc</div>
        </div>

        <div id="box3" class="portlet">
            <div class="portlet-header">Box 3</div>
            <div class="portlet-content">Box Desc</div>
        </div>

        <div id="box4" class="portlet">
            <div class="portlet-header">Box 4</div>
            <div class="portlet-content">Box Desc</div>
        </div>

    </div>

</body>
</html>

Are there any possible solutions or workarounds for this layout challenge?

Answer №1

The div containers are not aligning properly next to each other inside the #sortable element.

To fix this issue, you should increase the width of the container and if you insist on using floats, then make sure to enclose both #box2 and #box3 in a separate div.

Here is a solution:

<div id="middle">
    <div id="box2" class="portlet">
        <div class="portlet-header">Box 2</div>
        <div class="portlet-content">Description of Box 2</div>
    </div>

    <div id="box3" class="portlet">
        <div class="portlet-header">Box 3</div>
        <div class="portlet-content">Description of Box 3</div>
    </div>
</div>


#middle {
    float:left; 
    width:258px;
}

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

Utilizing the <style scoped> feature within my Angular template

When adding CSS styles in the specified htm file's templateUrl while loading a directive, I wonder if this is a bad idea. Will it be repeated every time the template is instantiated on the rendered page, or does Angular handle this differently? By usi ...

Extract the URL of the background image from a preexisting <div> element, or append a new attribute to the background-image property

I am facing a challenge in updating the existing background-image with a linear gradient. Currently, my CMS is generating a background-image, but I am unable to adjust the opacity using a linear gradient. My goal is to darken the background-image using CSS ...

Ajax Loading Bar similar to YouTube is not functioning as expected

I've implemented the YouTube-like Ajax Loading Bar to load a PHP file. Here is the JavaScript code I'm using: $(".ajax-call").loadingbar({ target: "#loadingbar-frame", replaceURL: false, direction: "right", async: true, complete: fun ...

Creating dynamic transformations and animations for characters and words within a paragraph in 3D

Looking to add animation effects to specific parts of a paragraph, but transforming the entire box instead. Remembering seeing a solution on StackOverflow before, now regretting not saving it. Spent over an hour searching for a similar answer without succ ...

Creating a condensed version of the process: Utilizing jQuery for thumbnail preview on hover to create an image slideshow

I've created a video preview slideshow function using jQuery. As a newcomer to jQuery, I'm questioning if there's a more efficient way to achieve this. Having separate timers for each frame feels cumbersome and limits the flexibility in chan ...

Is it possible to redirect the client to a different HTML file after they input a number?

Following a submission of numerical input, I am looking to navigate from the index.html file to another HTML file or similar destination. Could someone provide assistance? Below is the code I have written: <!DOCTYPE html> <html lang="en&quo ...

Alter the background of cards in real-time

I am unfamiliar with JavaScript, but I wish to utilize a function to alter the background color of multiple cards. function ChangeColorToGreen() { document.getElementById("ChangingCard").style.background = "green"; } function ChangeColorToPurple() { ...

Creating a CSS binding for width: a step-by-step guide

I have a complex layout with multiple elements in different divs that need to be aligned. Hardcoding the width or using JS on page load to specify values in pixels both seem like messy solutions. How can I achieve this without causing a zigzag effect? Her ...

Bing Map problem: p_elSource.attachEvent is throwing an error

I am encountering a significant issue with Bing Maps. The API link I am using is: Initially, I am seeing an error in firebug that looks like this: this.CreditsFor=function(a,i,j,h) { var e=[]; if(a!="undefined"&&a!=null&&typeof m_ta ...

Improve loading speed of thumbnail and full images in HTML

I'm struggling with slow loading images on my blog website. Can anyone help me figure out how to improve loading speed and: Is it possible to use a separate thumbnail image that is smaller in size instead of the full image for both thumbnails and th ...

jQuery enigma - struggling to get slideUp/slideDown to function

My goal is to utilize jQuery and CSS to display the initial paragraph out of a set of four, and insert a link or button at the conclusion of the first paragraph. Upon clicking on the link or button, my aim is to reveal the remaining three paragraphs in a s ...

Optimal placement and size for the slick slider

I am new to CSS and currently experimenting with the Slick slider on a project: My setup involves a div container that spans 100% of the width of the page. Inside this container, there is another div (housing the slider) that takes up 80% of the width. D ...

Leverage Arrays with Bootstrap SelectPicker in Javascript

I am looking to populate a Bootstrap SelectPicker with values from an array. I am unsure of how to loop through the array to extract the values. Currently, I have manually added values to the SelectPicker like this. <!DOCTYPE html> <html> < ...

Adding a block between two other blocks on a mobile device using Bootstrap 4

I am trying to achieve a specific layout on both desktop and mobile devices. On desktop, I want the layout to be structured as follows: [1][2] [3][2] The requirement is for column 2 to be the same height as columns 1 and 3 on desktop screens. For the mo ...

Tips for delivering a variable to a React Native Stylesheet

Is there a way to pass a variable to the "shadowColor" property in my stylesheet from an array declared in the code above? I keep encountering a "Can't find name" error. Attempting to use a template literal has not resolved the issue. Any assistance w ...

iOS experiences a lag in loading dynamic images

I have created a unique jquery carousel specifically designed for mobile devices. It features three containers that display three images at a time, with jquery handling the animation. By detecting the user's swipe direction (left or right), I update t ...

Switching Unicode icon when element is clicked

My form has two inputs - one for text input and the other for submitting, like a button. I have added an icon to the submit button and want it to change when clicked. <input class="searchBtn" id="submit" name="submit" type="submit" value="&#xf002"& ...

What impact do varying screen sizes have on the width of CSS?

Can someone explain what this CSS code actually does: .class { width: 800px; } I've noticed that on my laptop screen, the element appears to be exactly 800 pixels wide. However, when I view it on my tablet screen, it shows up as 1600 pixels wide. Th ...

How can the issue of the navbar color not being able to disappear or turn transparent be resolved, given its connection to the body color?

@import url('https://fonts.googleapis.com/css2?family=Bai+Jamjuree:wght@400;500;600;700&display=swap'); :root { --color-body: #b6cbce; --color-heading: #eef3db; --color-base: #033f47; --color-base2: #022a30; --color-brand ...

Getting all inline styles from an HTML string using JavaScript

(JavaScript) I am working with an html email template stored as a string. Is there a way to extract all the inline styles used in the template? For instance, if I have this input: <div style="min-width:300px;max-width:600px;overflow-wrap:break-word ...