Is it possible to determine the height of a div that has been assigned `overflow-y: auto`?

Is it possible to determine the height of a div element with its content without using overflow-y: auto?

I would like to keep overflow-y: auto applied but still be able to calculate the height of the div as if the overflow property was not there.

Specific Scenario:

Consider a chat window with text and image messages. The chat div has overflow-y: auto and at the bottom, there is another div with ref={scrollToBottomRef}

There is an useEffect hook:

  useEffect(() => {
    scrollToBottomRef.current.scrollIntoView({
      behavior: 'smooth'
    });
  });

It functions correctly, scrolling to the bottom when new messages are loaded. However, if an image is included in a message, the scroll jumps up from the bottom after rendering the image. How can this issue be resolved?

Answer №1

@Scrimothy highlighted the functionality of Element.scrollHeight, emphasizing its compatibility across various browsers without encountering any problems. This property takes into account padding but disregards border and margin. When the content within the div does not occupy the entire space, it will report the div's height accurately.

Answer №2

Recently, I made a modification to scrollIntoView within my useEffect function. Surprisingly, it performed as anticipated even without utilizing smooth scroll.

  useEffect(() => {
    scrollToBottomRef.current.scrollIntoView();
  });

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

Bootstrap Modal closing problem

While working on a bootstrap modal, I encountered an issue where the modal contains two buttons - one for printing the content and another for closing the modal. Here is the code snippet for the modal in my aspx page: <div class="modal fade" id="myMod ...

The state initialization process is beginning with an [object Object] being passed into the setState function

Within the async function provided below, I am invoking stationData to verify that an array of objects is being passed into bartData (which currently consists of an empty array). Upon exploration, I have observed a response containing the anticipated array ...

"Experiencing the inconvenience of having to manually refresh Next JS 13 in order to

After updating to Next.js 13, I've noticed that I have to refresh the screen every time I make changes to my files. It's quite inconvenient having to hard refresh constantly whenever I update something. This is my current configuration for Next ...

The maximum height of the MUI TextField Select Menu

Is there a way to specify a maximum height for the menu in a MUI TextField with select property? I am aiming to set a limit on the menu's height: <TextField fullWidth label="Select State" name="state" ...

What is the best way to eliminate all hover effects on smaller devices?

What is the solution to remove hover effects on elements in CSS? Consider the following HTML and CSS code: @media (max-width: 1220px) { .column { width: 200px; } .para { display: none; } .link:hover { color: red; } } @media (ma ...

The combination of Google Maps and CSS transforms causes a blur effect on the page

After implementing the CSS Transform technique to center a div both horizontally and vertically, the desired effect was achieved. However, an unexpected issue arose on pages that contained a Google Maps iframe causing the entire page to go blurry. To view ...

Tips for Positioning Ant Design Select Dropdown Anchor to the Right of the Select Component

Currently, I am utilizing Ant Design in my project and encountering an issue with a Select component. The Select is positioned to the right side of the screen and contains labels that are quite long. This causes the dropdown to overflow and a scrollbar t ...

CSS: Placing items within an ng-for loop utilizing the "fixed" position property

<ul class="nav nav-pills nav-stacked" style="list-style: none"> <li *ngFor="#el of dragZoneElems; #idx = index"> <h4 style="position: fixed; top:'idx'*10" [dragResponder]="el">{{el.first}} {{el.last}}</h4& ...

Looking to test form submissions in React using Jest and Enzyme? Keep running into the error "Cannot read property 'preventDefault' of undefined"?

Currently, I am developing a test to validate whether the error Notification component is displayed when the login form is submitted without any data. describe('User signin', () => { it('should fail if no credentials are provided&apos ...

Every time the page is refreshed, ExpressJS and NodeJS are working together to duplicate the array

var express = require("express"); var router = express.Router(); const fs = require("fs"); const path = require("path"); const readline = require('readline'); const directoryPath = path.resolve(__dirname,"../log ...

The event listener for 'load' is not functioning properly within the React (Gatsby) environment

I'm having trouble with a sticky scroll parallax effect when the page initially loads at a position other than the top. I'm utilizing the react-scroll-parallax library (https://www.npmjs.com/package/react-scroll-parallax). To address this issue, ...

What could be causing my background image not to appear in the Next 13 application?

Having trouble with a background image not rendering in my global SCSS file. While all other styles are working fine, the bg image is causing some issues. I don't think it's the file paths, but the new app dir in Next 13 might be throwing me off. ...

Getting to the values within a JSON object that contains multiple arrays

Is there a way to retrieve array data from the json object data? const [data, setData] = useState([]) const getData = () => { axiosInstance .get(url + slug) .then(result => setData(result.data)) } useEffect(() = ...

Incorporating Masonry-inspired images into a website's design section

I have been working on incorporating a masonry layout of images into my website, but I am facing alignment issues. The images are simply stacking on top of each other instead of creating the desired masonry effect. Are there any tips on how to create a mas ...

How to retrieve an element using its id within the useStyles() function in material-ui

I am facing an issue with accessing nested divs by their ids in the useStyles() function. Below is the code snippet of my component: import { useStyles } from "./components/Styles/Styles"; export const CountDown = () => { const classes = useStyles( ...

Creating a React component that can dynamically render either an image or a video based on the incoming API data is a great way to

I'm currently immersing myself in learning and honing my skills in React. My goal is to develop a React web application that enables users to view and scroll through Nasa's picture of the day based on their selected date. The API I will be utili ...

Having trouble with CSS when using jQuery's gt() selector?

While working with jQuery, I encountered a warning from Firefox regarding the following code snippet: $("li.example div.code:gt(4)").hide(); Firefox indicates a CSS Error: Unknown pseudo-class or pseudo-element 'gt'. Although the code works as ...

Ways to split images and text in list items using CSS

Can the text be formatted in a specific location on the page, separate from the image and heading? This is the HTML code I am currently using: <div class="tab-pane container fade" id="environmental"> <div class="row"> <div class="c ...

Prevent Typography components in Material UI from breaking onto separate lines

I want to style my text with two different Typography tags, but I don't want a line break in between. This is what I currently have: <Typography variant="title" color="inherit" noWrap> Project: </Typography> <Typography variant="sub ...

What is the best way to center a rotated element and have it aligned to the top?

* { box-sizing: border-box; } body { font-family: 'Poppins', sans-serif; margin: 0px; } .we-adopt { background-color: #8a8484; color: #ffffff; padding: 88px 0px; height: 100px; } .we-adopt-list span { display: i ...