html - How to have CSS border span the length of the page but have elements remain inside container?
问题描述
I am trying to create a navigation bar. I have used bootstrap for the body of my site and would like the navigation bar to span the same width. However, I want the border to span the full width; so that the elements 'Home, Learn, Progress, etc' appear to be right above the things in the actual page but not so that the border is cut off.
<div class="navbar container">
<div class="navbar-left">
{% url 'quizapp-home' as url %}
<a href='{{ url }}' {% if request.path|slice:":6" == url %} id="current" {% endif %}><i class="fas fa-home"></i> Home</a>
{% url 'quizapp-learn' as url %}
<a href='{{ url }}' {% if request.path|slice:":7" == url %} id="current" {% endif %}><i class="fas fa-pencil-ruler"></i> Learn</a>
{% url 'progress' as url %}
<a href='{{ url }}' {% if request.path|slice:":10" == url %} id="current" {% endif %}><i class="fas fa-chart-line"></i> Progress</a>
</div>
<div class='navbar-right'>
{% if user.is_authenticated %}
{% url 'profile' as url %}
<a href='{{ url }}' {% if request.path|slice:":9" == url %} id="current" {% endif %}> Profile</a>
{% url 'logout' as url %}
<a href='{{ url }}' {% if request.path|slice:":8" == url %} id="current" {% endif %}>Logout</a>
{% else %}
{% url 'login' as url %}
<a href='{{ url }}' {% if request.path|slice:":7" == url %} id="current" {% endif %}>Login</a>
{% endif %}
</div>
</div>
.navbar {
background-color: #ffffff;
overflow: hidden;
font-family: "Century Gothic", Century, sans-serif;
font-weight: bold;
list-style-type: none;
border-bottom: 2px solid #ccc;
/* box-shadow: 2px 2px 10px grey; */
}
.navbar a {
float: left;
color: #cccccc; /* light grey */
text-align: center;
padding: 10px 12px;
text-decoration: none;
font-size: 22px;
transition: all 0.3s ease; /* Add transition for hover effects */
}
.navbar a:hover {
color: #737373; /* dark grey */
}
.active {
color: #34a7e0; /* blue */
}
#current {
color: #34a7e0; /* blue */
}
.navbar-left a {
margin-left: 10px;
margin-right: 20px;
}
.navbar-right a {
margin-right: 10px;
float: left;
font-size: 18px;
}
解决方案
只需将内容包装.navbar
成.container
:
<nav class="navbar">
<div class="container">
<a class="navbar-brand" href="#">Navbar</a>
<!-- Your regular navbar content here, aligned with the page contents... -->
</div>
</nav>
记录在这里。无需自定义 CSS。
工作示例:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<body>
<nav id="navbar-example2" class="navbar navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Navbar</a>
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="#fat">@fat</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#mdo">@mdo</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#one">one</a>
<a class="dropdown-item" href="#two">two</a>
<div role="separator" class="dropdown-divider"></div>
<a class="dropdown-item" href="#three">three</a>
</div>
</li>
</ul>
</div>
</nav>
<main>
<div class="container">
<h1>Hello, world!</h1>
</div>
</main>
</body>
推荐阅读
- python - 在终端中输入“conda list”时列出了什么?
- c# - 如何稍后用行和其他列填充一个数据表列?
- r - 使用 quantreg 获取模型的显着性水平(p 值)
- php - PHP - 只有在迭代之间发生变化时才循环遍历数组并打印出值
- android - 从文件描述符上的相机错误返回图像
- python - 如何在菜单栏中实现刷新按钮以刷新所有组合框项的原始状态?
- python - Keras LSTM 拟合欠拟合
- javascript - Angular:使用 ControlValueAccessor 自定义输入
- python - 如何将行数堆叠到一行并分配id
- mongodb - .gitlab-ci.yml 中的 Mongodb 副本集