简体   繁体   English

如何使用 css 显示网格使我的列号动态化?

[英]How to I make my column number dynamic with css display grid?

I am currently using css display: grid;我目前正在使用 css display: grid; and grid-template-columns: auto auto auto auto auto auto auto auto auto auto;grid-template-columns: auto auto auto auto auto auto auto auto auto auto; to display the layout as shown in the image.以显示如图所示的布局。 My question is how to I make my column dynamic.我的问题是如何使我的专栏动态化。 At the moment, I hard-coded it by putting 10 auto which is referring to 10 columns but my table column is dynamic.目前,我通过放置 10 auto来对其进行硬编码,它指的是 10 列,但我的表列是动态的。 Some have 8 columns.有些有 8 列。 I already have my dynamic column number calculated in my Javascript / Jquery.我已经在 Javascript / Jquery 中计算了我的动态列号。 I don't have anything like sass.我没有像 sass 这样的东西。 Do I need it to get this done?我需要它来完成这项工作吗?

在此处输入图像描述

1) Changing the number of columns by JavaScript 1)通过JavaScript改变列数

const cols = 10;
object.style.gridTemplateColumns=`repeat(${cols},auto)`;

2) Changing the number of columns in PHP 2)改变PHP中的列数

<?php $cols = 10; ?>
...
<div class="grid" style="--cols: <?php echo $cols; ?>;">...</div>

and the following CSS code和下面的 CSS 代码

.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM