Shifts on the list are adjusted when placing an item

I'm struggling to resolve an issue where my list, #cardHolder, moves down when I drag an item within it. It seems to be a CSS-related problem that I can't figure out on my own. Any assistance would be greatly appreciated.

$(document).ready(function () {
    $( "#cardHolder, #hand" ).sortable({
      connectWith: ".connectedSortable",
      receive: function(event, ui) {
      var $this = $(this);
      if ($this.children('li').length > 1 && $this.attr('id') != "hand") {
      console.log('Only one per list!');
      $(ui.sender).sortable('cancel');
            }
      }

    }).disableSelection();
});
#trumpHolder{
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    margin-top: 5px;
    margin-bottom: 5px;
    margin-left: auto;
    margin-right: auto;

}
#holderWrapper{
    width: 100%;
    text-align: center;

}
#cardHolder {
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    margin: 5px 5px 5px 5px;
    display: inline-block;
}
#cardHolderOther{
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    display: inline-block;
    margin: 5px 5px 5px 5px;
}
.table{
    display: table;
    margin: 0 auto;


}
#hand {
    height: 250px;
    border: 5px solid #ccc;
    padding: 0;
    width: 100%;
    clear: both;
    margin-top: 5px;

}

#cardHolder li, #hand li {
    margin: 7px 7px 7px 7px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
    display: inline-block;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.24/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.8.24/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="boerenbridge.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="boerenbridge.css">


<ul id="trumpHolder" class="connectedSortable">

</ul>
<div id="holderWrapper">
<ul id="cardHolder" class="connectedSortable">

</ul>
<ul id="cardHolderOther" class="connectedSortable">

</ul>
</div>
<div class="table">
<ul id="hand" class="connectedSortable">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>
</div>

The provided code is as follows:

HTML

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Boerenbridge</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.24/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.8.24/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="boerenbridge.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="boerenbridge.css">
</head>
<body>
<ul id="trumpHolder" class="connectedSortable">

</ul>
<div id="holderWrapper">
<ul id="cardHolder" class="connectedSortable">

</ul>
<ul id="cardHolderOther" class="connectedSortable">

</ul>
</div>
<div class="table">
<ul id="hand" class="connectedSortable">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>
</div>
</body>

CSS

#trumpHolder{
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    margin-top: 5px;
    margin-bottom: 5px;
    margin-left: auto;
    margin-right: auto;

}
#holderWrapper{
    width: 100%;
    text-align: center;

}
#cardHolder {
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    margin: 5px 5px 5px 5px;
    display: inline-block;
}
#cardHolderOther{
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    display: inline-block;
    margin: 5px 5px 5px 5px;
}
.table{
    display: table;
    margin: 0 auto;


}
#hand {
    height: 250px;
    border: 5px solid #ccc;
    padding: 0;
    width: 100%;
    clear: both;
    margin-top: 5px;

}

#cardHolder li, #hand li {
    margin: 7px 7px 7px 7px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
    display: inline-block;
}

Javascript/Jquery

$(document).ready(function () {
    $( "#cardHolder, #hand" ).sortable({
      connectWith: ".connectedSortable",
      receive: function(event, ui) {
      var $this = $(this);
      if ($this.children('li').length > 1 && $this.attr('id') != "hand") {
      console.log('Only one per list!');
      $(ui.sender).sortable('cancel');
            }
      }

    }).disableSelection();
});

Answer №1

For proper alignment when using display: inline-block with #cardHolder, it is important to add vertical-align: top;. This ensures that the element sticks to the top instead of aligning to its default value, which is baseline. Check out this link for more information on vertical-align and inline elements.

Below, you can see examples comparing the default baseline value alignment on the left and the top value alignment on the right.

.inline-list {
  width: 250px;
  float: left;
}

.inline-list div {
  display: inline-block;
  border: 3px solid #444;
  margin: 0; 
  padding: 0;
}

.inline-list .cardholder {
  width: 100px;
  height: 100px;
}

.inline-list.align .cardholder {
  vertical-align: top;
}
<div class="inline-list">
    <div class="cardholder">Some text</div>
    <div class="cardholder"></div>
  </div>

  <div class="inline-list align">
    <div class="cardholder">Some text</div>
    <div class="cardholder"></div>
  </div>

Answer №2

To include in your stylesheet, simply:

#cardHolder {
    vertical-align : top;
    ...

Check out the example in action below!

$(document).ready(function () {
    $( "#cardHolder, #hand" ).sortable({
      connectWith: ".connectedSortable",
      receive: function(event, ui) {
      var $this = $(this);
      if ($this.children('li').length > 1 && $this.attr('id') != "hand") {
      console.log('Only one per list!');
      $(ui.sender).sortable('cancel');
            }
      }

    }).disableSelection();
});
#trumpHolder{
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    margin-top: 5px;
    margin-bottom: 5px;
    margin-left: auto;
    margin-right: auto;

}
#holderWrapper{
    width: 100%;
    text-align: center;

}
#cardHolder {

    vertical-align : top; /* <--------------- MODIFICATION */

    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    margin: 5px 5px 5px 5px;
    display: inline-block;
}
#cardHolderOther{
    border: 5px solid #ccc;
    padding: 5px;
    height: 250px;
    width: 150px;
    display: inline-block;
    margin: 5px 5px 5px 5px;
}
.table{
    display: table;
    margin: 0 auto;


}
#hand {
    height: 250px;
    border: 5px solid #ccc;
    padding: 0;
    width: 100%;
    clear: both;
    margin-top: 5px;

}

#cardHolder li, #hand li {
    margin: 7px 7px 7px 7px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
    display: inline-block;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.24/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.8.24/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="boerenbridge.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="boerenbridge.css">


