两端的Bootstrap Nav和Flexbox物品对齐问题

时间:2019-01-09 16:55:05

标签: html css twitter-bootstrap bootstrap-4

我在将导航栏中的项目移动到另一端时遇到困难。目前,我有这个: Navbar issue

我想将标志图像移到切换按钮旁边,仅当断点达到导航栏按钮大小992px时才存在。

我确定Bootstrap样式表正在使用justify-content: space-between;对其进行对齐,我曾尝试在标志和按钮上同时使用align-self: flex-end !important;以及justify-content-end

Codepen-https://codepen.io/jvern22/pen/gZjJBN

我的导航代码;     

  <div class="container container-navbar">

    <a class="navbar-brand" href="index.php">Website title</a>
    <a class="navbar-brand justify-content-end" href="#"><img src="assets/img/spain.png" class="navbar-flag"></a>

    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#top-navbar-1" aria-controls="top-navbar-1" aria-expanded="true" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="top-navbar-1">
      <ul class="navbar-nav justify-content-end">            
        <li class="nav-item"><a class="nav-link" href="?page=taketour"><?php echo $lang['nav_takeatour']; ?></a></li>
        <li class="nav-item"><a class="nav-link" href="?page=websites"><?php echo $lang['nav_websites']; ?></a></li>
      </ul>
    </div>        

  </div>
</nav>

2 个答案:

答案 0 :(得分:1)

您可以将标志和导航栏按钮包装在div中,如本CodePen或下面的代码片段所示:

html {
  height:100%;
  box-sizing: border-box;
}

body {
  min-height:100%; 
  padding:0; 
  margin:0; 
  position:relative;
  font: 400 15px "Open Sans", sans-serif;
	line-height: 30px;
  text-align: center;
  overflow-x: hidden;
	-webkit-font-smoothing: antialiased;
}

.bg-dark {
  background: #000 !important;
  color: #fff;
}

.bg-dark a {
  color: #aaa;
}

.bg-dark a:hover, .bg-dark a:focus {
  color: #fff;
}

.navbar-nav>li>a {
	line-height: 20px;
  padding-right: 12px!important;
  padding-left: 12px!important;
  padding-top: 10px!important;
  padding-bottom: 10px!important;
}

.navbar-flag {
  height: 20px;
}

@media (min-width: 992px) and (max-width: 2200px){

  .navbar-flag {
    display: none;
  }
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>


<nav class="navbar navbar-expand-lg fixed-top navbar-dark bg-dark navbar-no-bg" role="navigation">
    
      <div class="container container-navbar">     
        <a class="navbar-brand" href="index.php">Website title</a>
        
        <div>
          <a class="navbar-brand justify-content-end" href="#"><img src="
https://i.imgur.com/TXhhmGb.png" class="navbar-flag"></a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#top-navbar-1" aria-controls="top-navbar-1" aria-expanded="true" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
        </div>
        
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="top-navbar-1">
          <ul class="navbar-nav justify-content-end">            
            <li class="nav-item"><a class="nav-link" href="?page=taketour"><?php echo $lang['nav_takeatour']; ?></a></li>
            <li class="nav-item"><a class="nav-link" href="?page=websites"><?php echo $lang['nav_websites']; ?></a></li>
            <li class="nav-item"><a class="nav-link" href="?page=rates"><?php echo $lang['nav_rates']; ?></a></li>
            <li class="nav-item"><a class="nav-link" href="?page=contactus"><?php echo $lang['nav_contact']; ?></a></li>

          </ul>
        </div>        
        
      </div>
    </nav>

或者您也可以将父div的justify-content属性从space-between更改为flex-end并将margin-right: auto添加到第一个navbar-brand中,如下所示此CodePen或下面的代码段中:

html {
  height:100%;
  box-sizing: border-box;
}

body {
  min-height:100%; 
  padding:0; 
  margin:0; 
  position:relative;
  font: 400 15px "Open Sans", sans-serif;
	line-height: 30px;
  text-align: center;
  overflow-x: hidden;
	-webkit-font-smoothing: antialiased;
}
a.navbar-brand:first-child {
    margin-right: auto;
}
.bg-dark {
  background: #000 !important;
  color: #fff;
}

.bg-dark a {
  color: #aaa;
}

.bg-dark a:hover, .bg-dark a:focus {
  color: #fff;
}

.navbar-nav>li>a {
	line-height: 20px;
  padding-right: 12px!important;
  padding-left: 12px!important;
  padding-top: 10px!important;
  padding-bottom: 10px!important;
}

.navbar-flag {
  height: 20px;
}

@media (min-width: 992px) and (max-width: 2200px){

  .navbar-flag {
    display: none;
  }
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
        <nav class="navbar navbar-expand-lg fixed-top navbar-dark bg-dark navbar-no-bg" role="navigation">
    
      <div class="container container-navbar">
      
        <a class="navbar-brand" href="index.php">Website title</a>
        <a class="navbar-brand justify-content-end" href="#"><img src="
          https://i.imgur.com/TXhhmGb.png" class="navbar-flag"></a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#top-navbar-1" aria-controls="top-navbar-1" aria-expanded="true" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="top-navbar-1">
          <ul class="navbar-nav justify-content-end">            
            <li class="nav-item"><a class="nav-link" href="?page=taketour"><?php echo $lang['nav_takeatour']; ?></a></li>
            <li class="nav-item"><a class="nav-link" href="?page=websites"><?php echo $lang['nav_websites']; ?></a></li>
            <li class="nav-item"><a class="nav-link" href="?page=rates"><?php echo $lang['nav_rates']; ?></a></li>
            <li class="nav-item"><a class="nav-link" href="?page=contactus"><?php echo $lang['nav_contact']; ?></a></li>

          </ul>
        </div>        
        
      </div>
    </nav>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  </body>
</html>

答案 1 :(得分:0)

这里最简单的解决方案是仅扩展第一个flex项目,以便将所有其他项目推到右侧。只需在CSS中添加以下内容,即可轻松完成此操作:

.navbar-brand:first {
  flex-grow: 1;
}