CSS/SCSS/JS: Adjusting header height dynamically while keeping it fixed at the

Check out my Codepen demo here: https://codepen.io/nickfindley/pen/dJqMQW

I am seeking to replicate the current behavior of the page without specifying a fixed height for the header. The goal is to make the header adjust dynamically based on the content, adapt to window resizing, and be hidden when scrolling down the page. Additionally, I want the navbar to stick at the top using some JavaScript.

var pn = $(".page-nav");
pns = "page-nav-scrolled";
hdr = $(".page-header").height();

$(window).scroll(function() {
  if ($(this).scrollTop() > hdr) {
    pn.addClass(pns);
  } else {
    pn.removeClass(pns);
  }
});

$(window).on("scroll load", function() {
  pn.toggleClass(pns, $(this).scrollTop() > hdr);
});
body {
  margin: 0;
  padding-top: 10em;
}

.page-header {
  background-color: yellow;
  text-align: center;
  padding: 2em 0;
  position: fixed;
  top: 0;
  width: 100%;
  height: 20em;
  z-index: -100;
}

.page-nav {
  background-color: black;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.4);
  width: 100%;
  height: 4em;
  position: relative;
  margin-bottom: -4em;
  z-index: 10000000;
}
/* Other CSS styles... */

/* Include references to jQuery library and main HTML structure */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="page-header" role="banner">

  <section class="header-content">
    <h1>Fixed Header</h1>
    <p>Lorem ipsum dolor sit amet...</p>
  </section>

</header>

<nav class="page-nav" role="navigation">

  <div class="nav-wrapper">
    /* Navbar HTML content goes here */
  </nav>

<main class="page-main">
  <section class="page-content">
    <article class="entry">
      <header class="entry-header">
        <h2>Quisque a dolor vel leo porttitor luctus</h2>
      </header>

      <section class="entry-content">
        <p class="type">Lorem ipsum dolor sit amet...</p>
      </section>
    </article>
  </section>
</main>

I would prefer a pure CSS solution, but I'm also open to suggestions involving JavaScript.

Answer №1

Click on the following codepen link to see my adjustments:

  • Eliminated any body padding
  • Swapped out position fixed for relative positioning
  • Got rid of the fixed height

I am confident that these changes align with your requirements.

To view the modified CSS sections, click this link: https://codepen.io/anon/pen/vpvmxo

Below are the revised CSS snippets:

body {
  margin: 0;
  //padding-top: 10em;
}

.page-header {
  background-color: yellow;
  text-align: center;
  padding: 2em 0;
  position: relative;
  top: 0;
  width: 100%;
  //height: 20em; // not really necessary, visible portion of header is determined by body padding-top
  z-index: -100;
}

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

Ways to conceal the scroll bar upon the initial loading of a webpage

Recently, I created a single-page website consisting of 4 sections. The client has specific requirements that need to be fulfilled: The scrollbar should not be visible when the page loads. Once the user starts scrolling past the second section, the scrol ...

Obtain the final array within an array composed of multiple arrays

