Attach the div element to the bottom of the header element

When it comes to headers, we often use position: fixed; to keep the header fixed at the top of the page. But what if we need two headers - one main header and one smaller header for secondary links? After some research, I discovered a tip that suggests using position: relative; in the parent element style (the main header) and position: absolute; bottom: 0; in the child element style (the div). However, this results in the main header becoming unstuck from the top. One solution could be to add

margin-top: (height of the header);
to the div stylesheet. This may work on your current screen resolution, but on another device, where the height of the main header varies, the second header (div) might end up being positioned too low.

https://i.sstatic.net/Nzmmb.png

One possible workaround is shown below, but even then, the size will vary depending on the device's resolution:

https://i.sstatic.net/XMhrD.png

So, my question remains - How can we properly affix the div element to the bottom of the header element?

Answer №1

To achieve a sticky or fixed position for both headers, you can set the top property of the main header to 0 and the second header to the height of the main header. This height can be specified as a fixed value like 10px, or as a relative value like 10% or 10vh.

If using a relative value such as 10%, ensure that the ancestor of both headers has position: relative and occupies the full vertical space. Alternatively, using 10vh simplifies this process as it always refers to 10% of the viewport's height regardless of the stacking context.

/* Styles for achieving sticky/fixed positioning */
.header {
  display: flex;
  align-items: center;
  height: 10vh;
  position: sticky;
}

#primary-header {
  top: 0;
  background-color: green;
} 

#secondary-header {
  top: 10vh;
  background-color: red;
}

section {
  height: 5000px;
  background: linear-gradient(to bottom, black, white);
  color: white;
}
<header class="header" id="primary-header">Header 1</header>
<div class="header" id="secondary-header">Header 2</div>
<section>Scroll down...</section>

In more complex scenarios, utilize the calc() function to calculate the sum of various heights, paddings, margins, etc., of the first header in order to determine an appropriate top value for the second header:

/* Additional styles for accounting for varying heights */
.header {
  display: flex;
  align-items: center;
  height: 10vh;
  position: sticky;
}

#primary-header {
  top: 0.5rem;
  padding: 1rem;
  margin-top: 0.5rem;
  background-color: green;
} 

#secondary-header {
  /* Calculate total height for proper positioning */
  top: calc(10vh + 2.5rem);
  background-color: red;
}

section {
  height: 5000px;
  background: linear-gradient(to bottom, black, white);
  color: white;
}
<header class="header" id="primary-header">Header 1</header>
<div class="header" id="secondary-header">Header 2</div>
<section>Scroll down...</section>

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

How to dynamically add a column to a Wicket table

HomePage.java Table table = new Table(); List<String> listOfString = (List<String>) table.getList(); //accessing list from Table.java System.out.println("listOfString" + listOfString); //List -[name, abc, id, 101, photo, true, na ...

Learn how to join a comma with a space in AngularJS

My sequence of strings is "aby,abraham,issac,rebecca,job,david,daniel" and I want to insert a space after each comma. I am assigning the value using ng-bind and showing the result using ng-show. I cannot use the join function as the data is retrieved as a ...

Exploring the Depths of HTML with Jquery

This is an example of HTML code I am working with: <tr> <td> <div><span id="temp" /> </div> </td> </tr> <tr> <td> <div><span id="temp" /> </di ...

Utilize CSS styles exclusively when the user has JavaScript enabled

For my website to gracefully degrade, I need to conditionally load CSS only when its corresponding JavaScript is enabled and functioning properly. What is the most straightforward method to load local CSS if and only if the browser supports JavaScript? T ...

Sticky full-height slider with horizontal scrolling

I've been working on creating a horizontal slider animation that smoothly scrolls, inspired by the one showcased on The slider on weltio remains sticky as you scroll, showcasing images and content within. On my page, I have a lot of content and my g ...

In Android, make sure to include a newline after a heading when using the <h3> tag