<ul id="trumpHolder" class="connectedSortable">

</ul>
<div id="holderWrapper">
<ul id="cardHolder" class="connectedSortable">

</ul>
<ul id="cardHolderOther" class="connectedSortable">

</ul>
</div>
<div class="table">
<ul id="hand" class="connectedSortable">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>
</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

Is it possible to include an argument in an unnamed function in Node.js without assigning it to a variable?

Coming from a C/C++ background, I've been struggling with grasping the syntax of node.js. After searching online for code examples to explain blocking and non-blocking operations, I stumbled upon a piece that has left me perplexed for hours. Despite m ...

Django Website Experiencing Issues with Google Analytics Integration

I implemented google analytics by inserting the tracking script tag and code at the bottom of the head section in my base.html template which serves as the foundation for all other pages. Additionally, I set up 2 click events to track when users click on ...

What method is most effective for combining two JSON files in Angular?

My data includes a json file with a product list that looks like this: [{"id":76, "name":"A", "description":"abc", "price":199, "imageUrl":"image.jpg", "productCategory":[{ "categoryId":5, "category":null },{ "categoryId":6, " ...

Error encountered while building Sockets.io: Fatal error c1083. Seeking guidance for resolution

I'm currently trying to unpack a nodejs module called socket.io. Initially, I encountered a build error prompting me to install Visual Studio 2005 and .NET Framework 2 SDK, which I did. However, I am now facing a different error without any suggestion ...

Explore the input field to find relevant information, triggering an Ajax PHP call that displays search results in a convenient dropdown

Include an input box that displays a dropdown list of customer names when text is typed by the user <script> function displayCustomerList() { $.ajax({ url: "customerlist.php", success: function(result) { ...

Events are failing to appear in their correct positions on the screen

Software Version - "react-big-calendar": "^0.28.2" https://i.sstatic.net/nU4K6.png- Attached is a screenshot for reference. Please see below React code snippet: <Calendar defaultDate={moment().toDate()} defaultVie ...

How can I utilize Luxon to calculate the total number of days that are equal to or greater than 30?

Looking at my current array structure const arr = [ { id: '1', name: 'thing1', createdAt: '2022-09-21T16:26:02Z', }, { id: '2', name: 'thing1', createdAt: '2022-11-21T16:20:20Z', } ...

Using Backbone.js, the append method replaces the existing HTML content instead of simply adding an

As a beginner in backbone.js, I decided to start building a small todo application using examples from the "Backbone fundamentals" by Addy Yosmani. However, I encountered an issue with my code where instead of appending each item to the list view, it repla ...

Error: Jasmine test is encountering an undefined controller

karma.config.js: module.exports = function(config) { config.set({ basePath: '', frameworks: ['jasmine'], files: [ 'node_modules/angular/angular.min.js', 'node_modules/angular-mocks/angular-mocks ...

The functionality of the image click feature becomes disabled when resizing and switching to tab view

I have added header icons for home and user. When clicked on the user image in desktop view, it should redirect to the respective page. However, when I minimize it in tab or mobile view, nothing is displayed. This issue only occurs with the user image an ...

The function is not being triggered, should I include a window.load event?

Seeking to modify the CSS styles of an element upon click. ryan.js function ryanClicked(id){ document.getElementById(id).style.color = "blue"; alert("Asdsa"); } product.html <head> <script src="ryan.js"></script> </head ...

Struggling to line up text and image next to each other?

I have inserted an image and some text. The text consists of 1 <h6> tag and 2 <p> tags. I attempted to use the float:left property, but the content did not display side by side as intended. Here is the code from content.js: <div className ...

What is the reason for the additional line being generated?

Can anyone explain why there is an extra line being produced here? I came across another question where it was mentioned that this issue is not due to CSS but rather caused by HTML. Why is that? This is completely new to me, and I'm curious as to wh ...

Manipulating Vue.js data within HREF attributes using PHP

Does anyone know how to successfully pass the data from Vue into PHP? I've attempted a few methods, but when I try to use the resulting link in the href attribute (href = $ where $ name), it shows a strange string instead of the expected URL from the ...

Tips for eliminating a worldwide ajax event handler using jQuery

Although it may not be the most advanced question (I am a beginner in JQuery), I am facing an issue: Whenever a specific button is clicked on my website, I must add a function that registers a global ajax event handler (.ajaxSend to be precise) and verifi ...

Creating space between columns without the use of left and right gutters

I needed to adjust the spacing between columns, but the solution I found caused an overflow issue. I want to fix this overflow problem without using overflow: hidden because it conflicts with other components: .row { margin: 0 -25px; } .col { paddi ...

Is the CSS class turned off?

When pulling avatar images for my vBulletin forum, the code used is as follows: <img src="{vb:raw post.avatarurl}" alt="{vb:rawphrase xs_avatar, {vb:raw post.username}}" title="{vb:rawphrase xs_avatar, {vb:raw post.username}}" class="forumavatar1" /> ...

Combining two objects with JQuery while preserving all their properties

I am attempting to combine/merge two objects together. Currently, I have the following: var mergeObj = {}; var obj1 = { name: 'abd', id: 5454756, color: 'red' } var obj2 = { name: 'abc1', id:387589, color: 'blue'} I ...

retrieve the value obtained from a promise in an outer scope

I need a simple function that returns the name. Here's my existing code snippet: getName(entity, id) { const promise = userServices.getName(entity, id).then((data) => { return data; }); / ...

Storing data using angular-file-upload

In my application, I am utilizing the "angular-file-upload" library to save a file. Here is the code snippet that I am using: $scope.submitForm = function(valid, commit, file) { file.upload = Upload.upload({ url: '/tmp', data ...