The proper application of CSS to dynamically loaded elements in AngularJS is not functioning correctly

I recently discovered a great script from CodyHouse that allows me to create a filterable portfolio. You can check it out here:

Using AngularJS, I am able to parse data via JSON seamlessly. However, I have encountered an issue where the CSS is not being applied correctly to dynamically loaded elements.

An image showcasing the differences can be seen below:

As depicted in the image, the first row, which is loaded via Angular, seems to lack any applied CSS. On the other hand, the second row, already present in the HTML, displays correctly with the CSS rules.

This discrepancy is puzzling as both rows share the same CSS selectors and rules.

A Plunker demonstrating this issue can be found here: http://plnkr.co/edit/mFtMpm5CJOiPcu3tRVOj?p=preview

The following represents the markup generated by Angular:

<li class="mix color-1 check1 radio2 option3 ng-scope" style="display: inline-block;" ng-repeat="drawing in drawings"><img src="img/img-1.jpg" alt="Image 1"></li>

And below is the static html markup:

<li class="mix color-1 check1 radio2 option3" style="display: inline-block;"><img src="img/img-1.jpg" alt="Image 1"></li>

The CSS properties applied are as follows:

.cd-gallery ul { text-align: justify; }
.cd-gallery li { width: 23%; }

Despite no discernible difference between them, I am struggling to understand why the CSS isn't displaying properly on dynamic elements compared to static ones. This anomaly has left me scratching my head, as it's the first time I've encountered such behavior with CSS. Any insight or assistance would be greatly appreciated. Thanks in advance!

Answer №1

Aha, I've cracked the code and found the answer to the dilemma.

Essentially, the issue lies in the whitespace between the elements (which is necessary for proper justification) not being included in Angular's repetition. Therefore, you need to enclose the whitespace within a repeatable element, like so:

<span ng-repeat-start="drawing in drawings"></span>
    <li></li>
<span ng-repeat-end></span>

Credit goes to this discussion on Stack Overflow: Angular: Why doesn't CSS justification work with ng-repeat?

Admittedly, this workaround is not perfect as it entails adding excess markup, but hey, at least it gets the job done. If anyone has a more elegant solution, please do share!

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

The first-child and last-child selectors are not working properly in the CSS

In the family of elements, the first child should proudly show off the image icon for home while the last child remains humble without any background imagery: Check out the evidence here: http://jsfiddle.net/gUqC2/ The issue at hand is that the first chi ...

Issues with the parallax zooming out effect

Delving into the world of parallax effects, I'm on a quest to achieve a captivating zoom-out effect while scrolling. However, I'm encountering an issue where the image shrinks beyond the browser width and the div's height. In the example pr ...

Can data be presented in AngularJS without the use of scope variables?

I have a method: <span ng-init="getJobApplicantsList(jobId)">(number should be display here)</span> Is there a way to display the data without having to store it in a scope variable? I need to use this method in many places. This is my cont ...

What causes line-height to be overlooked in a span element but effective in a div element?

Take a look at this example: CSS: How to reduce line spacing of text? I've experimented with it, and I noticed that the CSS property line height is not very effective when applied to a span element. Why is this the case? Why does it only work properl ...

Placing an image on the chosen option of a <mat-select> using Angular Material

I have incorporated Angular Material into my Angular 2 project, and I am trying to insert a static image (HTML element) in the selected value of mat-select. Unfortunately, I have been unable to find a solution for this issue. Is there anyone who can ...

Display the file name of the following/preceding images on the backward/forward button

Hello, I am fairly new to web development and could use some professional assistance. I have a slideshow set up, but I am struggling with adding the filename of the next or previous images to the back/forward icons of the slideshow. Is this achievable with ...

Ensure that a bootstrap table's cell occupies the entire available space, while also allowing for scrolling when there is overflow

Many questions have been raised about Bootstrap, Tables, and responsiveness. However, I have yet to find one that addresses my specific issue with getting a particular cell (<td> element) to: (a) Fill the available space, stopping at the window boun ...

Utilizing the dnd library to incorporate drag and drop functionality

I've encountered an issue with the code snippet below. Although I am able to drag elements, I am unable to drop them. How can I trigger the dropFunction when a drop occurs? Drag code: <div> <a class="button" ng-class= ...

Guide to changing the size of an image window in CSS when the container's width falls below a specific threshold

I am struggling with a dilemma regarding an image that is 1920 pixels wide. My goal is to have it displayed centered and cropped in the browser window when its width exceeds 1024 pixels. In case the browser width is below 1024 pixels, I want the image to b ...

What is the best way to ensure the div expands to the width of the screen as it is being

What changes should I make to the CSS in order to adjust the div class="breadcrumbs" when the table width exceeds the screen length and the background does not extend all the way to the end? See the screenshot for reference. I want the dark blue bar to str ...

Transforming SQL data into JSON format for Treeview using C#

I have a stored procedure that retrieves data from a table. I am looking to transform it into the following format: data: [ { text: "Furniture", items: [ { text: "Tables & Chairs" }, ...

In Ruby on Rails, effortlessly passing JSON data from the index action while also rendering a separate application layout at the same time

Having issues with passing @vacancies to json while rendering another layout in my Vacancies controller. The current code isn't working as expected - json is not being passed even though I have specified "wide" layout. When I remove format.html { rend ...

Deleting a database entry with Angular

I am struggling to figure out how to delete a database record in Angular using my MVC controller. I've created a method for this purpose but I'm unsure of how to properly call it and pass the required id. [HttpPost] public static vo ...

The primefaces codeMirror is failing to properly load its own CSS and JavaScript files

I am experiencing an issue with codeMirror from primeface-extension when trying to use it with SQL syntax. Upon loading the page that contains this component, I encounter a 404 error where the Css and javascript components cannot be found. Despite having ...

Encountering issues with an Express Server when sending multiple AngularJS Post requests. Server crashes when attempting a second post request

Since running two post requests simultaneously is not feasible on my client, I attempted to execute the second post within the .then section of the first post. This method has been successful in my previous projects. However, I encountered an issue where m ...

Is Ionic capable of storing data similar to sharedpreferences?

Hey there, I'm just starting out with the ionic framework and could use some advice. What is the best way to store user data like usernames, passwords, or login keys? Would it be beneficial for me to utilize HTML5 local storage? I want users to be ab ...

Column taking up the entire width positioned on the left side

Is there a way to make a column expand to the full width on the right within a container? <div class="container"> <div class="row"> <div class="col-5"> Lorem ipsum </div> <div class="col-7 img-c ...

changing the name of a variable within ng-include

Here is some important HTML code: <ng-include src="'app/views/order.html'"></ng-include> Within the scope of this ng-include, there is a variable called trade. The structure of the trade variable is as follows: var trade = { or ...

AngularJS in Spring: A Dynamic Duo

While exploring the Spring website for an AngularJS-Spring application, I came across a blog post detailing the steps to create a secure single-page application. Following the instructions in the blog, I was able to successfully run the application. Howeve ...

Having trouble displaying an image in the background using CSS? Look no further than Laravel

After running the command php artisan storage:link, I am facing an issue where the image does not load. Upon inspection, it displays a message saying "Could not load the image". https://i.sstatic.net/KKFy9.png What adds to my confusion is that ...