Tips for creating a new row in an Angular 2 template

Currently, I am delving into Angular 2. In my template, I have set up a code snippet to display data fetched from APIs:

<div class="blocks">
  <div router-active class="block" *ngFor="let item of items">
    <div class="inblock">
      <p class="title">{{ item.name }}:</p>
    </div>
  </div>
</div>

Everything is functioning properly. However, when one div's height exceeds others, it creates an uneven row like the image below:

https://i.sstatic.net/oHcQq.png

I envision having a neat row with only three divs each before starting a new block. Although I know how to achieve this normally, I am struggling to implement it in Angular 2!

UPDATE: I am not looking for a fixed height solution since the content length can vary. Therefore, applying a fixed height through CSS won't resolve the issue.

Answer №1

To resolve this issue, implement a CSS solution. You can refer to the following code snippet:

.item {
    float: left;
    width: 200px;
    margin: 15px;
    border: 2px solid #4598AC;  
}

.item:nth-child(4n+1){
  border: 1px dashed green;
  clear: both;
}

This code snippet utilizes the float property to display the items inline. By using nth-child selector, you can specify that every 4th item should clear the float.

Answer №2

Have you considered generating a custom 'css' file for your template and linking it to the styleUrls property? By defining a class in this CSS, you have the freedom to style the div with any properties you desire and then apply that class name to your elements.

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

CSS descendant selector add style excluding child elements

My issue involves two divs each containing a table. When I apply the CSS class "table" to the first div, the second table in the div is impacted as well. .tbl-generic th:nth-child(-n+2),td:nth-child(-n+2) { background-color:red; width:25px; } ...

Dragging in JQuery-UI at the current mouse position

Here is the code snippet I have: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Draggable - Default func ...

Utilize JavaScript libraries in a TypeScript project

Looking to integrate a payment system called iyzico into my project. The iyzico payment library can be found here. However, there are no types available for it. How can I utilize this library in my Node.js TypeScript Express backend project? I attempted t ...

Background Services in the Moment

Currently in the process of developing a mobile application with the Ionic framework that utilizes Laravel 4 REST API for CRUD operations on a MySQL database. As per the requirements of the app, it needs to constantly communicate with the backend service t ...

"Utilizing jQuery to enable smooth scrolling of entire windows with every twist of the mouse, similar to the

Does anyone have tips on how to achieve a scrolling effect that covers 100% of the window, similar to what is seen on this website? My website currently has vertical scrolling, with each div set to 100% width and height. I have implemented an anchor syste ...

What are the steps to set up auto-building with create-react-app?

I've been utilizing create-react-app for some time now. Autoreloading with 'npm start' or 'yarn start' has been working well on its own, but now I'm facing another issue. Currently, I am running the app on an Express server th ...

The browser encountered an issue trying to load a resource from a directory with multiple nested levels

I've stumbled upon an unusual issue. Here is a snapshot from the inspector tools. The browser is unable to load certain resources even though they do exist. When I try to access the URL in another tab, the resource loads successfully. URLs in the i ...

Ongoing issue: iOS Safari failing to trigger storage events

Two tabs are open in iOS Safari, both directed to the same page. Each tab has an event listener for the storage event on the window object, and localstorage is updated when the client interacts by clicking/tapping/typing. In Internet Explorer, the inactiv ...

Using Angular Ionic 3 to apply the disabled attribute

I have a situation in my main.ts where I need to disable a textarea in the HTML if an object is initialized. I've attempted various methods like: ng-attr-disabled="!myObj"; ng-attr-disabled="{myObj!= null}"; and also directly using ng-disabled. I e ...

Tips for including ngScenario in your end-to-end testing setup

Can anyone provide guidance on how to utilize ngScenario for testing NG scripts? I am struggling to figure out the loading process... In addition to simply loading the JS file with the <script> tag, what steps are necessary to ensure it functions co ...

Utilizing Translation (i18n) in AngularJS Controller to Implement Popups

I'm currently working on an AngularJS app and need to implement i18n. Everything is running smoothly except for error handling, specifically with utilizing translations in controller for popups. Here is a snippet of my controller: function showError ...

The functionality allows users to scan multiple barcodes simultaneously and will display the results on the screen once the scanning is complete and save the results for future reference

I'm currently developing a mobile barcode scanning app using Ionic 3, targeting Android and iOS devices. However, I've encountered a roadblock. Originally, the app was designed to scan and display one product barcode. Now, a new requirement has b ...

Utilize the full width available to create a flexible div element without any fixed sizes

I have come across similar questions, but none of them quite addressed my specific situation. My dilemma involves a tabbed navigation that can vary in size depending on the number of tabs added to it. Adjacent to the navigation, there is a second area hous ...

"Creating a dynamic dropdown menu and sub-menu in PHP using MySQLi with unique slug values

I need to generate a dynamic menu from the database, including main menus and submenus. For instance: <li class="<?= ($pg == 'departments') ? 'active':''; ?>"> <a href="departments">Departments</a> ...

Add a .handlebars or .hbs file to an HTML document

When visiting the emberjs.com homepage, you will find an example of a todo list using Ember.js and Handlebars.js. Within the todo list, there is an extension for a .hbs file. I am curious - what exactly is a .hbs file? And how can I include a .hbs script ...

Sometimes jQuery may require multiple executions with just one click

Content of index.php <script type="text/javascript" src="//<?php echo $_SERVER["SERVER_NAME"];?>/javascript/jquery-1.10.2.min.js" ></script> <script type="text/javascript"> $(document).ready(function() { $( document ).on( 'c ...

How to delete an element from an array with UnderscoreJS

Here's a scenario: var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]; I'm looking to delete the element with id value of 3 from this array. Is there a method to achieve this without using splice? Perhap ...

Can you point me in the direction of some resources for developing a custom plugin with stylelint?

After visiting the stylelint website and github, I downloaded it locally using npm. The stylelint website suggests using the following format to create my own plugin: var myPluginRule = stylelint.createPlugin(ruleName, function(primaryOption, secondaryOpt ...

Executing the onSuccess callback in Ajax without any ability to manipulate the ajax requests

My dilemma lies in needing to execute a JavaScript function upon the successful completion of an AJAX call. Unfortunately, I am unable to directly manage the AJAX calls as they are handled by the DNN5 framework. Is there a way for me to trigger my functio ...

Using regular expressions in JavaScript, we can separate a paragraph into individual sentences

Hey there! I'm currently working on a project that requires extracting sentences from a paragraph using regex in JavaScript. Unfortunately, my attempts to achieve this have resulted in syntax errors as I have tried methods used in other programming la ...