Fixed header in a div, allowing content to scroll when hovering over the header

How can I ensure that the header remains fixed at the top while allowing the content to scroll when hovered over? The current issue is that when a user hovers over the header and tries to scroll, the content does not move.

body {
        margin: 0;
    }
    .wrapper {
        width: 60%;
    }
    .header {
        position: fixed;
        top: 0;
        width: calc(60% - 2em);
        padding: 1em;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #e5e5e5;
    }
    .content {
        max-height: 300px;
        overflow-y: auto;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        padding-top: 3.5em;
    }
    a.tile {
        width: calc(33.333% - 32px);
        height: 200px;
        margin-bottom: 6px;
        padding: 12px;
        text-align: center;
        border-radius: 12px;
        border: 1px solid #efefef;
    }
<body>
    <div class="wrapper">
        <div class="header">
            <span>9 of 25</span>
            <div class="links">
                <a href="#" disabled>Previous</a>
                <a href="#">Next</a>
            </div>
        </div>
        <div class="content">
            <a href="#" class="tile">Tile 1</a>
            <a href="#" class="tile">Tile 2</a>
            <a href="#" class="tile">Tile 3</a>
            <a href="#" class="tile">Tile 4</a>
            <a href="#" class="tile">Tile 5</a>
            <a href="#" class="tile">Tile 6</a>
            <a href="#" class="tile">Tile 7</a>
            <a href="#" class="tile">Tile 8</a>
            <a href="#" class="tile">Tile 9</a>
        </div>
    </div>
</body>

The example demonstrates how the body will scroll when hovering over the header, but the content div does not. This issue is more commonly observed in full-screen view.

Answer №1

One issue causing the lack of scrolling when hovering over the header is that the header element is not a child of the scrollable container, resulting in scroll events not being triggered.

A simple solution involves moving the header into the overflowing container. By doing this, scroll events will automatically be handled. Additionally, applying the CSS property position: sticky to the header will ensure that the browser reserves enough space for it without requiring you to define a top padding. Check out the example below to see how this works:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.wrapper {
  width: 60%;
}

.header {
  position: sticky;
  top: 0;
  width: 100%;
  padding: 1em;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #e5e5e5;
}

