【问题标题】:Right Sidebar links need to appear on top on small screens右侧边栏链接需要出现在小屏幕的顶部
【发布时间】:2021-09-30 01:29:47
【问题描述】:

我们的布局分为三个主要部分,顶部有主菜单,下面是主要内容和右侧的页面特定侧边栏。

主要内容和侧边栏由引导 col-* 类分隔。

当页面显示在小屏幕上时,我需要将右侧边栏移到主要内容上方,而不是下方(这是 bootstrap col 的默认行为)。一旦移动到顶部,还需要将其从 '' 转换为 ''。我试过 d-flex row-reversecol-reverse 但这并没有改变任何东西。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <main role="main">
        <div class="container">
            <div class="row d-flex">
                <div class="col-sm-12 col-md-8">
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                </div>
                <div class="col-sm-12 col-md-4">
                    <h3>Sidebar</h3>
                    <ul class="list-group">
                        <li class="list-group-item"><a href="#">Sidebar Link 1</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 2</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 3</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 4</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 5</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </main>

【问题讨论】:

  • 您希望边栏在长度小于或等于 576 像素的屏幕上移到顶部吗?通过应用 (sm),您可以对大于 576px 的屏幕应用样式

标签: javascript html css bootstrap-4


【解决方案1】:

我将class row 更改为flex-column-reverse

我们有预期的行为,主要内容上方的侧边栏。

之后可以使用media query来设置屏幕大小的功能。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
    <main role="main">
        <div class="container">
            <div class="d-flex flex-column-reverse">
                <div class="col-sm-12 col-md-8">
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                    this is the main content. this is the main content.this is the main content. <br />
                </div>
                <div class="col-sm-12 col-md-4">
                    <h3>Sidebar</h3>
                    <ul class="list-group">
                        <li class="list-group-item"><a href="#">Sidebar Link 1</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 2</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 3</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 4</a></li>
                        <li class="list-group-item"><a href="#">Sidebar Link 5</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </main>

【讨论】:

  • flex-column-reverse 之后添加类flex-md-column 将使其成为完整的答案。
【解决方案2】:

Bootstrap 提供了一种使用列排序实现此目的的简洁方法。这是引导程序 5 的 official documentation 的链接。基本上,这些列将从 1-6 排序。

请记住,引导程序首先是移动设备。我们应用order-1 使侧边栏出现在屏幕分辨率低于 576px 的设备上的主要内容之前。然后我们应用order-md-2 使侧边栏出现在屏幕分辨率超过768px 的主要内容之后。

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<main role="main">
  <div class="container">
    <div class="row d-flex">
      <div class="col-sm-12 col-md-8 order-2 order-md-1">
        this is the main content. this is the main content.this is the main content. <br /> this is the main content. this is the main content.this is the main content. <br /> this is the main content. this is the main content.this is the main content.
        <br /> this is the main content. this is the main content.this is the main content. <br /> this is the main content. this is the main content.this is the main content. <br /> this is the main content. this is the main content.this is the main
        content. <br /> this is the main content. this is the main content.this is the main content. <br />
      </div>
      <div class="col-sm-12 col-md-4 order-1 order-md-2">
        <h3>Sidebar</h3>
        <ul class="list-group">
          <li class="list-group-item"><a href="#">Sidebar Link 1</a></li>
          <li class="list-group-item"><a href="#">Sidebar Link 2</a></li>
          <li class="list-group-item"><a href="#">Sidebar Link 3</a></li>
          <li class="list-group-item"><a href="#">Sidebar Link 4</a></li>
          <li class="list-group-item"><a href="#">Sidebar Link 5</a></li>
        </ul>
      </div>
    </div>
  </div>
</main>

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 2017-07-28
    • 2018-07-30
    • 2017-07-31
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多