bootstrap col-auto 布局高度对齐在移除按钮组件时出现问题

huangapple go评论152阅读模式
英文:

bootstrap col-auto layout height alignment breaking when removing a button component

问题

我正在尝试使用Bootstrap创建一个弹性列布局,其中页眉和页脚区域具有自动高度,而内容区域可滚动。当我们在页脚和页眉列中插入组件时,它将采用组件的大小,其余的空间将由内容区域占据。到目前为止,一切都运行得很顺利。但是当我隐藏/移除“previous button”(上一个按钮)时,具有“col-auto”类的页脚的高度对齐会中断。请帮助。

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
setTimeout(() => {
  document.querySelector('.prev-btn').remove();
}, 2000);

<!-- 语言: lang-css -->
html,
body {
  height: 100%;
}

<!-- 语言: lang-html -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />

<div class="container-fluid p-0 h-100">
  <div class="row g-0 flex-column h-100">
    <header class="col-auto bg-secondary">
      <nav class="navbar bg-light">
        <div class="container-fluid">
          <a class="navbar-brand">Navbar</a>
          <form class="d-flex" role="search">
            <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success" type="submit">Search</button>
          </form>
        </div>
      </nav>
    </header>

    <div class="col d-flex align-items-center justify-content-center overflow-auto">
      <div class="col-6 py-3 m-auto">
        Content area
      </div>
    </div>

    <footer class="col-auto">
      <div class="container-fluid p-3 bg-light">
        <div class="row g-0 align-items-center">
          <div class="col">
            <button class="btn fw-bold btn-outline-danger shadow-none">CANCEL & RESTART</button>
          </div>

          <div class="col-auto px-3 text-center">STEP 1 OF 4</div>

          <div class="col">
            <div class="d-flex justify-content-end">
              <button class="btn fw-bold btn-outline-secondary px-5 me-3 shadow-none prev-btn">PREVIOUS</button>
              <button class="btn fw-bold btn-outline-primary px-5 shadow-none">NEXT</button>
            </div>
          </div>
        </div>
      </div>
    </footer>
  </div>
</div>

<!-- 结束代码段 -->

注意:这是您提供的HTML和CSS代码,我没有对其进行翻译。如有任何其他问题,请随时提出。

英文:

I am trying to get a flex column layout using bootstrap with the header and footer area taking auto height and the content area scrollable. When we insert components inside the footer and header column it will take the size of the components and the rest of the space will be taken by the content area. So far it's working very well. But when I hide/remove the previous button, the footer with col-auto class the height alignment breaks. Please help.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setTimeout(() =&gt; {
document.querySelector(&#39;.prev-btn&#39;).remove();
}, 2000);

<!-- language: lang-css -->

html,
body {
height: 100%;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;div class=&quot;container-fluid p-0 h-100&quot;&gt;
&lt;div class=&quot;row g-0 flex-column h-100&quot;&gt;
&lt;header class=&quot;col-auto bg-secondary&quot;&gt;
&lt;nav class=&quot;navbar bg-light&quot;&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;a class=&quot;navbar-brand&quot;&gt;Navbar&lt;/a&gt;
&lt;form class=&quot;d-flex&quot; role=&quot;search&quot;&gt;
&lt;input class=&quot;form-control me-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot;&gt;
&lt;button class=&quot;btn btn-outline-success&quot; type=&quot;submit&quot;&gt;Search&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;/header&gt;
&lt;div class=&quot;col d-flex align-items-center justify-content-center overflow-auto&quot;&gt;
&lt;div class=&quot;col-6 py-3 m-auto&quot;&gt;
Content area
&lt;/div&gt;
&lt;/div&gt;
&lt;footer class=&quot;col-auto&quot;&gt;
&lt;div class=&quot;container-fluid p-3 bg-light&quot;&gt;
&lt;div class=&quot;row g-0 align-items-center&quot;&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;button class=&quot;btn fw-bold btn-outline-danger shadow-none&quot;&gt;CANCEL &amp;amp; RESTART&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;col-auto px-3 text-center&quot;&gt;STEP 1 OF 4&lt;/div&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;div class=&quot;d-flex justify-content-end&quot;&gt;
&lt;button class=&quot;btn fw-bold btn-outline-secondary px-5 me-3 shadow-none prev-btn&quot;&gt;PREVIOUS&lt;/button&gt;
&lt;button class=&quot;btn fw-bold btn-outline-primary px-5 shadow-none&quot;&gt;NEXT&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/footer&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

这似乎是因为在重新布局期间,“取消和重新启动”按钮正在换行。您可以使用Bootstrap的text-nowrap类来防止这种情况,尽管这可能需要在较小的屏幕上进行其他布局更改。一个解决方案可能是将“1 of 4”标签移动到移动设备上的按钮上方。

英文:

This seems to be happening because the CANCEL & RESTART button is wrapping during reflow. You can use Bootstrap's text-nowrap class to prevent this, though that may require other layout changes on smaller screens. One solution might be to move the "1 of 4" label above the buttons for mobile.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

setTimeout(() =&gt; {
document.querySelector(&#39;.prev-btn&#39;).remove();
}, 2000);

<!-- language: lang-css -->

html,
body {
height: 100%;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;div class=&quot;container-fluid p-0 h-100&quot;&gt;
&lt;div class=&quot;row g-0 flex-column h-100&quot;&gt;
&lt;header class=&quot;col-auto bg-secondary&quot;&gt;
&lt;nav class=&quot;navbar bg-light&quot;&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;a class=&quot;navbar-brand&quot;&gt;Navbar&lt;/a&gt;
&lt;form class=&quot;d-flex&quot; role=&quot;search&quot;&gt;
&lt;input class=&quot;form-control me-2&quot; type=&quot;search&quot; placeholder=&quot;Search&quot; aria-label=&quot;Search&quot;&gt;
&lt;button class=&quot;btn btn-outline-success&quot; type=&quot;submit&quot;&gt;Search&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;/header&gt;
&lt;div class=&quot;col d-flex align-items-center justify-content-center overflow-auto&quot;&gt;
&lt;div class=&quot;col-6 py-3 m-auto&quot;&gt;
Content area
&lt;/div&gt;
&lt;/div&gt;
&lt;footer class=&quot;col-auto&quot;&gt;
&lt;div class=&quot;container-fluid p-3 bg-light&quot;&gt;
&lt;div class=&quot;row g-0 align-items-center&quot;&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;button class=&quot;btn fw-bold btn-outline-danger shadow-none text-nowrap&quot;&gt;CANCEL &amp;amp; RESTART&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;col-auto px-3 text-center&quot;&gt;STEP 1 OF 4&lt;/div&gt;
&lt;div class=&quot;col&quot;&gt;
&lt;div class=&quot;d-flex justify-content-end&quot;&gt;
&lt;button class=&quot;btn fw-bold btn-outline-secondary px-5 me-3 shadow-none prev-btn&quot;&gt;PREVIOUS&lt;/button&gt;
&lt;button class=&quot;btn fw-bold btn-outline-primary px-5 shadow-none&quot;&gt;NEXT&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/footer&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 22:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定