Easily showcase a limitless number of items within a 10-column grid using Bootstrap!

This is the code snippet:

<div *ngFor="let minute of state.minutes.specificMinutes.selectedMinutes | keyvalue" class="col-sm-1 checkbox-container">
   <div class="custom-control custom-checkbox">
     <input type="checkbox" (click)="state.minutes.subTab='specificMinutes'"
            class="custom-control-input" id="minute-{{minute.key}}-checkbox"">
     <label class="custom-control-label" for="minute-{{minute.key}}-checkbox">{{minute.key}}</label>
   </div>
</div>

I want to fit only 10 custom-controls in a row, but since I am using col-sm-1, I end up with 12. Is there a way to ensure exactly 10 custom-controls per row without having a decimal value like col-sm-1.2? How can this be accomplished?

The number of items in the array exceeds 10, and the exact count is not known beforehand.

Answer №1

To evenly distribute space among 10 columns, just add the class="col"

Answer №2

When dealing with an unknown number of items, one approach is to divide your array or list into smaller chunks containing 10 items each in your component class. This allows you to iterate through them effectively.

For instance, you can create a function like this in your component class:

chunkArray() {
  let minutes = this.state.minutes.specificMinutes.selectedMinutes;
  let chunks = [];
  let chunkSize = 10;
  for (let i = 0, j = minutes.length; i < j; i += chunkSize) {
    chunks.push(minutes.slice(i, i + chunkSize));
  }

  return chunks;
};

Subsequently, in your template, utilize nested *ngFor loops as demonstrated below:

<div *ngFor="let minutes of getChunks()" class="row">
  <div *ngFor="let minute of minutes | keyvalue" class="col checkbox-container">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" (click)="state.minutes.subTab='specificMinutes'" class="custom-control-input"
        id="minute-{{minute.key}}-checkbox"">
       <label class=" custom-control-label" for="minute-{{minute.key}}-checkbox">{{minute.key}}</label>
    </div>
  </div>
</div>

The first loop will render a row for each chunk, while the inner loop will display the individual elements within each chunk.

If needed, adjusting the value of chunkSize will change the number of items per row dynamically.

Update

In scenarios where the total item count is not fixed and might not be evenly divisible by 10, the last chunk could have fewer than 10 items. To handle this situation, consider modifying your markup as follows:

<div *ngFor="let minutes of getChunks()" class="row">
  <div class="offset-sm-1></div>
  <div *ngFor="let minute of minutes | keyvalue" class="col-sm-1 checkbox-container">
  ...
  </div>
  <div class="offset-sm-1></div>
</div>

Alternatively, adjust the final array in the getChunks() method to pad it with placeholder elements until there are 10 items. This way, your template will continue to display items uniformly.

Answer №3

To create space on both sides, simply insert an empty column.

.block {
  width: 20px;
  height: 20px;
  border-radius: 10px;
  padding: 10px;
  margin: 5px;
  background-color: skyblue;
  color: black;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-md-1">
    <!-- EMPTY -->
  </div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1"><span class="block">*</span></div>
  <div class="col-md-1">
    <!-- EMPTY -->
  </div>
</div>

Answer №4

If you're looking to make some layout adjustments, try utilizing an offset.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-sm-1 offset-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1"><!-- Last Div Remains Empty --></div>
</div>

Alternatively, you can opt for a custom design like the one below:

/*  SECTIONS  */

.section {
  clear: both;
  padding: 0px;
  margin: 0px;
}


/*  COLUMN SETUP  */

.col {
  display: block;
  float: left;
  margin: 1% 0 1% 0%;
}

.col:first-child {
  margin-left: 0;
}


/*  GROUPING  */

.group:before,
.group:after {
  content: "";
  display: table;
}

.group:after {
  clear: both;
}

.group {
  zoom: 1;
  /* For IE 6/7 */
}


/*  GRID OF TEN  */

.span_10_of_10 {
  width: 100%;
}

.span_9_of_10 {
  width: 90%;
}</answer4>
<exanswer4><div class="answer" i="58997289" l="4.0" c="1574425722" m="1574438530" a="YnJvb2tzcmVseXQ=" ai="4279496">
<p>If you need to adjust your layout, try incorporating an offset.</p>

<p><div>
<div>
<pre><code><link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-sm-1 offset-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1">col</div>
  <div class="col-sm-1"><!-- Last Div Remains Empty --></div>
</div>

Another option would be creating a personalized design similar to this:

/*  SECTIONS  */

.section {
  clear: both;
  padding: 0px;
  margin: 0px;
}


/*  COLUMN SETUP  */

.col {
  display: block;
  float: left;
  margin: 1% 0 1% 0%;
}

.col:first-child {
  margin-left: 0;
}


/*  GROUPING  */

.group:before,
.group:after {
  content: "";
  display: table;
}

.group:after {
  clear: both;
}

.group {
  zoom: 1;
  /* For IE 6/7 */
}


/*  GRID OF TEN  */

.span_10_of_10 {
  width: 100%;
}

.span_9_of_10 {
  width: 90%;
}

.span_8_of_10 {
  width: 80%;
}

.span_7_of_10 {
  width: 70%;
}

.span_6_of_10 {
  width: 60%;
}

.span_5_of_10 {
  width: 50%;
}

.span_4_of_10 {
  width: 40%;
}

.span_3_of_10 {
  width: 30%;
}

.span_2_of_10 {
  width: 20%;
}

.span_1_of_10 {
  width: 10%;
}


/*  GO FULL WIDTH BELOW 480 PIXELS */

@media only screen and (max-width: 480px) {
  .col {
    margin: 1% 0 1% 0%;
  }
  .span_1_of_10,
  .span_2_of_10,
  .span_3_of_10,
  .span_4_of_10,
  .span_5_of_10,
  .span_6_of_10,
  .span_7_of_10,
  .span_8_of_10,
  .span_9_of_10,
  .span_10_of_10 {
    width: 100%;
  }
}
<div class="section group">
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
  <div class="col span_1_of_10">
    1 of 10
  </div>
</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 feasible to use Angular 2 in conjunction with local storage and JWT implementation on IE9 and

Could someone please advise me on how to implement local storage in Angular2 for IE9 and above? I've attempted following this guide https://medium.com/@blacksonic86/angular-2-authentication-revisited-611bf7373bf9#.h42z63t9v, but my Angular 2 applicati ...

The iframe content is not updating despite using jQuery

On this site , the following code is present: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <iframe style="position: absolute; top: 0; left: 0; height: 100%; width: 100%" src="blank.html"></iframe ...

Enhanced customization of material-ui styles

After exploring various resources on styling overrides, I encountered a unique challenge. I am engaged in crafting styled material-ui components and integrating them across different sections of my application. My objective is to enable the customization o ...

extract individual components from the google books api

It has been quite a while since I last dabbled in JavaScript, so I decided to embark on a project creating a "bookcase" to organize the books I have read and those I still want to read. One challenge I encountered was figuring out how to separate the eleme ...

Bizarre Actions of a JavaScript Object

Currently, I am in the process of developing a Multiplayer game using Phaser and Eureca io. My main focus right now is on perfecting the authentication of players and their controls. To achieve this, I have implemented a method on the server that retrieves ...

Creating a Tree Hierarchy with Angular 4 FormArray

Looking for a demonstration on how to effectively utilize FormArray with a Tree Structure? I am currently working on implementing inline editing for a hierarchical system Although I have managed to make it functional for the initial level, I am facing ch ...

Is window.open exclusive to Firefox?

Apologies if this question has been asked before! I am experiencing some issues with my Javascript code. It works perfectly in Firefox and opens a pop-up window as expected. However, in IE 9 it does nothing, and in Chrome it behaves like a link and change ...

Resolved plugin issue through CSS adjustments

Take a look at this template 1) After referring to the above template, I developed a fixed plugin using JavaScript. 2) Clicking the icon will trigger the opening of a card. 3) Within the card, I designed a form using mdb bootstrap. Everything seems to ...