.content {
  max-height: 300px;
  overflow-y: auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

a.tile {
  width: calc(33.333% - 32px);
  height: 200px;
  margin-bottom: 6px;
  padding: 12px;
  text-align: center;
  border-radius: 12px;
  border: 1px solid #efefef;
}
<body>
  <div class="wrapper">
    <div class="content">
      <div class="header">
        <span>9 of 25</span>
        <div class="links">
          <a href="#" disabled>Previous</a>
          <a href="#">Next</a>
        </div>
      </div>
      <a href="#" class="tile">Tile 1</a>
      <a href="#" class="tile">Tile 2</a>
      <a href="#" class="tile">Tile 3</a>
      <a href="#" class="tile">Tile 4</a>
      <a href="#" class="tile">Tile 5</a>
      <a href="#" class="tile">Tile 6</a>
      <a href="#" class="tile">Tile 7</a>
      <a href="#" class="tile">Tile 8</a>
      <a href="#" class="tile">Tile 9</a>
    </div>
  </div>
</body>

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

What is the process for integrating three.js code manually into an iframe?

I recently posted a question on Stack Overflow inquiring about how to input code into an iframe without using a file or URL. While I was successful with simple cases like <h1>Hello World</h1>, my ultimate goal is to integrate three.js into thes ...

Displaying limited items based on conditions in CSS

If there are 10 <p> tags, is it possible to limit them to only 5 by hiding the other 5 using CSS? Do I need to add a class five times with nth-child? Can CSS accommodate conditions for this purpose? Considering that the number of <p> tags is ...

Having trouble updating values in Vue3 when accessing the next item in an object?

I'm attempting to allow my users to browse through a collection of various items. Take a look at the records object below: 0: {id: 1, pipeline_id: 1, raw: '1', completion: null, processed: 0, …} 1: {id: 2, pipeline_id: 1, raw: '2&apo ...

Looking for guidance on sending data from a JS file to HTML using Nodejs? Seeking advice on various modules to achieve this task effectively? Let's

Looking for advice on the most effective method to transfer data from a JS file (retrieved from an sqlite db) to an HTML file in order to showcase it in a searchable table. My platform of choice is NodeJS. As a beginner, I am willing to put in extra time a ...

The loading time for the API response in Laravel is unacceptably slow

When I run my API locally, it works fine. However, when I deploy it to run on a server, it becomes unusually slow. Is this slowness due to my server and how can I check that? Here are some of the responses I received: // Vue import axios from '@axio ...

Tips for making a Bootstrap dropdown submenu appear as a 'dropup'

I'm using a Bootstrap dropdown menu with a submenu that should drop upwards while the rest of the menu drops down. How can I achieve this? Here is the code snippet: <div class="dropdown"> <a class="inputBarA dropdown-toggle" data-toggle= ...

What is the method for inserting a vertical line between two div elements?

Can you show me how to add a vertical line between two div elements? .flex-container { display: -webkit-flex; margin: 0 auto; text-align: center; } .flex-container .column { width: 320px; text-align: center; } .vr { border-left: 2px dott ...

Exploring the power of Vue with Cypress testing and streamlining the development process

Currently, I am facing a challenge while trying to run my E2E tests on Gitlab using their CI/CD platform. The issue I'm encountering is the inability to have both my development server and Cypress running simultaneously in order for the E2E tests to ...

Sort PHP results by the initial letter of the name in a stylish manner

Hello, I am attempting to organize categories by the first letter and format them using ul and li elements. Here is my code: $Sql = "SELECT *, COUNT(Cup_Id) AS num FROM tabcup INNER JOIN tabcats ON tabcupom.Cat_Id = tabcats.Cat_Id ...

Guide on combining item costs and showing the total price using Vuejs and Vuex

I need help with writing code in vuex to pull data as a getter from Cart View. Specifically, I want to display the total price of chosen products and ensure that it is added up and displayed correctly. Any guidance on how to achieve this would be appreciat ...

Testing using Vitest in the Vue 3 Suspense wrapper: A comprehensive guide

I experimented with various methods, such as utilizing await flushPromises(); or using await nextTick(); or trying component.vm.nextTich() Unfortunately, none of these approaches seem to be effective! ...

Having trouble with AJAX within AJAX not functioning properly?

Similar to the Facebook commenting system, I am aiming for comments to be appended to previous ones and displayed all at once. However, currently the comments only show up after the page is reloaded. Please Note: Each post initially loads with just two co ...

Adjust the background color of the date and time selection tool

Trying to customize the background of a datetime picker created using jquery.pttimeselect.js. Despite linking the correct css file (jquery.pttimeselect.css), I am unable to change the background color. Any suggestions on how to successfully modify the ba ...

What is the purpose of activating hover styles when tapping on mobile devices?

On my button, you can see the color change when hovering: button { background-color: orange; } button:hover { background-color: darkorange; } Check out this example: https://jsfiddle.net/oepb5ks4/ While this works well on desktop, it's a diffe ...

Enable and disable modal popup functionality in ng-bootstrap for Angular 5

How do I enable (editable) and disable (not editable) an ng-bootstrap modal popup? I have the following HTML code to display a modal popup when a button is clicked. However, I want it to be inactive until a certain condition is met. I am unsure of how to ...

Looping through multiple lines of output in VueJS

Looking for a solution to optimize my for loop <div class="w3-panel w3-blue w3-card-4" v-for="peer in peers" v-bind:key="peer" > {{ peer.hop }} {{ peer.time }} </div> ...

Why can't the file upload button locate its CSS styles?

Check out this jFiddle demonstrating the issue. Some of the Angular code may appear broken in the example, but that's just due to it being pasted into jFiddle; it's not an actual problem I'm facing. The CSS styles aren't working on the ...

Issue in VueJs where mutations do not properly save new objects to the state

I am facing an issue with updating my vuex store after modifying my user credentials in a component. Below is the code snippet for reference: mutations: { updateUserState: function(state, user) { state.user = user; }, } actions: { updat ...

Determining the container height for every two-image row

I am currently working on a project for this website. My focus right now is on resizing vertical images using the following script: function Gallery(selector) { this.add_module = function (type, image) { var portrait_text = image.next('.portr ...

I aim to design a unique child window specifically for an "about" section within an electron js application on the Windows platform

I am looking to create a child browser window to showcase some key points about my application. According to the Electron JS documentation, it supports the "about" role for Mac OS but does not have built-in support for Windows. Therefore, I am in the pro ...