adjusting the size of buttons depending on the quantity of items displayed

Currently in my Angular application, I am working on rendering buttons within the view using the *ngFor directive. My goal is to have the rendered buttons appear side by side. While I have achieved this, I now want to dynamically resize each button based on the total number of buttons being rendered.

Here is what I aim for with button widths:

  • If there are 4 buttons, each should take up 25% width
  • If there are 2 buttons, each should occupy 50% width
  • If only 1 button exists, it should span the entire 100% width

I require guidance on how to implement this functionality efficiently. Any suggestions would be greatly appreciated.

Below is a snippet from my HTML and CSS files:

<nb-card>
<div class="col-md-12">
  <div class="col-md-3 pt-5" *ngFor="let data of TestData">
    <nb-card class="testCard">
      <input type="text" class="form-control" [value]="data.test[0]" readonly>
        <div>
            <input type="button" *ngFor="let ts of data.test[1]"  class="form-control testButton" (click)="onClick(ts.id)" [value]="ts.foo" readonly>
        </div>
    </nb-card>
  </div>
</div>
</nb-card>
.testButton {
display: inline-block;
width: 25%;
/* By specifying width as 25%, the buttons render side by side properly. */
}

Answer №1

In case flexbox is not an option:

<input type="button" [style.width.%]="100/data.test[1].length">

Answer №2

If you're looking for a more elegant approach, consider using flexbox. However, if Angular is your preference, utilizing the ngStyle directive is recommended:

<nb-card>
  <div class="col-md-12">
    <div class="col-md-3 pt-5" *ngFor="let data of TestData">
      <nb-card class="testCard">
        <input type="text" class="form-control" [value]="data.test[0]" readonly>
          <div>
              <input type="button" *ngFor="let ts of data.test[1]"  class="form-control testButton" (click)="onClick(ts.id)" [value]="ts.foo" readonly
  [ngStyle]="{'width':(100 / data.test[1].length) + '%'}">
          </div>
      </nb-card>
    </div>
  </div>
  </nb-card>

The ngStyle directive empowers you to dynamically calculate the desired width within the DOM based on your data.

Answer №3

To achieve the desired layout on the button container, you can leverage the power of flexbox in CSS. It's as simple as adding the following code snippet:

input[type='text'] div {
    display: flex;
}

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

Jquery not functioning properly for show and hide feature

I'm new to using Jquery and JqueryUI. I have a div named front, which I want to initially display on window load and then hide it by sliding after a delay of 5500 milliseconds. However, I'm encountering errors in the jquery.min.js file. The HTML ...

Two methods to access req.body on Node.js

I have encountered a question or issue while making a request using the put method. When I use the following code: userFactory.editProduct = function(id) { return $http.put('/api/editProduct', id) } and router.put('/editProduc ...

jquery quicksearch component for searching is malfunctioning

Utilizing a jquery plugin known as quicksearch to filter a list of comments. Below is an excerpt from the markup: <ol class="commentlist"> <li class="comment byuser comment-author-admin bypostauthor even thread-even depth-1" id="li-com ...

Type Error: Issue encountered while resolving module specifier

Upon trying to import the d3.js library in a project that utilizes npm, I encountered the error message: TypeError: Error resolving module specifier: d3. This issue specifically occurred while using Firefox. index.html <!DOCTYPE html> <html lang ...

Tips for resolving the npm glob-parent dilemma

Vulnerability in glob-parent <5.1.2 Detected Severity Level: moderate Description: Regular expression denial of service - More information at https://npmjs.com/advisories/1751 Resolution: fix available through `npm audit fix` Affected Package: node_modu ...

The Angular model does not automatically refresh when the Space or Enter key is pressed

Having an issue with my editable div and the ng-trim attribute. Even though I have set ng-trim to false, pressing SPACE or ENTER does not increment the string length by one in the div below. Using Angular 1.3.x and wondering if anyone has any ideas on how ...

The pagingParamsProvider is not recognized in the ResultAnalysisDetailController, which is causing an error

My generated controller is causing an issue with the pagingParams injection. Although other jhipster controllers work fine, mine throws an error for unknown provider. (function() { 'use strict'; angular .module('netCopOnlin ...

Utilizing the jQuery setInterval method

I am currently working on a jQuery function that flips an element and then turns it back after a few seconds. The issue I am facing is that there are multiple elements with the same "flip" tag, and when one element is clicked, all elements flip back after ...

having difficulty accessing the value within the Angular constructor

My current issue arises when I click on a button and set a value within the button click method. Despite declaring the variable in the constructor, I am unable to retrieve that value. The code snippet below demonstrates this problem as I keep getting &apos ...

Getting Creative with Jquery Custombox: Embracing the 404

Encountering a problem with jquery custombox version 1.13 <script src="scripts/jquery.custombox.js"></script> <script> $(function () { $('#show').on('click', function ( e ) { $.fn.custombox( this, { ...

Anticipating the arrival of data from an unspecified child component prior to executing the effect

Suppose there is a parent component: function Parent({ children }) { useEffet(() => { // Perform action only after all children have returned their values via props (or context) }) return <div>{ children }</div> } How do I ensure ...

PHP website freezes unexpectedly in Firefox version 4

For over a year, our website has been functioning perfectly. However, with the release of Firefox4, we have noticed a new issue. Occasionally, the page hangs randomly. I have tested the website on IE8/9, Chrome10+, Safari, and Opera, and the issue only see ...

The comparison between local variables and data can result in a significant drop in performance

My current project involves VueJS and Cesium, but I'm facing a performance issue with a significant drop in frame rate. While I have identified the problem area, I am unsure of why this is happening and how to resolve it. export default { name: ...

An unexpected runtime error occurred due to a SyntaxError: the JSON input abruptly ended during the authentication process with the next-auth module

Encountering an Unhandled Runtime Error SyntaxError: Unexpected end of JSON input when trying to SignIn or SignOut with authentication credentials. The error is puzzling as it displays the popup error message, but still manages to register the token and s ...

Positioning an HTML div

Currently, I am diving into the world of HTML and have included a relative div in my code. Although there are two additional divs positioned before it, they do not overlap; unfortunately, the first two still occupy space above the relative one. My desire ...

Tips for accessing the next sequential tag that is similar in HTML with the help of jQuery

I have generated the following HTML code from some plugins. <div class="parent"> <span>item1</span> <input type="hidden"></input> <span>item2</span> <span class="active">item3</span> <inpu ...

Customizing the direction of Ext.menu.Menu involves modifying the menu's orientation to

I am looking for a solution to make Ext.menu.Menu display horizontally on the first level, with the submenu (second level) displaying vertically, and then the subsubmenu (third level) back to horizontal... My goal is to have odd-level menus display horizo ...

Having issues with -ms-filter not functioning in IE while using Angular 4

Having trouble with filters not functioning in IE browser when using angular 4. Below is the CSS code snippet: .style{-ms-filter: grayscale(50%); -ms-filter: brightness(70%);} ...

Transferring an array of objects between routes

I am currently working with Node.js and Express.js, attempting to pass an array of objects from one route to another. I initially attempted passing it through the QueryString, but encountered issues with the typeof identifying it as an object rather than f ...

Subpage issue with Bootstrap dropdown menu functionality

I have utilized bootstrap to create a dropdown menu. The primary item is a link that functions properly on the homepage, but not on the subpage. Check out my code snippet here: <li> <a href="/preview/#design-centre" class="dropdown-toggle sc ...