Showcasing a checkbox with the chosen item marked as selected

My goal is to allow users to select hobbies from a checklist provided. The chosen hobbies should then be added to the user table. I want the checkboxes to display the user's current hobbies - checked if selected, unchecked if not. Users should be able ...

The slider causing conflicts with widgets and content positioned below it in an HTML layout

When implementing a slider with a fade effect, there seems to be an issue with the placement of widgets on the homepage. The problem arises from the margin-top setting as when images change in the slider, the widgets clash with the slider. During the trans ...

`Issues with CSS/JQuery menu functionality experienced in Firefox`

After creating a toggleable overlay menu and testing it in various browsers, including Internet Explorer, everything seemed to work fine except for one major issue in Firefox (version 46). The problem arises when toggling the overlay using the "MENU" butt ...

What's the best way to get rid of the one-pixel gap between these elements on high-resolution displays like

http://jsfiddle.net/36ykrp9x/5/ HTML <div class="container"> <button>O</button><button>O</button> </div> CSS .container { width: 100%; background-color: rgb(120, 200, 200); text-align: center; } butt ...

Shifting Angular Component Declarations to a New Location

Here's a question that might sound silly: In my Angular project, I am looking to reorganize my component declarations by moving them from angular.module.ts to modules/modules.modules.ts. The goal is to structure my src/app directory as follows: src ...

Persist the checkbox value in the database and update the status of the label accordingly

I need a checkbox for user preference selection that is stored in a MySQL database for constant availability. The label should change to "Enabled" when the checkbox is selected and vice versa. Upon saving (submitting) the page, an alert message should disp ...

It's impossible for me to align two blocks at the same level

Check out the following code snippet. I am trying to group mid_left and mid_right within a single mid div tag, but struggling with positioning mid_right correctly - either outside of the mid tag or not at the same level as mid_left. I have experimented wit ...

increased space between tables in HTML email design for optimal display in Gmail inbox

Here is the link to my code: http://jsfiddle.net/user1212/G86KE/4/ I am experiencing an issue in Gmail where there is extra white space between 2 tables inside the same cell. I have attempted using display:block; margin:0; padding:0; line-height:0; How ...

Creating a div that automatically resizes to fit a specific width, with excess content wrapping to the next row

In our project, we currently have a design with 4 div elements arranged in two rows and two columns. However, the client has requested that we allow for multiple div elements within a container that spans 100% width. The inner divs should adjust to fit si ...

Positioning an anchor element over an image in Internet Explorer (IE)

Encountering an issue with IE9 (and 8) involving the positioning of empty anchor elements over an image. The anchors contain text, which is pushed off the page using CSS's text-indent property. Currently working on a website with promo panels display ...

Utilizing Node.js to dynamically inject variables in SASS compilation

I am currently working on an application where I need to dynamically compile SASS before rendering it on the client side (a caching system is in the works, so no worries there). At the moment, my tool of choice is node-sass and so far, everything is runnin ...

Tips for showing more rows by clicking an icon within an Angular 2 table

When I click on the plus (+) button in the first column of each row, only one row expands. How can I modify it to expand multiple rows at a time? Thanks in advance. <div> <table class="table table-striped table-bordered"> <thead> ...