使用Bootstrap 4调整窗口大小时移动div(垂直和水平)

时间:2018-02-11 19:38:03

标签: html css twitter-bootstrap bootstrap-4

CSS

.main{
    border-radius: 20px;
    margin-left: 20px;
    margin-right: 20px
}

.left{
    max-width: 250px;
    height: 1%;
    border-radius: 20px;
}

.right{
    max-width: 250px;
    height: 1%;
    border-radius: 20px;    
}   

.parent-container {
    display: flex;
}

HTML

<div class="parent-container">
   <div class="container left">
       <div class="row">
           Left content
       </div>
   </div>

    <div class="container card main py-3">
        <div class="container card">
            <div class="card-body">
                <div class="row">
                    <div class="col-lg-12 text-center">
                        <h2>TITLE2</h2>
                    </div>
                </div>
            <div class="row">
                <div class="col-md-12 text-center">
                    <a href="#" class="link1">
                      <img src="2.jpg">
                        <h3>Title3</h3>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="container right">
        <div class="row">
            <div class="right-content">
               Right content here
            </div>
       </div>       
   </div>
</div>

在全宽度上,我的页面布局如下所示:

[导航栏]
[1] [2] [3]

我在上一篇文章中被告知使用“order-last order-md-first”,但这对我没有帮助,因为当我调整窗口大小时,我会得到这样的结果:

[导航栏]
[2] [3] [1]

我正在寻找的效果是:

[导航栏]
[2] [3]

... [1]

([1]中的[1] div)

它是否可以实现?提前谢谢。

1 个答案:

答案 0 :(得分:1)

要实现您正在寻找的效果,除了订单类之外,还需要偏移类。

但首先是列:

下面代码中的

col-lg-3 - col-lg-6 - col-lg-3表示侧边栏列在较大屏幕上占用3个单位,中间列占6个单位(总共12个)。

在较小的屏幕上(小于lg),中间列获得8个单位(col-8),右列获得4个单位(col-4),总计达到12又一次。

这就是为什么左栏也必须有col-4并且需要在较小的屏幕上偏移8个单位,以便在较小的屏幕上将其直接放置在右栏下方。

这就是为什么我们需要左列的以下一组顺序和偏移类:

order-last order-lg-first offset-8 offset-lg-0

  • order-last是左列的默认顺序,即最小屏幕的顺序(Bootstrap 4是&#34;移动优先&#34;,所以最小的屏幕来吧)

  • order-lg-first说:&#34;从大屏幕(lg)起,你先来。&#34;

  • offset-8确定默认偏移量,即最小屏幕

  • offset-lg-0表示从大(lg)屏幕尺寸开始,不应有偏移(零偏移)。

这是工作代码段:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    Dropdown
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <div class="dropdown-divider"></div>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
            <li class="nav-item">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
        </form>
    </div>
</nav>
    
<div class="container mt-3">
    <div class="row">
        <div class="col-4 col-lg-3 order-last order-lg-first offset-8 offset-lg-0 bg-warning">
            <h2>1</h2>
            Left content <br>
            Left content <br>
            Left content <br>
        </div>
        <div class="col-8 col-lg-6 bg-primary">
            <h2>2</h2>
            Center <br>
            Center <br>
            Center <br>
        </div>
        <div class="col-4 col-lg-3 bg-secondary">
           <h2>3</h2>
            Right content <br>
            Right content <br>
            Right content <br>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;