With the implementation of bootstrap 4, I have successfully integrated a fixed navbar inside a container. Initially, at the top of the page, it occupies the full width, but as soon as I start scrolling, it smoothly transitions into the confines of the container.
Is there a way to ensure that when I scroll back to the very top, the navbar returns to its original position within the container?
I've also included a feature where the navbar changes color and background color when scrolled, highlighting the active section on the page.
<body data-spy="scroll" data-target="#navbarScroll">
<!-- Navigation Section -->
<div id="navbarScroll">
<nav class="navbar container fixed-top navbar-expand-lg navbar-light">
<a class="navbar-brand" href="#"></a><!-- removed brand name leaving only toggler -->
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navi" aria-controls="navi" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navi">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#portfolio">Portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#contact">Contact</a>
</li>
<ul>
</div>
</nav>
</div>
<!-- About Section -->
<div class="backgroundColor">
<div class="anchor" id="about"></div>
<div class="container">
<div class="row one">
<div class="col-md-12">
<img src="http://res.cloudinary.com/lyoon/image/upload/c_scale,h_350,w_350/v1503457656/02_27_16_2_u3qrdf.jpg" class="img-fluid profpic" alt="Caught a bass!">
</div>
<div class="description col-md-8">
<p>Front-End Developer and UX/UI Designer, with experience in HTML, CSS, and JavaScript. Proficient in Bootstrap for CSS, jQuery for JavaScript, and responsive web design.</p><hr>
<p>Passionate junior Web Developer who enjoys programming, music, and nature walks.</p>
</div>
</div>
<!-- Portfolio Section -->
<div class="anchor" id="portfolio"></div>
<div class="row two">
<div class="col-md-12">
<h3 class="titleport">Portfolio</h3>
</div>
<div class="col-md-12">
<p class="secondPara">Click on the image to view demo. Click the app title below the image to view the code.
<br />
All projects are created using HTML, CSS, and JavaScript.
</p>
</div>
</div>
<div class="row three">
<div class="col-md-6 img-section1">
<figure>
<a href="https://lawrenceyoon.github.io/Score_Keeper/">
<img src="http://res.cloudinary.com/lyoon/image/upload/c_scale,h_400,w_500/v1509214906/Screen_Shot_2017-10-28_at_11.21.21_AM_w2ieuq.png" class="img-fluid" alt="Score Keeper Game">
</a>
<figcaption><a href="https://github.com/lawrenceyoon/Score_Keeper">Score Keeper</a></figcaption>
</figure>
</div>>
// Rest of the portfolio entries shortened for conciseness
</div>
<!-- Contact Section -->
<div class="anchor" id="contact"></div>
<div class="row four">
<div class="col-md-12">
<h3 class="titlecontact">Contact</h3>
</div>
<div class="col-md-12">
<p class="contactme">Feel free to reach out via email if you wish to get in touch!</p>
</div>
</div>
</div><!-- End of Container -->
</div><!-- End Background Color -->
Below is the defined CSS style section...
/* General Styling */
body {
position: relative;
font-family: 'Slabo', serif;
font-size: 20px;
letter-spacing: 0.5px;
}
h3 {
font-family: 'Oswald', sans-serif;
font-size: 40px;
letter-spacing: 2px;
}
.container {
background-color: rgb(255, 255, 255);
}
.backgroundColor {
background-color: rgb(163, 167, 168);
}
.anchor {
position: relative;
top: -46px; /* Added for spacing after clicking on nav links so navbar doesn't overlap */
}
/* Navbar Styling */
.navbar {
background-color: white;
padding: 0;
}
// Active Link Styling
.navbar-light .navbar-nav .active>.nav-link, .navbar-light .navbar-nav
.nav-link.active, .navbar-light .navbar-nav .nav-link.show, .navbar-
light .navbar-nav .show>.nav-link {
background-color: rgb(163, 167, 168);
color: white;
}
/* About Section Styling */
.one {
text-align: center;
border-bottom: 1px solid gray;
margin-top: 46px; /* Necessary for preventing navbar from overlapping profile picture MUST MATCH .ANCHOR */
}
.profpic {
border-radius: 50%;
margin-top: 30px;
margin: 30px auto 0px auto;
}
.description {
margin: 30px auto;
}
/* Portfolio Section Styling */
.two {
text-align: center;
}
.titleport {
padding: 30px 0px;
margin: 0px;
}
p.secondPara {
margin-bottom: 30px;
}
.three {
border-bottom: 1px solid gray;
padding-bottom: 25px;
}
.img-section1, .img-section2, .img-section3, .img-section4 {
text-align: center;
}
img {
border-radius: 5%;
}
figure {
margin: 0px;
}
figcaption {
margin: 5px 0px;
}
/* Contact Section Styling */
.titlecontact {
padding: 30px 0px;
text-align: center;
margin: 0px;
}
.contactme {
text-align: center;
font-style: italic;
margin: 0px;
}
a {
color: inherit;
transition: all 0.5s;
-webkit-transition: all.5s;
-moz-transition: all 0.5s;
}
a:hover {
color: rgb(1, 193, 213);
text-decoration: none;
}
.iconContainer {
text-align: center;
margin: 30px 0;
}
a.icons {
text-decoration: none;
margin: 10px;
}