Show/Hide HTML elements using CSS: maintaining in memory without releasing

I'm struggling to understand this concept. I have multiple Divs within an HTML page, each representing a different section with unique images referenced from CSS and controlled using JavaScript (

document.getElementById('DIV').style.display='none/block';
).

Let's consider 2 divs for this example: Div1 & Div2 as parent divs, with child divs within them (Div1a, Div2a).

Here's where it gets interesting - when I hide Div1 with display = 'none'; but keep the image background in place, the image still lingers in the browser memory even though Div1 is hidden.

The confusion deepens when I move the background-image to a child div (Div1a) within Div1. In some cases, hiding the parent Div1 does remove the child div's image from the browser memory, while in others, it doesn't. The inconsistency baffles me.

At this point, I am completely lost on how to tackle this issue. Your insights and time are highly appreciated.

Thank you for your patience and suggestions.

Code Example:

With this method:

<div id="Div1">
    content....
</div>

<div id="Div2" style="display: none">
    ...content
</div>

div#Div1{
    background-image: url(images/mybg.png);
    background-repeat: no-repeat;
    width: 480px;
    height: 360px;
}

document.getElementById("Div1").style.display='none';
document.getElementById("Div2").style.display='block';

The image remains in the resources tab after executing the above javascript code.

When trying this approach:

<div id="Div1">
    <div id="Div1a">
        content....
    </div>
</div>

<div id="Div2" style="display: none">
    content....
</div>

div#Div1a{
    background-image: url(images/mybg.png);
    background-repeat: no-repeat;
    width: 480px;
    height: 360px;
}

document.getElementById("Div1").style.display='none';
document.getElementById("Div2").style.display='block';

The image is successfully removed from the resources tab upon executing the javascript code, although this behavior isn't consistent.

Answer №1

Hiding an element with display: none does not release it from memory in any web browser. The DOM element remains present, taking up the same amount of memory, but is simply hidden from view and layout.

If you truly want to free up memory by removing an element, you must physically delete it from the DOM using something like parent.removeChild(child). Additionally, ensure there are no references to the DOM element in your JavaScript code that could prevent it from being garbage collected.


When assessing memory usage in a browser, the methods used may not accurately indicate whether an image has been released. Even if the memory has been freed internally for reuse, it may not be returned to the operating system immediately. Simply unloading an image may not result in a visible decrease in memory consumption by the browser. The specifics of what shows up in memory usage would vary based on the browser, operating system, and the tools employed for monitoring memory.

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

Struggling with arrays within arrays

Currently, I am in the process of working on a react project and faced with the task of accessing data within a deeply nested object. Here is an example snippet of the object: {name: "Products", attributes: {…}, children: Array(1), value: "", getElem ...

Craft a dynamic menu of categories using PHP

As a beginner in coding and PHP, I am working on creating an online shop for a school project. I am looking for a simple code that can select categories from my database and display them on the homepage. Additionally, when a user clicks on a category, I ...

Enhanced Bootstrap Carousel with White Background Transitions

Having trouble articulating my issue, so here's a video that should clarify: https://example.com/video I'm using a bootstrap carousel template for a full-width slider. Despite my efforts to remove the white-space transitions, they persist. Am I ...

No search results found when using Vue.js searchbar filter

Today I delved into the world of VUEjs for the first time, experimenting with extracting data from a URL in JSON format. Initially, this process went smoothly, but my curiosity led me to attempt adding a search feature. I followed online tutorials, which u ...

Leveraging the power of $lookup and $mergeObjects in aggregation

I'm looking to join a collection. Previously, I used only lookup to get separated fields that are joined, but now I need the results similar to MySQL join. I have tried using $lookup and $mergeObjects for this action, but they are not working well. H ...

Sharing package JSON file dependencies with child engines and addons in Ember.js

I am seeking information on how Ember Js can share the parent app's package.json file dependency (xyz:3.0.0) with child engines and addons without them needing to redeclare the dependencies in their own package.json files. This is to reduce the overal ...

Expanding and collapsing content with jQuery's toggle and show

In my ASP.NET MVC2 application, I am generating dynamic markup using C#. The data in the database determines how many rows are shown. Here, I have displayed two rows - one for viewing and the other for editing. By default, I want the 'view' row(s ...

Using PHP to handle JSON responses

I am currently working on a project that involves retrieving JSON responses using PHP. My goal is to obtain a JSON array without any HTML tags mixed in, but unfortunately, the output includes unwanted HTML tags. I need assistance in figuring out how to r ...

Delete specific rows by clicking a button in AngularJS

I have a table with checkboxes for each row and I am trying remove the selected rows when a button is clicked. The selected row indexes are stored in an array using ng-change, but I cannot figure out how to delete them all at once with a single button clic ...

Is there an issue with hover not working for MUI styled component when focused?

Having trouble getting both focus and hover effects to work on the same MUI styled component in React. Check out this code sandbox for a demonstration. When clicking on the Select element, the background and border colors do not change: https://i.sstatic. ...

Despite Nodejs's efforts, it was unable to successfully populate the user

I have been able to successfully reference and store other documents in my mongodb database as objectids for my application. However, I am facing an issue with the .populate method not working as expected. Below is the code snippet that I am using for po ...

Unit testing a React component by using the `renderToString` method

Context My React application is built using the React Starter Kit. In the server.js file, components are rendered using renderToStaticMarkup and then passed to the Html component, which includes it using dangerouslySetInnerHTML as shown here. I am facing ...

Node.js, Sails, and Express aid in examining all parameters with ease

Currently, I am checking my params in the controllers like this: create: function (req, res) { if(req.params.label && req.params.password){ // do stuff } } However, I wish to streamline this process. Is there a tool available that c ...

What is the process for extracting dates in JavaScript?

I need help extracting the proper date value from a long date string. Here is the initial date: Sun Aug 30 2020 00:00:00 GMT+0200 (Central European Summer Time) How can I parse this date to: 2020-08-30? Additionally, I have another scenario: Tue Aug 25 ...

Converting flat JSON data into interactive HTML elements

After successfully saving my chatbot's conversation history as a flat json file following a previous question, I am now trying to display the contents of the json in my index.html file. Unfortunately, when I attempted to use the code provided in my ht ...

How to align two columns in the center using Bootstrap 4

Help Needed with Centering Two Columns in Bootstrap I am facing an issue with centering two columns in Bootstrap. In Bootstrap 3, I used push and pull classes to achieve this, but in Bootstrap 4 it seems different and I can't seem to figure it out. ...

Utilizing globalProperties in Vue 3 Web Components: A Comprehensive Guide

I'm having trouble understanding how to implement globalProperties within a web component. Here's a snippet from my main.js file: import { defineCustomElement } from 'vue' import axios from 'axios' import VueAxios from ' ...

activating hover states is prevented when using overflow:scroll

Visit the codepen page I am attempting to set a max-height or height to a long list and make the rest scroll. I achieved this by using: height:800px; overflow:scroll; The issue I'm facing is that the "#subcategory" items stop displaying on hover ...

Flask Server produces a response with a considerable delay when accessed through AJAX

I am currently running 2 servers on localhost, each with different ports. One of them is a basic flask server in Python and its code is provided below: from flask import Flask,jsonify from flask_cors import CORS app = Flask(__name__) CORS(app) @app.rout ...

Is there a variance in DPI [font sizes] between QWebView and other QWidgets?

I have a QWebView that is showing HTML content styled with CSS: body { font-size: 10pt; } Within the same window, there is also a QTextEdit field where I adjusted the font like this: QFont newFont; newfont.setPointSize(10); myEditField->setFont(newFo ...