Ways to assign unique css margin-top values to individual children within a list

I am working with the code below:

function calculateImageMargins(elementId) {
 var ids = elementId;
 var totalWidth = 0
 var totalHeight = 0;
 ids.children().each(function(index){
     var imgWidth = ids.children().width();
     var imgHeight = ids.children().height();
     totalWidth += imgWidth;
     totalHeight += imgHeight;
     var leftMargin = imgWidth - totalWidth;
     var topMargin = imgHeight - totalHeight;
     ids.children(index).css('margin-left',leftMargin);
     ids.children(index).css('margin-top',topMargin);
   });
}
var objLi = jquery("li", this);
calculateImageMargins(objLi);

An example of my HTML structure is as follows:

<div id="imgrt">
<ul id="if">
   <li><img src="../pictures/album/20-c-44.jpg" /></li>
   <li><img src="../pictures/album/20-c-44.jpg" /></li>
</ul>
</div>

I do not assign specific IDs or classes to each element. I am open to suggestions on whether that would be beneficial. Without utilizing separate identifiers, how can I apply unique margin-left and margin-top values to each <li> and <img> within the <ul>?

This might result in overlap between the images.

The CSS for <ul> includes:

#if { left: 0%; top: 0%; margin: 5px; padding: 5px; position: relative; width: 400px; height: 400px; background-color: #101010; z-index: 30; }

Answer №1

Instead of utilizing ids.children(), opt for $(this). When inside the each loop, $(this) will provide you with the current item.

http://jsfiddle.net/nvnTW/

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

Implement CSS styles in Pelican without the need to develop a custom theme

I am quite fond of the default Pelican theme called notmyidea. It suits my needs perfectly. However, I am facing a challenge in incorporating the Python-Markdown admonishments extension as it requires adding some css rules to the template. I have searche ...

Populate the jsrender template with initial data

Is there a way to display a select option within a table? $('select').append($('<option>').text('single')); $('table').append($('#rowTmpl').render({name:'peter'})); th,td {border:1px soli ...

"Utilize Bootstrap's responsive 3-column grid layout for a sleek design, complete with hidden col-12 elements below each grid

Looking to create a 100% width column below an element within a column of a bootstrap grid. For better understanding, here is what I'm aiming for: https://i.stack.imgur.com/pl1Fi.png On selecting one of the images (from 1 to x), a hidden div with di ...

Is Ajax still a popular choice for developing web applications?

Currently, I am developing a comprehensive ajax-based web application and have been investigating various frameworks for SEO optimization and history tracking. During my research, I came across an interesting framework at Although it seems promising, I no ...

Exploring locations using the Google Geolocation API

I have a website and am trying to determine the location of my visitors. I came across this code on Google Code: <script type="text/javascript" src="gears_init.js"></script> <script type="text/javascript"> var geo = google.gears.factory ...

Troubleshooting with mpdf reveals compatibility issues with Bootstrap

I'm encountering an issue where Bootstrap isn't functioning correctly in mpdf. Is there a solution to address this problem? I require all the content to be displayed on a single page, similar to how it appears in the preview. Here is the HTML co ...

Check if the element with the ID "products" exists on the page using JQuery. If it does, then execute a certain action

Is there a way to create a conditional statement that executes if the element with the ID "products" is present in the HTML code? Thanks in advance for your help! Pseudo code using JavaScript (JQuery) if ( $('#products').length ) { // do s ...

Could you please clarify how to trigger the code below the layout when a data Gridview row is double clicked?

Although the HTML CODE concept can work, I am looking to apply it on my gridview table after data is binded. $(document).ready(function () { $('.clickme').click(function() { alert("Hello World"); }); }); <t ...

Fetch and display ajax response content and featured image on Wordpress post editing screen

Feeling stuck once again, I am currently working on customizing the Wordpress Post edit screen where I want to populate post content and the featured image from an Ajax Response. My attempts so far have been unsuccessful due to the Content Editor's us ...

Eliminate the shadow or background glow effect from the data labels in highcharts?

Can someone help me with my Highcharts issue? On my chart, the red labels have a white glow behind them in Chrome and Firefox. How can I get rid of that glow or change it to blue so it blends in better? I've tried using various properties like shadow ...

Organizing data by year and semester for display in Laravel Blade

I am working on organizing and printing data based on school year and semester in Laravel. In MS Access, I can achieve this using relationship and group reports, but I'm facing challenges in implementing it in Laravel. Below are some sample records: ...

The custom selection feature lacks compatibility across various web browsers

I was in need of a custom select box so I implemented the following : html : <div class="select"> <select name="Step_01_Roof_Width" onChange="GetWidth();" class="Step_01_SelectBox" id="Step_01_Select_Width"> </div> css : .select s ...

Increasing the height of list items

I'm currently working on creating a scale using list items, and I want the height of each item to increase incrementally. So far, I haven't been able to find a solution other than adding a class to each item. Here's a fiddle demonstrating t ...

Disappearing Jquery Datepicker icons when hovering over them

I am currently utilizing a jquery date picker that includes two icons on top (next, previous). However, I've noticed that when I hover over these icons, they disappear. https://i.sstatic.net/zsCQy.png Upon further investigation, it appears that when ...

The overflow of CSS text selection color spills beyond the boundaries of the box

I'm just starting to learn CSS and was wondering if there is a way to prevent the text selection color from extending into the 'gutter' (I believe that's the correct term) like shown here: It may seem trivial, but I've observed th ...

Displaying a Bootstrap loading spinner on the right side of a list item

I want to position a Bootstrap loading spinner on the right side of a ui li, with the text "Keywords" on the left side of the ui li. My current HTML code is as follows: <ul class="list-group" id="my-list"> <li class="l ...

What is the best method for deleting an uploaded image from a box?

My code is quite lengthy, so I'll just showcase the JavaScript portion here. Here is a snippet of my JavaScript code: <script type="text/javascript"> let current = 0; for(let i = 0; i < 5; i++) { $('#thumbnail-view- ...

How can I keep a collapsible navbar from wrapping underneath an image?

I am having difficulty preventing the collapsed navbar from wrapping under the image when the resolution drops below 476px. I attempted to use d-sm-inline-block in my <nav> tag but it still wraps. I have included an image demonstrating the current b ...

What is the best way to seamlessly integrate a Bootstrap Navigation and Carousel within a 100vh space?

I'm having trouble getting the bootstrap navigation and carousel to fit perfectly on the screen without any overflow. My goal is to have both elements occupy 100vh seamlessly, with no scrolling necessary. My first attempt was adding img-fluid: 100vh; ...

Adjust remaining vertical space

Trying to design a TailwindCss layout that resembles the following: +----------------------+ | | +-----+----------------+ | | | | | | | | | | | | +-----+------ ...