如何使 Bootstrap 5.3.0 可折叠按钮显示在这个简单的网页中?

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

What needs to be done to make the Bootstrap 5.3.0 Collapsible button show in this simple web page?

问题

我试图创建一个简单的网页,使用Bootstrap(版本5.3.0)的可折叠组件,但无论我尝试什么都无法使其工作。
我只想要一个可折叠组件,其中包含一些链接,当您单击它时会显示这些链接。
但是,即使我将问题简化为仅显示字符串列表(如下所示的代码),问题仍然存在。
问题是可折叠按钮无论我尝试什么都不显示。
<span class="navbar-toggler-icon"></span>不可见。我在其两侧添加了'X',只是为了指示不可见的按钮在哪里。
有人知道如何修复这个问题,使可折叠按钮可见吗?
谢谢。
以下是代码:

<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<header class="bg-primary text-white text-center py-3">
  <div class="container">
    <div class="row">
      <div class="col-md-4">
        <div class="left-header">
          <h3>Left Header</h3>
        </div>
      </div>
      <div class="col-md-4">
        <div class="central-header">
          <h3>Central Header</h3>
        </div>
      </div>
      <div class="col-md-4">
        <div class="right-header">
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNav">
            X<span class="navbar-toggler-icon"></span>X
          </button>
        </div>
        <div class="collapse" id="collapsibleNav">
          <div class="card card-body">
            <ul class="nav">
              <li class="nav-item">Item 1</li>
              <li class="nav-item">Item 2</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</header>

<main class="container mt-4">
  <h2>Main Content</h2>
  <p>This is the main content of the page.</p>
</main>

希望这能帮助您解决可折叠按钮不可见的问题。

英文:

I've tried to create a simple web page that uses the Bootstrap (version 5.3.0) Collapsible and I cannot get it to work no matter what I try.
All I want is a Collapsible that 'holds' a few links in it which are shown when you click it.
But when that didn't work I simplified it to just a list of strings (code shown below) but the problem is still there.
The problem is that the Collapsible button does not show no matter what I try.
The &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt; is not visible. I've added 'X' to both sides
of it just so there is an indication where that invisible button is.
Anyone knows how to fix that, make the Collapsible button visible?
Thanks.
Here's the code:

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

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;

&lt;header class=&quot;bg-primary text-white text-center py-3&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;div class=&quot;left-header&quot;&gt;
          &lt;h3&gt;Left Header&lt;/h3&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;div class=&quot;central-header&quot;&gt;
          &lt;h3&gt;Central Header&lt;/h3&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;div class=&quot;right-header&quot;&gt;
          &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#collapsibleNav&quot;&gt;
              X&lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;X
            &lt;/button&gt;
        &lt;/div&gt;
        &lt;div class=&quot;collapse&quot; id=&quot;collapsibleNav&quot;&gt;
          &lt;div class=&quot;card card-body&quot;&gt;
            &lt;ul class=&quot;nav&quot;&gt;
              &lt;li class=&quot;nav-item&quot;&gt;Item 1&lt;/li&gt;
              &lt;li class=&quot;nav-item&quot;&gt;Item 2&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/header&gt;

&lt;main class=&quot;container mt-4&quot;&gt;
  &lt;h2&gt;Main Content&lt;/h2&gt;
  &lt;p&gt;This is the main content of the page.&lt;/p&gt;
&lt;/main&gt;

<!-- end snippet -->

答案1

得分: 1

尝试将.navbar选择器添加到父容器中:right-header navbar,因为实际上它来自该类(--bs-navbar-toggler-icon-bg),而不是图标,_汉堡包_实际上是在.navbar中定义的一个变量,所以通过将它添加到父容器中,它变得可访问:

.navbar-toggler-icon {
    background-image: var(--bs-navbar-toggler-icon-bg);
}
.navbar {
    --bs-navbar-toggler-icon-bg: url("data:image/svg+xml,%3csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; viewBox=&#39;0 0 30 30&#39;%3e%3cpath stroke=&#39;rgba%2833, 37, 41, 0.75%29&#39; stroke-linecap=&#39;round&#39; stroke-miterlimit=&#39;10&#39; stroke-width=&#39;2&#39; d=&#39;M4 7h22M4 15h22M4 23h22&#39;/%3e%3c/svg%3e");
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<header class="bg-primary text-white text-center py-3">
  <div class="container">
    <div class="row">
      <div class="col-md-4">
        <div class="left-header">
          <h3>Left Header</h3>
        </div>
      </div>
      <div class="col-md-4">
        <div class="central-header">
          <h3>Central Header</h3>
        </div>
      </div>
      <div class="col-md-4">
        <div class="right-header navbar">
          <button class="navbar-toggler mx-auto" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNav">
            X<span class="navbar-toggler-icon"></span>X
          </button>
        </div>
        <div class="collapse" id="collapsibleNav">
          <div class="card">
            <ul class="nav card-body">
              <li class="nav-item">Item 1</li>
              <li class="nav-item">Item 2</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</header>

<main class="container mt-4">
  <h2>Main Content</h2>
  <p>This is the main content of the page.</p>
</main>
英文:

Try adding .navbar selector to the parent container: right-header navbar, because it's actually coming from that class (--bs-navbar-toggler-icon-bg), not the icon, the hamburger is actually a variable defined in .navbar, so by adding it to the parent it becomes accessible:

.navbar-toggler-icon {
    background-image: var(--bs-navbar-toggler-icon-bg);
}

<!-->
.navbar {
--bs-navbar-toggler-icon-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%2833, 37, 41, 0.75%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
}

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

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;

&lt;header class=&quot;bg-primary text-white text-center py-3&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;div class=&quot;left-header&quot;&gt;
          &lt;h3&gt;Left Header&lt;/h3&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;div class=&quot;central-header&quot;&gt;
          &lt;h3&gt;Central Header&lt;/h3&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;div class=&quot;right-header navbar&quot;&gt;
          &lt;button class=&quot;navbar-toggler mx-auto&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#collapsibleNav&quot;&gt;
              X&lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;X
            &lt;/button&gt;
        &lt;/div&gt;
        &lt;div class=&quot;collapse&quot; id=&quot;collapsibleNav&quot;&gt;
          &lt;div class=&quot;card&quot;&gt;
            &lt;ul class=&quot;nav card-body&quot;&gt;
              &lt;li class=&quot;nav-item&quot;&gt;Item 1&lt;/li&gt;
              &lt;li class=&quot;nav-item&quot;&gt;Item 2&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/header&gt;

&lt;main class=&quot;container mt-4&quot;&gt;
  &lt;h2&gt;Main Content&lt;/h2&gt;
  &lt;p&gt;This is the main content of the page.&lt;/p&gt;
&lt;/main&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 14:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387490.html
匿名

发表评论

匿名网友

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

确定