Lately, I've been experimenting with HTML tags on my Android device. I noticed that when I use the <h3> tag, it adds a line space automatically before the next line. In HTML, you can remove this space by setting the padding and margin to 0px. Ca ...

css3 perspective for text with a sleek appearance

I am currently facing a rather simple issue. I have a 3D container with text inside it, here is an example: .container{ transform: perspective(120px) rotateY(45deg); } <div class="container"> Lorem ipsum dolor sit amet consectetur adipisicing ...

The issue of selecting multiple classes simultaneously is not being resolved

Here is my JavaScript code snippet: $('.normal-box').click(function(){ //unique row id var id = $(this).data('row-id'); //its index according to the content array var index = $(this).data('index'); //which car ...

How to access a bootstrap 4 tab panel on a different page

How can I successfully open a specific tab panel on the service.html page when clicking a link on my index page? I'm using bootstrap nav-pills and have tried the following, which unfortunately did not work as expected: service.html <div class="na ...

Is it possible to conceal glyphicons by employing the attribute hidden="hidden"?

Is it possible to conceal a glyphicon icon in this manner? <i class="glyphicon glyphicon-remove-circle" title="Please add a suitable value" id="author" hidden="hidden"></i> <p>Author</p> ...

Image with Responsive Textarea in Bootstrap

Having a bit of trouble figuring things out. Hi everyone! I have a basic frontend setup with a navbar and two columns below. The left column has a fixed size of 80px, while the right one contains an image (making it fluid/responsive). The issue I'm ...

What is the best way to create a search box in AngularTS?

My search box code is not functioning properly. Can someone please assist me in fixing it? html: <form #f="ngForm" (ngSubmit)="onSubmit(f)" #searchForm="ngForm"> <input type="search" placeholder="Search..."> </form> search-form Compo ...

Encountering an issue with the router that is unable to correctly read the head.html and tail

As a student diving into Express, I have come across a slight issue that I need help with. Within my routes/index.js file, the following code block exists: router.post('/orders.html', function(req, res, next) { var fhead = __dirname+ "/ ...

org.openqa.selenium.NoSuchElementException: The Label element could not be found with the specified XPATH

When it comes to handling the test payment gateway pop-up, I seem to be having an issue with this particular block of HTML code: <div class=""> <div class="legend">Select payment method</div> <div id="payment-options" class="grid cl ...

"Automatically scroll back to the top of the page once new content

Is there a way to automatically scroll the page to the top after content has been loaded via Ajax? Here is the code I am using to display the content: $(document).ready(function () { var my_layout = $('#container').layout(); $("a.item_l ...

Remove elements from an array based on the indices provided in a separate array

1) Define the array a = [a,b,c,d,e,f,g,h,i,j]; using JavaScript. 2) Input an array 'b' containing 5 numbers using html tags. This will be referred to as the 'b' array. 3) Insert elements into array 'b' ensuring they are alwa ...

Is it achievable to use multi-level :not selectors in CSS / JS querySelector?

I have come across numerous tutorials explaining the use of multiple :not selectors on a single element. However, I have yet to find any examples of using :not selectors on different levels within a single CSS / JS querySelector path. For instance, conside ...

I am eager to demonstrate the utilization of AngularJS in displaying hierarchical table rows

I am facing an issue with the code I wrote to display a tree-structured table. It's not working as expected and I need some assistance. The goal is to show only rows in a tree structure, not the entire table inside a table row. <script type="text/ ...

Creating a unique seat arrangement resembling airplane wings

I am facing a challenge of creating a seatmap for airplanes using only HTML, CSS, and jQuery. While this task is generally manageable, I'm struggling with how to incorporate wings into the design. Currently, I differentiate seats located over the wing ...

Detecting if a physical keyboard is connected using CSS media query

Imagine having a website with keyboard shortcuts, where the goal is to visually display these shortcuts as part of the element they correspond to. When it comes to touch interfaces, especially on mobile phones, this may not be necessary because: Most us ...