Two divs featuring a cohesive gradient background, with one div containing a sticky element

I am currently facing a dilemma that is causing me some frustration. The situation is as follows:

I have two divs on my webpage. The top div consists of a banner with the logo, while the bottom div is the navbar containing icons as links and a dropdown menu.

My goal is to achieve the following:

  1. Apply a consistent linear gradient background over both divs.
  2. Make the navbar stick to the top of the page when scrolling by setting its position to sticky with top: 0.

I have managed to accomplish each of these tasks separately, but I'm struggling to make them work together simultaneously. Is there a way to achieve this?

Below is a simplified version of the CSS and HTML code. The first version features the sticky navbar with separate gradient backgrounds for each div:

#banner,
.navbar {
  background-image: linear-gradient(to bottom right, rgb(0, 87, 128), rgb(0, 157, 230));
  padding: 5px;
}

.navbar {
  position: sticky;
  top: 0;
}

.navbar li {
  display: inline;
}

.container {
  height: 1000px;
}
<div id="banner" class="Container-fluid">
  <div class="banner-row">
    <h1>Logo!</h1>
  </div>
</div>
<div class="navbar">
  <ul class="nav navbar-nav">
    <li><a href="/home/"><span>Home</span></a></li>
    <li><a href="/about/"><span>About</span></a></li>
    <li><a href="/contact/"><span>Contact</span></a></li>
  </ul>
</div>
<div class="container body-content"><h1>Content!</h1><p>Stuff to fill a space.</p></div>

And here is an alternative approach where the header has a gradient background and the navbar is sticky:

header {
  background-image: linear-gradient(to bottom right, rgb(0, 87, 128), rgb(0, 157, 230));
  padding: 5px;
}

.navbar {
  position: sticky;
  top: 0;
}

.navbar li {
  display: inline;
}

.container {
  height: 1000px;
}
<header>
  <div id="banner" class="Container-fluid">
    <div class="banner-row">
      <h1>Logo!</h1>
    </div>
  </div>
  <div class="navbar">
    <ul class="nav navbar-nav">
      <li><a href="/home/"><span>Home</span></a></li>
      <li><a href="/about/"><span>About</span></a></li>
      <li><a href="/contact/"><span>Contact</span></a></li>
    </ul>
  </div>
</header>
<div class="container body-content"><h1>Content!</h1><p>Stuff to fill a space.</p></div>

Answer №1

To enhance the look of your navbar, consider using a pseudo-element with gradient coloration that overflows from the top to cover the banner area. Adjust the top value according to your preferred style for optimal results.

#banner {
  padding: 5px;
  position:relative;
  z-index:1;
}

.navbar {
  position: sticky;
  padding: 5px;
  top: 0;
}
.navbar:before {
  content:"";
  position:absolute;
  z-index:-1;
  left:0;
  right:0;
  bottom:0;
  top:-100px;
    background-image: linear-gradient(to bottom right, rgb(0, 87, 128), rgb(0, 157, 230));
}


.navbar li {
  display: inline;
}

.container {
  height: 1000px;
}
<div id="banner" class="Container-fluid">
  <div class="banner-row">
    <h1>Logo!</h1>
  </div>
</div>
<div class="navbar">
  <ul class="nav navbar-nav">
    <li><a href="/home/"><span>Home</span></a></li>
    <li><a href="/about/"><span>About</span></a></li>
    <li><a href="/contact/"><span>Contact</span></a></li>
  </ul>
</div>
<div class="container body-content">
  <h1>Content!</h1>
  <p>Stuff to fill a space.</p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance your viewing experience by magnifying a specific element, all while maintaining full control to navigate

Is it possible to create a zoom effect on a webpage that focuses on one specific element while still allowing for navigation and scrolling? I have searched online for plugins like Fancybox and Zoomooz, but none of them offer the functionality I need. I sp ...

textarea element enclosed by a div tag

I'm looking to add two side-by-side boxes on my page for comments. One box will display previous comments and should be disabled, while the other allows current users to leave comments. I achieved this by using two separate divs positioned next to eac ...

Is it possible to highlight the original 'anchor' or position of an element that has been moved using the float property?

