在前端开发中,经常需要使用网格布局来排列页面元素,而手写网格布局往往会浪费时间和精力。这时候,我们可以考虑使用 npm 包 gridbuilding。
简介
gridbuilding 是一个使用 CSS Grid 布局的 npm 包,它可以帮助我们快速的创建网格布局。使用 gridbuilding,我们可以通过简单的配置就可以生成多种供选择的网格布局,而不需要手写 CSS。
安装
使用 npm 安装 gridbuilding,命令如下:
npm install gridbuilding
使用
创建网格布局
在 HTML 中引入 gridbuilding,代码如下:
<link rel="stylesheet" href="./node_modules/gridbuilding/dist/gridbuilding.min.css">
在 HTML 文件中定义一个容器元素,作为网格布局的父元素。然后,我们可以在容器元素中定义行和列,代码如下:
-- -------------------- ---- ------- ---- ----------------------- ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------
在上面的例子中,我们定义了一个 id 为 grid-container 的容器元素,并在其中定义了两个 row 元素和六个 column 元素,生成了一个包含两行三列的网格布局。
选择网格布局
gridbuilding 提供了多种不同的网格布局供选择,我们可以通过在容器元素中添加不同的 class 来选择不同的布局。
默认布局
默认布局是最基本的网格布局,如下代码所示:
-- -------------------- ---- ------- ---- ----------------------- ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------
两列等宽布局
在容器元素中添加 class 为 grid-two-columns
,就可以生成两列等宽的网格布局。如下代码所示:
<div class="grid-container grid-two-columns"> <div class="column">1</div> <div class="column">2</div> <div class="column">3</div> <div class="column">4</div> <div class="column">5</div> <div class="column">6</div> </div>
三列等宽布局
在容器元素中添加 class 为 grid-three-columns
,就可以生成三列等宽的网格布局。如下代码所示:
-- -------------------- ---- ------- ---- --------------------- -------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ------
自定义网格布局
我们也可以通过配置自定义的网格布局。我们可以通过修改四个变量($grid-columns, $grid-gutter, $grid-container-width, $grid-max-width)来修改网格的列数、间距、容器宽度和最大宽度。在默认情况下,$grid-columns 为 12,$grid-gutter 为 30px,$grid-container-width 为 960px,$grid-max-width 为 1200px。
在 CSS 文件中,我们可以这样配置自定义变量:
$grid-columns: 8; $grid-gutter: 40px; $grid-container-width: 800px; $grid-max-width: 1000px;
然后,在 HTML 文件中添加自定义 class,并把自定义变量写进 data 属性中,如下代码所示:
-- -------------------- ---- ------- ---- --------------------- -------- --------------------- --------------------- ------------------------------- --------------------------- ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------
示例代码
展示默认网格布局
-- -------------------- ---- ------- --------- ----- ------ ------ ----- ---------------- -------------------------------- ----- ---------------- ------------------------------------------------------------- ------- ---------- - ------ ---- ------- - ----- - ---- - ----------------- ----- ------- --- ----- ----- -------------- ----- - ------- - ----------------- ----- -------- ----- ----------- ------- - -------- ------- ------ ---- ------------------ ---- ----------------------- ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------ ------ ------- -------
该示例代码展示了 gridbuilding 的默认网格布局效果。
展示两列等宽网格布局
-- -------------------- ---- ------- --------- ----- ------ ------ ----- ---------------- -------------------------------- ----- ---------------- ------------------------------------------------------------- ------- ---------- - ------ ---- ------- - ----- - ---- - ----------------- ----- ------- --- ----- ----- -------------- ----- - ------- - ----------------- ----- -------- ----- ----------- ------- - -------- ------- ------ ---- ------------------ ---- --------------------- ------------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------ ------- -------
该示例代码展示了 gridbuilding 的两列等宽网格布局效果。
展示三列等宽网格布局
-- -------------------- ---- ------- --------- ----- ------ ------ ----- ---------------- -------------------------------- ----- ---------------- ------------------------------------------------------------- ------- ---------- - ------ ---- ------- - ----- - ---- - ----------------- ----- ------- --- ----- ----- -------------- ----- - ------- - ----------------- ----- -------- ----- ----------- ------- - -------- ------- ------ ---- ------------------ ---- --------------------- -------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------ ------- -------
该示例代码展示了 gridbuilding 的三列等宽网格布局效果。
展示自定义网格布局
-- -------------------- ---- ------- --------- ----- ------ ------ ----- ---------------- -------------------------------- ----- ---------------- ------------------------------------------------------------- ------- ---------- - ------ ---- ------- - ----- - ---- - ----------------- ----- ------- --- ----- ----- -------------- ----- - ------- - ----------------- ----- -------- ----- ----------- ------- - -------- ------- -------------- -- ------------- ----- ---------------------- ------ ---------------- ------- -------- ------- ------ ---- ------------------ ---- --------------------- -------- --------------------- --------------------- ------------------------------- --------------------------- ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ---- ------------ ---- ---------------------- ---- ---------------------- ---- ---------------------- ------ ------ ------ ------- -------
该示例代码展示了如何使用自定义变量来定义网格布局,并在 HTML 文件中添加自定义 class 和 data 属性。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/600568d181e8991b448e4907