Create a Bootstrap grid with three columns

I've successfully implemented an accordion collapse feature for my categories/subcategories where a user can click on the category name to reveal the subcategories below. However, now I want to display the categories/subcategories in a three-column grid in the center of the page but I'm having trouble getting it right. Can anyone provide some guidance on how to achieve this?

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

$('.toggle').click(function(e) {
        e.preventDefault();

        var $this = $(this);

        if ($this.next().hasClass('show')) {
            $this.next().removeClass('show');
            $this.next().slideUp(350);
        } else {
            $this.parent().parent().find('li .inner').removeClass('show');
            $this.parent().parent().find('li .inner').slideUp(350);
            $this.next().toggleClass('show');
            $this.next().slideToggle(350);
        }
    });
ul {
  list-style: none;
  padding: 0;
  text-decoration: underline;

  .inner {
    padding-left: 1em;
    //overflow: hidden;
    display: none;

    &.show {
      /*display: block;*/
    }
  }

  li {
    margin: .5em 0;

    a.toggle {
      width: 100%;
      font-weight: bold;
      display: inline-block;
      color: #3c3d3d;
      padding: 1.2em;

      &:hover {
        color: #c7c9c9;
      }
    }
  }
}

.category-select:hover {color: #c7c9c9;}
.category-select {color: #787979;}
<div class="container-fluid">
      <div class="row">
        <div class="col-md-8 offset-md-1">
        <%= form_tag (search_url), :method => "get", id: "search-form" do %>
          <div class="row">
            <div class="col-md-4">
              <ul class="accordion">
              <li>
              <% @categories.each do |category| %>
              <a class="toggle" href="javascript:void(0);" style="text-decoration: none; text-align: center;"><%= category.name.capitalize %></a>
              <% unless category.children.empty? %>
              <ul class="inner" style="text-align: center;">
                <% category.children.each do |subcategory| %>           
                    <li><label class="category-select" style="cursor: pointer;">
                      <span style="display: none;"><%= check_box_tag :search, subcategory.id %></span>
                      <%= subcategory.name.capitalize %>
                    </label></li>      
                <% end %>
              </ul>
            <%end%>
            <%end%>
            <%= submit_tag '', :style => "display: none;" %>
         <% end %>
           </li>
          </ul>
        </div>
        </div>
       </div>
    </div>
    </div>

Answer №1

Your query is a bit unclear, but there are several methods you can try to make the loop utilize 3 columns. I'm not certain if this solution will fit your needs as it's hard to grasp the question accurately.

You could attempt the following:

<div class="row">  ---> Optional
  <% @some_items.in_groups_of(3) do |group| %>
    <div class="row">
      <% group.each do |item| %>
        <div class="col-md-4">
          <%= item.name %>
        </div>
      <% end %>
    </div>
  <% end %>
</div>   ---> Optional

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

How can jQuery select an element by its id attribute by referencing the href attribute of a different element?

Whenever the user interacts with a div.nav element, jQuery switches its class to active. Presently, it applies display: block to all three of the div.content elements. I aim for jQuery to only apply the display: block property to the div.content elements t ...

Displaying text within an HTML table featuring a vibrant background

I'm having trouble printing a basic certificate that is formatted as an HTML table. There are a couple of frustrating issues I'm facing. 1) When I try to print the table using Chrome, my CSS changes are not being applied. 2) I can't seem to ...

What is the best way to show information entered into a textbox on a different webpage?

Looking for advice on how to collect data in a text box and display it on another page? I have included HTML code for both the announcement page (where the data is entered) and the archive page (where the data should be shown). Could someone guide me on ...

Display the page number when printing using CSS

I am struggling to display the page number at the bottom of my printable document. I followed a source on Stack Overflow, but unfortunately, it did not work for me. My current CSS attempt to achieve this is as follows: body { text-align: justify; } /* ...

"Enhancing partial views in Ruby on Rails: A step-by-step guide to

Seeking a method to update the partial view product_images.html.erb using ajax My js code is $("#delete-img").click(function(e){ var checkValues = $('input[name=image_id]:checked').map(function(){ return $(this).val(); ...

Using HTML to showcase various prompt messages based on the screen resolution

For the button click event, when clicked on desktop screens it will show a prompt message saying 'Hi'. On the other hand, when clicked on mobile screens, the prompt message displayed will be "Hello". ...

Using the `document.querySelector` method to target the `.mat-progress-bar-fill::after` element

Is there a way to dynamically adjust the fill value of an Angular Material Progress Bar? In CSS, I can achieve this with: ::ng-deep .mat-progress-bar-fill::after { background-color: red; } However, since the value needs to be dynamic, I am unable to do ...

Excessive scrolling issue with dropdown menu

Currently, I have a solution in place to handle scrolling very lengthy menus in my dropdowns. You can find more information about it here: http://css-tricks.com/long-dropdowns-solution/ I am wondering if there is a way to prevent the menus from continuou ...

Draggable Table Columns with HTML, CSS, and JavaScript

My goal is to create a table in a JSP page that functions similarly to the example shown here. I would like to be able to drag a column of dates within the table and have a form pop up. Can anyone provide guidance on how I can achieve this in JSP? ...

Javascript postback functionality is fully operational in bootstrap 4, however, it is failing to return

Encountering an issue with my postback JavaScript function in the ASP/BootStrap environment. After searching on stackoverflow for a solution, I couldn't find one that resolved my problem. The problem is when I click on an image, the page reloads (the ...

What is the best way to showcase a ul element as inline-block?

Is there a way to keep the bottom menu aligned in one row on all screen sizes? I attempted using display: inline-block, but it doesn't seem to work for me. Below is the CSS code: footer #middle-footer { background: #F6F6F6; color: #000; font-size ...

Ways to find the image source using JavaScript/Jquery upon page loading?

I've scoured countless forums, yet a viable solution still eludes me. My objective is simple - upon page load, I want to gather all elements with the ".home" class and store them in an array called arr. Subsequently, the script should iterate through ...

The two CSS classes are styled with different border-color values, but only one is being applied correctly

My goal is to modify the border color of a textarea using jQuery. Previously, I achieved this by using .css("border-color","rgb(250,0,0)"), and it was working perfectly. However, I have now been advised against using CSS directly in JavaScript and told to ...

The wrapAll() method can be used to wrap list items within two columns

I am looking to group multiple li elements within two div containers by using jQuery's wrapAll method. The challenge lies in the fact that these items are rendered within a single <ul> element via a CMS. Here is the current setup: <ul> ...

The refined search parameters do not appear on the OpenCart theme

Recently, while managing my online shop using the opencart cms, I decided to enhance the search functionality by adding filters to my products. I followed a tutorial that guided me through the process: link to tutorial After implementing two filters for s ...

What could be causing my website to automatically adjust the CSS height on its own?

My code looks like this: <li style = "height: 250px;"> <p>Foo</p></li> Despite my efforts, when I visit the website, the height doesn't seem to change. After inspecting the element in Chrome, I discovered that it's s ...

Show image when hovering over individual words

Is there a way to display different descriptions or images when hovering over each word in a paragraph using HTML? It seems that only the last element is being retrieved and displayed across the entire paragraph. Any suggestions on how to fix this issue wo ...

What is the best way to align my Navbar in the center?

Previously, I searched on this platform for solutions but couldn't find anything that worked. Currently, I am using the Bulma Framework which is not very well-known. However, I need assistance in aligning the brand link on the navbar to the center. Be ...

What is the best way to arrange four divs in a row on a mobile device that is responsive?

I am facing an issue with displaying 4 divs of equal size on my website. In desktop view, they appear side by side but in mobile view, they stack on top of each other. I have tried using display: flex; and bootstrap classes like col-3, col-sm-3, col-md-3, ...

Does the utilization of setTimeout(function, 100) have an impact on performance?

I have implemented a function that continuously checks if the URL contains any of the navigation hrefs every 100ms. If it finds a match, specific elements are assigned the class active. Here's the code snippet: var checkActive = function(){ ...