My dilemma involves working with a dynamically generated array, where I need to extract only the values from the last array for aggregation purposes, discarding all others. Imagine a structure similar to this: let target = [] let data = [ [ { name: &apo ...

Forwarding to a specific webpage

`<!DOCTYPE html> <html class="no-js"> ... </body> </html> I'm in the process of developing a website where users can enroll in programs by clicking on an ...

Navigating and extracting nested JSON properties using JavaScript (React)

I am currently working with a nested JSON file that is being fetched through an API. The structure of the JSON file looks something like this: { "trees":{ "name":"a", "age":"9", "h ...

How can I change the background color of a parent div when hovering over a child element using JavaScript

I am working on a task that involves three colored boxes within a div, each with a different color. The goal is to change the background-color of the parent div to match the color of the box being hovered over. CSS: .t1_colors { float: left; wid ...

Unable to display the Error 404 Component within a nested Component using React Router

When attempting to display the Error component if no matches are found, I encountered an issue - selecting a non-existing route only leads to a blank page. In the example, the Main component adds a sidebar menu and renders all its children inside it. If ...

steps to attach click event to row of a-table component in ant design vue

Here is the code snippet for the table I am working on. I have imported a-table from antd. To enable click functionality to route to a details page from this listing page, an additional td can be added. <a-table :columns="companiesColumns" :d ...

Tips for expanding the boundary of a Font Awesome icon

The image is in a h1 editing margin of h1 only edits margin-left increases the margin on the left of the image. trying to increase margin of i element does nothing either on i element does nothing either. <h1 class="display-4"><i class="fas fa- ...

"Learn how to compile a single jade file using grunt-jade instead of compiling all files at once

In my jade.js file, the code looks like this: 'use strict'; var config = require('../config'); module.exports = { dist: { options: { pretty: true, debug: false, timestamp: '<%= new Date().getTime() %>&apo ...

Visual Studio cannot find the reference for require, resulting in an error in Node.js

I'm currently working on developing a web application using Visual Studio, Node.js, and React.js var fs = require('fs'); var path = require('path'); var express = require('express'); Unfortunately, I encountered an erro ...

Designing an HTML banner with 100% width and 660 pixels in height for optimal responsiveness

I am in need of a responsive banner that will be displayed at 100% width and 600px height on fullscreen. Below is the code I have: <style> /* default height */ #ad { height: 660px; width: 100%; } @media o ...

Incorporating a javascript library or plugin into Vue.js code

Hey there, I recently came across the really cool component called vue-social-sharing. It's my first time using a library like this and I'm a bit confused on how to set it up properly. I've installed it through NPM, but when it comes to the ...

Calling Node Express request inside a GET route

I am currently utilizing nodejs as an intermediary layer between my public website and an internal server within our network. Through the use of express.js, I have created a basic REST api where the endpoint should trigger a request call to a webservice a ...

When the button is clicked, my goal is to increase the value of the text field by one using JavaScript

I need the functionality for each button to increment the value text field located next to it <input type="number" value="0" min="0" max="5" step="1" id="qty"/> <button onclick="buttonClick()" type="button" class="btn btn-success btn-sm" id="add" ...

Error encountered when processing a PUT request for a node causing a

After receiving minimal views and replies on a similar question I posted last night, I am reaching out again in hopes of resolving my issue. For the past two days, I have been struggling to fix this problem and progress with my project! If you are interes ...

Tips for retrieving specific values from drop-down menus that have been incorporated into a dynamically-sized HTML table

How can I retrieve individual values from dropdown menus in HTML? These values are stored in a table of unspecified size and I want to calculate the total price of the selected drinks. Additionally, I need the code to be able to compute the price of any ne ...

Choose a select few checkboxes and then disable the remaining checkboxes using Vue and Laravel

I'm currently working on a project using Laravel 10 and Vue3. In the form, users are allowed to select only 3 checkboxes. Once they have selected 3 checkboxes, all remaining checkboxes should be disabled. I attempted to implement this functionality a ...

Steps for building JSX with a loop

Recently diving into the world of React and Javascript, I've been attempting to assemble a JSX 'tree' dynamically using a loop rather than hard-coding the data. However, I find myself facing a hurdle where Visual Studio Code insists on havi ...

Deliver JSON from Node.js to the client

I am a beginner in the world of Node.js and JavaScript, facing a challenge that I can't seem to overcome. Currently, I have a Node application set up with Express. The issue at hand involves a script that sends JSON data to my server, which is then s ...

Issue "RangeError: minimumFractionDigits value is invalid" when using ChartJS in a Next.js application

I'm currently working on developing an application utilizing react-chartjs-2.js. The functionality is smooth in my local environment, but when moved to production, I encounter the following error: Application error: a client-side exception has occurre ...