Creating a row with 5 columns in Bootstrap 4 dynamically is a handy trick for enhancing the layout of your website

Struggling to find the right layout for displaying products on my store's home page.

   <div class="row">

  @foreach($produts as $product)  //this is the syntax of my templating engine

   <div class="col-md-3">
  </div>


  @endforeach
 </div>

The current issue I'm facing is that with col-md-3, the products appear too big and with col-md-2, they appear too small. To resolve this, I want to divide it into 5 columns. The discrepancy in size breaks the design significantly. Using class="col" doesn't work for me as I am adding multiple products programmatically causing them to split in several lines. It would be ideal if Bootstrap had 24 columns instead of 12. Is there a Bootstrap solution to achieve this or will I have to resort to pure CSS?

Answer №1

To easily implement this style, simply include the following CSS snippet:

.col-20 {-ms-flex: 0 0 20%; flex: 0 0 20%; max-width: 20%;}
@media (min-width: 576px) {.col-sm-20 {-ms-flex: 0 0 20%; flex: 0 0 20%; max-width: 20%;}}
@media (min-width: 768px) {.col-md-20 {-ms-flex: 0 0 20%; flex: 0 0 20%; max-width: 20%;}}
@media (min-width: 992px) {.col-lg-20 {-ms-flex: 0 0 20%; flex: 0 0 20%; max-width: 20%;}}
@media (min-width: 1200px) {.col-xl-20 {-ms-flex: 0 0 20%; flex: 0 0 20%; max-width: 20%;}}

By utilizing the custom class col-20, you can enhance the layout of your Bootstrap4 columns.

<div class="col-xl-20 col-lg-4 col-md-6">...

Answer №2

If you want to showcase 5 products in the center of your page, you can achieve this by using an empty div with the class col-md-offset-1 to shift your product 1 column to the right. Then, display each product using col-md-2. This method ensures that the products are displayed centrally, not aligned to one side. Below is a sample code snippet demonstrating this approach:

 <div class="row">

<div class="col-md-offset-1">
<div>

@foreach($produts as $product)  //this is the syntax of my templating engine

<div class="col-md-2">
    $product
</div>


@endforeach

Answer №3

Here is an example of how you can include your custom class:

<div class="row">
    <div class="col-sm-6 col-md-4 col-lg-3 col-xl-5th">
    </div>
</div>

You can also apply CSS styles specifically for a minimum width of 1200px:

@media (min-width: 1200px) {
    .col-xl-5th {
        flex: 0 0 20%;
        max-width: 20%;
    }
}

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

Troubleshooting Problems with CSS Span Placement

Challenges with Span Positioning in CSS Currently, I am encountering difficulties while working on the index.html.erb file for an example Rails website. Below is a snippet of the HTML code I am struggling with (please note that this is simply a fabricated ...

Code Snippet: Adjust Table Column Alignment using CSS

Apologies for the basic question, as I am new to CSS. Below is the UI that I am working with: https://i.stack.imgur.com/crAYE.png Here is the code that corresponds to the UI: <div class="row centered-form center-block"> <div cla ...

Putting text on top of an image using Bootstrap

Recently diving into the world of Bootstrap, I have come across a puzzling issue: Take a look at the HTML code from my website: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width ...

Tips for Aligning Several Images Horizontally in an Article

Does anyone know how to center pictures in an article horizontally? I've tried various techniques without success. If it would be helpful, I can share the CSS I currently have. However, I am open to starting from scratch with the CSS as well since I ...

Automate the process of filling up and activating a bootstrap dropdown upon a click, and clearing it out when it is

Is there a method to reveal the dropdown content only upon clicking instead of displaying all content at once and increasing the lines of HTML code? Perhaps using an onclick attribute on the button and incorporating a function inside? var data = [{ " ...

Initiate a page break at the beginning of the table in case there are not enough lines on the first page

Does anyone know how to ensure that a table starts on a new page when printing, rather than continuing at the bottom of the previous page? I have tables with repeating headers and many rows, and sometimes a new table will start at the very end of a page. ...

Despite being positioned absolutely, they are able to make z-index work effectively

Currently, I am facing an issue with two elements in my design. The first element consists of dotted circles that should have a z-index of -999 so they remain in the background entirely. The second element is a login form that needs to have a z-index of 99 ...

adjusting the font size based on the size of the screen

Hi everyone, I've encountered an issue with adjusting font sizes based on screen size. My framework is Bootstrap 3 and I'm using LESS. Here is the initial part of my LESS code: @font-size-base: 16px; @font-size-large: ceil((@fo ...

How do I go about configuring the uploaded image?

Here is a sample of my HTML code: <div class="images"> <figure> <button id="upload-add-product" class="icon-plus"></button> <input id="upload-file" type="file" style="display: none;" multiple/> </fi ...

Enhance a link using jQuery to allow it to expand and collapse

I am currently working on an MVC view that features a 3-column table showcasing a list of products. The first column, which contains the product names, is clickable and directs users to a specific product page. I am looking to implement functionality where ...

user1234: I'm interested in decreasing the amount of items shown on the screen when it's smaller

Currently working on the design of the Search page for an online store. I aim to adjust the number of items displayed on the screen gradually as the screen size changes, similar to Amazon's layout. Additionally, I'm looking to rectify the excess ...

Align an image in the center of a div without using absolute positioning

I'm currently facing an issue where I need to align an image at the center of a div without using absolute positioning. This is necessary because the image spills over into any adjacent divs on the page. Specifically, I have a previous div containing ...

Place picture in the bottom right corner of the div without using absolute positioning

I am looking for a way to position an image in the lower right corner of a div, but I want to avoid using absolute positioning. The reason for this is that when I use absolute positioning, the text wrap is affected due to the float right applied to the ima ...

Issue with integrating Contact Form 7 HTML into Wordpress

I've revisited this question with a new "input" HTML tag Despite changing everything, CSS still isn't being applied. HTML Code : <form action="#" class="contact-one__form row"> <div class="col-lg-6"> ...

Hover to stop menu movement

Can someone help me achieve a similar menu hover effect like this one? I'm trying to create a menu with a hold/pause effect on hover, specifically in the section titled "A programme for every vision". The goal is to navigate through the sub menus smo ...

Looking for a jQuery plugin that helps with form alignment?

In a blog post comment, I came across an interesting jQuery form alignment plugin: jQuery.fn.autoWidth = function(options) { var settings = { limitWidth : false } if(options) { jQuery.extend(settings, options); }; ...

Escaping from its container, text breaks free with the power of CSS

Experiencing difficulties with CSS flex layout that are proving challenging to resolve. HTML: <div class="image-view"> <div class="info"> <span class="label title">d37ci4x.jpg</span> <span class="label scale ...

Can IE(7?) cause distortion of backgrounds from sprites?

This task is giving me a headache. We're almost finished with revamping our website. The last step involves consolidating all the glyphs and icons into a sprite. These images are transparent .png files, so we made sure the sprite maintains transparen ...

Maintain the previous droppable positioning after refreshing the page

I am encountering an issue with the .droppable event. I have set up two sections where I can move elements, but every time the page is refreshed, the positioning of the elements reverts to the initial position. How can I maintain the last positioning of th ...

Shade the entire td column in different colors depending on the characteristics of th

I am working with a table and here is the code for it: <table> <thead> <tr> <th style="width: 40%; text-align: center; vertical-align: center;">Nr.<br> crt.</th> <th style="font-weight: bold; wi ...