Bootstrap 4:如何将标头与包装器对齐

时间:2019-07-09 15:26:31

标签: bootstrap-4

我正在尝试将标题与1024px包装器对齐。以下是代码:https://codepen.io/anon/pen/wLRgyN我尝试将300x100对齐到每个包装纸的右侧,并在其左侧导航,以使其整体看起来均匀,并尝试保持其响应速度。

我尝试了填充,边距和浮点,但这似乎并没有解决问题

validators

2 个答案:

答案 0 :(得分:0)

如果您将container上的navbar-collapse移开,则不会弄乱下面的项目。

还添加.right会将导航定位在右侧。我只想删除较小屏幕上的绝对位置。

.border-top {
	border-top-width:5px !important;
}
.border-bottom{
	border-bottom-width:2px !important;
}
.border-dark{
	border-color:black !important;
}
.backgroundstart{
	background-image:url("https://i.imgur.com/ikxZHrk.png");
}

.navbar a {
	font-size:1.1rem !important;
	font-weight:bold ;
	text-transform:uppercase !important;
	color:black !important;
}

.navbar a:hover {
	color:#347F00 !important;
}

.right {
  float:right;
  position: absolute;
  right: 0;
  padding-right: 15px;
}
<!doctype html>
<html lang="de">
  <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.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="css/style.css">

    <title>Plauener Haarstudio</title>
  </head>
  <body>
<nav class="navbar navbar-expand-lg navbar-light bg-light border-top border-bottom border-dark">
  <a class="navbar-brand" href="#"><img class="w-50"src="https://i.imgur.com/YoN2enU.png"></a>
  <button class="navbar-toggler collapsed" 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="right navbar-collapse collapse" id="navbarSupportedContent" style="">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">start <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">über uns</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">kontakt</a>
      </li>
    </ul>
  </div>
</nav>
<div class="container">
<div class="row wrap">
<div class="col">
<img src="https://i.imgur.com/ikxZHrk.png" class="img-fluid">
</div>
</div>
</div>



    <!-- 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.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

答案 1 :(得分:0)

因此元素上已经有一些已经定义的flex类

尝试在徽标区域周围添加Div并为其提供一类flex-mobile-nav-有关此类,请参见下面的CSS

添加.nav-container-nav,其中包含徽标和导航选项-CSS也已更新

<div class="nav-container-nav"> //// Add this

<div class="flex-mobile-nav">
 <a class="navbar-brand" href="#"><img class="w-50"src="https://i.imgur.com/YoN2enU.png"></a>
 <button class="navbar-toggler collapsed" 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>
<div class="navbar-collapse collapse" id="navbarSupportedContent" style="">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">start <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">über uns</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">kontakt</a>
      </li>
    </ul>
  </div>

</div> //// end container

还要从.navbar-nav中删除.mr-auto类

<nav class="navbar navbar-expand-lg navbar-light bg-light border-top border-bottom border-dark">

并从(导航栏崩溃折叠)中删除.container

<div class="navbar-collapse collapse" id="navbarSupportedContent" style="">

添加到CSS

.navbar {
  display:flex;
  justify-content:space-between;
}
.navbar-collapse {
 flex-grow:0;
}

.navbar-expand-lg .navbar-collapse {
      justify-content: flex-end;
}
.flex-mobile-nav {
    display: flex;
    justify-content: space-between;
    width: 100%;
}
.nav-container-nav {
    max-width: 1024px;
    margin: 0 auto;
    width: 100%;
    display: flex; 
}
.col {
  background:#000;
  text-align:center;
}

Codepen:https://codepen.io/aystarz52/pen/RzEyaE?editors=1000