Can the XXX be displayed using a CSS trick without the need for an extra HTML element or container? I attempted to use pseudo-elements and translate, but it was unsuccessful. p { background-color: grey; max-width: 200px;} small { float: right; margin-r ...

What is the method for a HTML button to trigger the execution of a Selenium (Java) code located in a remote location through a

I need to run a Selenium Code written in Java from a NAS (Network Attached Storage) or another connected machine. My goal is to create a web page with a button that, when clicked, triggers the execution of the Selenium script located on the connected mac ...

Instructions for setting a background image on the h:body tag using CSS in JSF

I have an image that is 250 x 25 in size. I am trying to display it on the page background with both repeat-x and repeat-y properties, but the code below is not working: <h:body style="background: url(/Images/logoback.jpg) repeat-x repeat-y"> ...

Div splits into two when positioned absolutely

Currently, I am playing around with Bootstrap 4 and have a card nested inside a card-columns: <div class="card-columns"> <div class="card"> <div class="card-block"> <h4 class="card-title">iPhone 5S</h4> ...

Adjust the height of a DIV element using Jquery Resizable to a minimum height of 1px, smaller than its default value

Having an issue with the Jquery UI Resizable functionality. I've implemented Jquery resizable to adjust a div's width and height dynamically. It's been working well, but I'm encountering a problem when attempting to decrease the height ...

The functionality to display dynamic images in Vue.js appears to be malfunctioning

As a newcomer to vue.js, I am facing a challenge in dynamically displaying images. Manually changing the images works fine, but I'm running into issues with dynamic display. I've come across similar problems online, but none of the solutions seem ...

Noticeable increase in load time post integration of Facebook SDK

I am in the process of integrating Facebook social buttons into my website. Feel free to visit the website here. Though I am new to using the Facebook API, I noticed a significant increase in load time after adding the code to the footer. After conductin ...

Secure login system featuring self-contained HTML and Javascript functionalities

I want to create an idle game that will save data on the server instead of using cookies. I am looking for a straightforward method (ideally without using MySQL or PHP) to implement user registration and login functionality using HTML5 and/or Javascript. ...

Is there a way to retrieve information from all pages of a table on a webpage using Chrome?

On a webpage, I have a table containing 100 rows that are displayed 20 at a time. By clicking "next page," I can view the following set of 20 rows and the table view is updated accordingly. I am able to access the data for each batch of 20 rows using dev ...

Tips for providing a link to a label and passing a query string to a different page in ASP.NET using C#

I am working on an asp.net page with SQL Server 2008 as the database. I have created three stored procedures; one to count the total number of users in tbl_users, another for counting the total male users, and the last one for counting the total female use ...

What could be causing me to have to click the button twice in order to view my dropdown list? Any suggestions on how to

I am facing an issue where I have to click twice on the "Action" button in order to see my dropdown list. How can I resolve this? Is there a way to fix it? $(document).ready(function() { $(".actionButton").click(function() { $dropdown = $("#cont ...

Retrieving information enclosed within a tag

I have been researching this topic and trying to implement various solutions, but I am not achieving the desired output. Here is the HTML code snippet: <div class="span-8"> <dl> <dt> <a title="A Coruña" href="http://www. ...

Do you think it's important to include a maxlength validation message for an input field in Angular?

Utilizing the maxlength attribute on a form field not only enables Angular's default validation, it also restricts input beyond the specified length from being typed into the text box. So, does this imply that displaying an error message for violating ...

What could be causing this jQuery to malfunction?

Having trouble with the functionality of hasclass and this.addclass. if ($('.sidebar-menu-container li').hasClass('current-menu-item')) { $(this).addClass('expanded'); } Check out this CodePen for reference. ...

Conceal the icon for the Dropdown Menu arrow

Is there a way to hide the old arrow icon in the Select (drop down) after changing it with a new one? HTML: <div class="row"> <div class="col-sm-12"> <select name="country" class="form-control SearchBar"> <option ...

Struggling to make images wrap properly using flexbox

I've been struggling to get these images to wrap properly within my container. They keep overflowing beyond the designated width and I'm not sure if it's because I have paragraphs placed under each image. Any ideas on how to make 3 images ap ...

Execute function when button is not active

Within my Angular 2 application, I have a form along with the following code snippet that relates to the button responsible for submitting the form. <button (mouseover)="showMesageValidation()" class="edm-button" type="submit" [disabled]="!f.valid"> ...

Interactive table on click

Seeking assistance with an issue involving a combobox (select) and a dynamic table generated from PHP. The table displays names along with their Firstname, Lastname, and ID (hidden). Upon clicking on a row in the table, I should retrieve the ID of that spe ...