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

Customize the appearance of the date input box in MUI KeyboardDatePicker

Currently, I am attempting to customize the appearance of the KeyboardDatePicker component including board color, size, font, and padding. However, none of the methods I have tried seem to be effective. Here is what I have attempted so far: 1 . Utilizing ...

Tips for bringing in and taking out data in Vue

I have a set of files called total.vue and completeness.vue. I aim to display the chart from total.vue within completeness.vue, which is my designated admin dashboard where I plan to feature 2 or 3 additional charts. For this task, I will be exporting con ...

I am puzzled as to why I keep receiving the error message "Cannot read property 'poPanel' of undefined"

CSS In my project, I am implementing a feature that displays an ordered list by looping through an array of objects and adding them on a button click. It works smoothly for adding items, but when I try to implement a remove function to delete each item, I ...

Tips for toggling the visibility of an element when a button is clicked in React

In my todo list, I need to display the details of each item when clicked on a button. There are two buttons available: "View Details" and "Hide Details". Below is the code snippet: class Todos extends React.Component{ construc ...

What is the best way to format PHP emailed content from a web form?

Currently, I am working on a web contact page and facing an issue where the emails being sent arrive as plain text. I would like to enhance them by adding a background, incorporating the organization's logo, and styling the text to align with the webs ...

node index of data that has been posted

My HTML file is quite straightforward: <form method="post" action="http://localhost:3000/post"> <input name="name" type="text"/><br /> <input name="last_name" type="text"/><br /> <button id="submit" type="submit"& ...

Using ImageResource in ClientBundle to create an actual <img> element

Is there a way to inform ClientBundle that all images should be actual img elements instead of fake css-background images, as background images are not printed by default in IE9? ...

Tips for designing a navbar specific to a profile section

I am currently working on an angular application with a navigation bar composed of anchor tags. When the user logs in, I have an ngIf directive to display my profile icon with dropdown options. However, I am facing challenges in styling it correctly. I aim ...

Is there a way to link two HTML files containing jQuery within an HTML index file?

I'm currently working on creating an index page for a pair of HTML files that utilize jquery. Let's break down the structure of these files: Emps1, Emps2: These are two HTML tables that start off empty. TabA_stats, TabB_stats: Each HTML file ha ...

Eliminate the empty space that separates the text from the border

I'm looking to adjust the spacing between my number and its top and bottom borders. https://i.stack.imgur.com/UYFpX.png Currently, the number takes up the full width but leaves empty space above and below it. Any suggestions on how I can make it fi ...

Chrome reports a Javascript error: indicating that it is not recognizing the function

When working with a js file and html, I encountered an issue where one function works fine but another prompts an error in Chrome: Uncaught TypeError: specification_existing is not a function I'm puzzled as to why one function works while the othe ...

Create a right-to-left (RTL) CSS file within a create-react-app project and dynamically switch between them depending on changes

I am currently working on a multi-language project using create-react-app. My goal is to incorporate a library like "cssJanus" or "rtlcss" to transform the Sass generated CSS file into a separate file. This way, I can utilize the newly created file when sw ...

Tips for importing extensions with Rselenium

I could use some assistance in understanding how to activate a chrome extension using RSelenium. The extensions are visible in the browser tabs but are not automatically loaded when working with RSelenium. ...

The conversion of XML to HTML using setQuery(QString) in QXmlQuery is unsuccessful

Whenever I use the method setQuery(QUrl(file.xsl)), it functions properly. However, if I first load the file into a QString and then invoke setQuery(theString), the subsequent evaluateTo() operation fails (resulting in a boolean exception and empty result) ...

What is the best way to align an element based on the position of another element?

I have integrated a calendar icon above a fullcalendar, and when I click on the icon, a react-datepicker pops up. Here is my CSS : .react-datepicker { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 0.8rem; background-color: ...

Issue with Vue-gtag @legacy query not retrieving customer identification ID

Currently utilizing Vue2, I opted for vue-gtag@legacy as it's tailored for compatibility with version 2 of VueJS. Here is the setup in main.ts plugin: import VueGtagPlugin from "vue-gtag"; Vue.use(VueGtagPlugin, { config: { id: "G-MY ...

How can I enable a constant condition in eslint?

I have encountered an issue with my package.json configuration. Here is the current eslintConfig generated: "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/essential", "@vue/standard" ...

drawImage - Maintain scale while resizing

I am currently working on resizing an image using drawImage while maintaining the scale. Here is what I have so far... window.onload = function() { var c = document.getElementById("myCanvas"); var ctx = c.getContext(" ...

Two rectangular divisions placed next to each other, with one perfectly centered and the height of the second division adjusting to match the height of the

I attempted to implement the Matthew James Taylor method, utilizing three columns. However, my main issue arises when I desire the middle column to dictate the height. In other words, if the center div is 500px in height, I want the right div's height ...

Retrieving various properties of an object by matching IDs and displaying them without repeated reductions

I'm interested in finding a more efficient way to retrieve properties from a reduced object within a Vue component or wrapper DOM element. Let's say I have these two data objects in my component: player: [{ active: true, id: 0, name: &q ...