BootstrapRows/Columns。如何根据屏幕大小将所有 'col' 强制排成一行
Bootstrap Rows/Columns. How to force all 'col' into one row depending on Screen size
这张图片并排堆叠成一行。我想强制它保持连续,无论是将图像大小更改为滚动(我认为这看起来很俗气),还是其他建议。
该示例显示了 11 张图片和 2 行每行 11 张图片。如果图像填满 space,则图像会溢出到不需要的第二行。我不想要这个。无论 window 大小或每行图像数量 (3-40+),我都希望它保持在一行中。我知道 bootstrap 网格在 12 列上工作...但是当我需要每行的动态列数时,我不确定如何进行这项工作。
我提供了图片示例。我该怎么做?
更新:
第一个答案修复了包含许多图像的行,但引入了当图像很少时我需要消除的间距。我将 padding 和 margin 设置为 0,但它们的间距仍然均匀,而不是像我希望的那样在左侧彼此相邻。
$(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/bootstrap@4.5.3/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/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
解决方案代码:
$(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/bootstrap@4.5.3/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/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
调用 d-inline bootstrap class 并在图片上设置最大宽度 div。例如,我给了
.col{ max-width: 100px; }
代码如下-
<!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/bootstrap@4.5.3/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/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
这张图片并排堆叠成一行。我想强制它保持连续,无论是将图像大小更改为滚动(我认为这看起来很俗气),还是其他建议。
该示例显示了 11 张图片和 2 行每行 11 张图片。如果图像填满 space,则图像会溢出到不需要的第二行。我不想要这个。无论 window 大小或每行图像数量 (3-40+),我都希望它保持在一行中。我知道 bootstrap 网格在 12 列上工作...但是当我需要每行的动态列数时,我不确定如何进行这项工作。
我提供了图片示例。我该怎么做?
更新: 第一个答案修复了包含许多图像的行,但引入了当图像很少时我需要消除的间距。我将 padding 和 margin 设置为 0,但它们的间距仍然均匀,而不是像我希望的那样在左侧彼此相邻。
$(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/bootstrap@4.5.3/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/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
解决方案代码:
$(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/bootstrap@4.5.3/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/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>
调用 d-inline bootstrap class 并在图片上设置最大宽度 div。例如,我给了
.col{ max-width: 100px; }
代码如下-
<!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/bootstrap@4.5.3/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/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
</body>
</html>