I want a button in the navbar that:
- stays inline with other navbar items (doesn't go to the next line);
- sits outside of the collapsed navbar section; and
- remains centered on the page for all screen sizes, even when the right side of the navbar is collapsed.
My main challenges are:
- centering the button; and
- some navbar items not functioning properly on smaller screens (when navbar is collapsed).
Here is my code:
Another CSS attempt I made:
.navbar-button {
position: absolute;
left: 0;
width: 100%;
text-align: center;
white-space: nowrap;
}
This caused issues with some other navbar links. I also tried using z-index
, but it didn't work as expected.
body {
padding-top: 50px;
padding-bottom: 20px;
}
.section {
margin-bottom: 30cm;
}
.navbar-button {
display: inline-block;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Test</title>
<!-- Bootstrap Core CSS -->
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#brand">Brand</a>
<div class="navbar-button">
<button type="button" class="btn btn-primary navbar-btn">Button</button>
</div>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="text-center">
<a href="#link1">Link-1</a>
</li>
<li class="text-center">
<a href="#link2">Link-2</a>
</li>
<li class="text-center">
<a href="#link3">Link-3</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="section" id="brANd">
<h3>Top</h3>
</div>
</div>
<div class="row">
<div class="section" id="lInk1">
<h3>Section-1</h3>
</div>
</div>
<div class="row">
<div class="section" id="LInK2">
<h3>Section-2</h3>
</div>
</div>
<div class="row">
<div class="section" id="LiNK3">
<h3>Section-3</h3>
</div>
</div>
</div>
</body>
</html>