Search code examples
bootstrap-4rowcol

Bootstrap Rows/Columns. How to force all 'col' into one row depending on Screen size


This image gets stacked side by side in a single row. I'd like to force it into stay in a row, whether that's changing image size to perhaps a scroll(I think this will look tacky), or another suggestion.

The example shows 11 images and 2 rows of 11 images. The images spill to an unwanted 2nd row if it fills the space. I don't want this. I want it to stay in one row no matter the window size or amount of images (3-40+) per row. I know bootstrap grid works on 12 columns... but I'm not sure how to make this work when I need a dynamic amount of columns per row.

I've provided images examples. How do I do this?

enter image description here

enter image description here

UPDATE: The 1st answer fixed the row with many images, but introduced spacing that I need to be eliminated when there are few images. I made padding and margin 0 but it's still spacing them evenly instead of right next to each other on the left like I'd like.

enter image description here

$(document).ready(function(){
  $("input").on('input',function(e){
 
    $("#box").html(function(){
    let html_string =''
    for(let num=0 ; num < e.target.value ; num ++){
      html_string = html_string + '<div class="col px-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt=""  width="100" height="100"></div>'
     }
      return html_string
    
    })
  });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<style>
</style>
</head>
<body>
  <div class="container">
  <div class="row">
    <div class="col">
      <input type="number" min=1 max=30 value=1 >
    </div>
  </div>
    <div class="row" id="box">
    
      <div class="col px-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt=""  width="100" height="100"> </div>


  </div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>

</body>
</html>

Solution CODE:

$(document).ready(function(){
  $("input").on('input',function(e){
 
    $("#box").html(function(){
    let html_string =''
    for(let num=0 ; num < e.target.value ; num ++){
      html_string = html_string + '<div class="col col-panel d-inline px-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt=""  width="100" height="100"></div>'
     }
      return html_string
    
    })
  });
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<style>
.col-panel{
max-width: 100px
}
</style>
</head>
<body>
  <div class="container">
  <div class="row">
    <div class="col">
      <input type="number" min=1 max=30 value=1 >
    </div>
  </div>
    <div class="row" id="box">
    
      <div class="col col-panel d-inline px-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt=""  width="100" height="100"> </div>


  </div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>

</body>
</html>


Solution

  • Call d-inline bootstrap class and set a max-width on picture div. For example, I have given .col{ max-width: 100px; }

    Code below-

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
    <style>
      .col{
        max-width: 100px;
      }
    </style>
    </head>
    <body>
      <div class="container">
        <div class="row">
          <div class="col d-inline p-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt="" ></div>
          <div class="col d-inline p-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt="" ></div>
          <div class="col d-inline p-0"><img class="img-fluid" src="https://dummyimage.com/vga" alt="" ></div>
        </div>
      </div>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
    
    </body>
    </html>