Bootstrap对齐中心

时间:2018-03-09 13:35:49

标签: html css twitter-bootstrap twitter-bootstrap-3

我把品牌放在中心导航栏上,我希望导航到左侧并导航到右侧。所有这些工作都很好,但我有问题如何对齐左右导航中心,如下面的截图所示。enter image description here

这是我的代码:



.navbar {
    position: relative;
    height:150px;
}
.brand {
    position: fixed;
    top: 23%;
    left: 50%;
    /* bring your own prefixes */
    transform: translate(-50%, -50%);
    margin-left: -50px !important;  /* 50% of your logo width */
    display: block;
}

<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-dark bg-dark">
    <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>
    <a class="brand" style="margin: 0; float: none;" href="#">
        <img src="img/profile.png" width="250" />
    </a>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">

        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Почетна</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">О нама</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Мени</a>
            </li>
        </ul>


        <ul class="nav navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">Прославе</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Галерија</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Локација</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Контакт</a>
            </li>
        </ul>
    </div>
</nav>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您好,您可以尝试这样做以达到您想要的效果。

首先删除.mr-auto上的课程<ul class="navbar-nav">,然后添加

.custom-css<div class="collapse navbar-collapse custom-css" id="navbarSupportedContent">,因为.navbar-collapse已经设置为display:flex,我们现在只需将justify-content: space-around;设置为.custom-css

请参阅以下代码。

.navbar {
    position: relative;
    height:150px;
}
.brand {
    position: fixed;
    top: 23%;
    left: 50%;
    /* bring your own prefixes */
    transform: translate(-50%, -50%);
    margin-left: -50px !important;  /* 50% of your logo width */
    display: block;
}

.custom-css{
  justify-content: space-around;
}
<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-dark bg-dark">
    <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>
    <a class="brand" style="margin: 0; float: none;" href="#">
        <img src="img/profile.png" width="250" />
    </a>
    <div class="collapse navbar-collapse custom-css" id="navbarSupportedContent">

        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="#">Почетна</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">О нама</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Мени</a>
            </li>
        </ul>


        <ul class="nav navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">Прославе</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Галерија</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Локација</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Контакт</a>
            </li>
        </ul>
    </div>
</nav>

希望这有帮助

答案 1 :(得分:0)

你有2个ul类,在一个类中组合所有

  • 元素,然后使用Div类Container,然后使用justify content = center将所有元素放在中心。请参阅下面的代码。

    .container {
        display: flex;
        align-content: center;
        justify-content: center;
        height:150px;
    }
    .brand {
        position: fixed;
        top: 23%;
        left: 50%;
        /* bring your own prefixes */
        transform: translate(-50%, -50%);
        margin-left: -50px !important;  /* 50% of your logo width */
        display: block;
    }
    
    
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="style.css">
    
    <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-dark bg-dark">
        <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">
    
    
    
           <div class="container">
            <ul class="nav navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="#">Прославе</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Галерија</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Локација</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Контакт</a>
                </li>
                <li class="nav-item active">
                    <a class="nav-link" href="#">1</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">2</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">3</a>
                </li>
            </ul>
            </div>
        </div>
    </nav>
     Run code snippetReturn to post
    
    </body>
    </html>
    
  • 答案 2 :(得分:0)

    在徽标的左侧和右侧制作2个可折叠导航会更好(并且反应灵敏)......

    https://www.codeply.com/go/8xMhvER5At

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark position-relative">
            <div class="navbar-collapse collapse w-100 dual-collapse2 order-1 order-md-0">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Почетна</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">О нама</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Мени</a>
                    </li>
                </ul>
            </div>
            <div class="mx-auto order-0 mt-lg-5 mt-0 order-md-1 position-relative">
                <a class="mx-auto" href="#">
                    <img src="//placehold.it/250" class="rounded-circle">
                </a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                    <span class="navbar-toggler-icon"></span>
                </button>
            </div>
            <div class="navbar-collapse collapse w-100 dual-collapse2 order-2 order-md-2">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="#">Прославе</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Галерија</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Локација</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Контакт</a>
                    </li>
                </ul>
            </div>
    

    然后你只需要一点CSS导航高度:

    @media (min-width: 992px) {
        .navbar {
            height:150px;
        }
    }