Abundance of DOM elements - the difference between hidden and display none

When I have numerous DOM elements on my page and assign them all a display: none property, the browser continues to perform quickly (smooth scrolling and snappy page response).

But if I hide the elements using visibility: hidden instead, the browser slows down as if they were all being rendered on the screen.

I would appreciate it if someone could provide a detailed explanation for this behavior. Thanks!

Answer №1

Essentially, even though invisible elements may not be physically drawn on the page, browsers still reserve space for them in order to properly lay out the visible elements.

To learn more about this concept, check out MDC visibility:hidden:

While invisible elements are not visually rendered, they do have an impact on layout. Descendants of these elements will appear if their visibility is set as visible (though this feature may not work in older versions of IE).

If you use display: none instead, browsers only need to consider and arrange the visible elements, disregarding the invisible ones completely.

The balance between visible and invisible elements, along with the total number of elements, can influence how much of a difference this makes.

Answer №2

Picture this:
You're working on a painting with a white background, meticulously adding intricate details to an apple over the course of an hour. Then, you cover it all up with another layer of white paint. That's what visibility is like.

display:none is akin to never starting the painting at all. It may be quicker to load initially.

However, using display:none has its drawbacks: when switching back to block (or inline, etc.), you have to start from scratch on the painting. With visibility, the browser simply removes that last coat of paint and everything is back as before. In this case, visibility is faster.

Just keep in mind that when you use visibility:hidden, the element maintains its position in the layout, so the surrounding elements remain unaffected.

If you want a more technical explanation, check out David Baron's talk.

Answer №3

Wow, this concept is fascinating. Could it be that using visibility: hidden is essentially the same as utilizing opacity: 0?

While I may not be a developer myself, I can't help but wonder if there could be significant performance improvements by having elements with hidden visibility simply rendered as transparent squares with matching dimensions instead of being fully painted. This might especially apply in cases where the element's dimensions are already known.

Answer №4

When using visibility:hidden, the elements are rendered on the screen but remain hidden from the user's view. On the other hand, with display:none, the elements are not rendered at all.

Answer №5

When using visibility: hidden, the elements' sizes must still be calculated in order to reserve the necessary space for them, even though they are not visible on the screen.

Answer №6

visibility: collapse: element hides itself, but it still occupies space in the layout.

opacity: 0: element will be included in render tree and painted, but will not be visible to users.

Answer №7

When using display: none, the elements are completely removed from the DOM, whereas with visibility: hidden, they remain present but are just invisible.

A key distinction is that form input fields with display: none will not be submitted with the form, while those with visibility: hidden will still be included. This has been my observation, though different browsers could have varying behaviors.

Answer №8

"The browsing experience feels sluggish, almost like each element is individually hand-drawn on the screen."

My theory for the slowness is that even though the tag is rendered, it's not actually visible on the screen.

For more information, take a look at this article.

Answer №9

When display:none is utilized, the browser avoids initializing those elements and rendering their content. On the other hand, with visibility:hidden, the elements are initialized but simply hidden from view.

Explore more about CSS display properties here

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

Adding space around images in JavaFX

Just a quick question. I'm wondering if there is a way to apply padding to an ImageView using CSS in JavaFX. I've attempted insets, padding, and other methods with no success. Alternatively, can the same effect be achieved with a Text Object? ...

Data displayed in a table-like format

Is there a way to achieve this layout view image here (currently done using tables) while maintaining semantic markup? I believe the best tag for this would be dl. Each cell should have the height of its corresponding row. EDIT: The left column contains d ...

Slight Misalignment of Elements

I am attempting to align two corners of an element so that they perfectly match the corners of another element. In simpler terms, I am aiming to synchronize the corners of one element with those of another element. Below is the code snippet: ... When y ...

Tips for changing the color of an underline in an <a> tag when it is hovered over

Is it possible to change the color of the underline in an <a> tag when hovering over it? After some investigation, it seems that direct color changes are not feasible. Instead, you can use the border-bottom option. However, my attempt did not yield ...

Is there a way to deactivate keyboard input on an HTML number input field? How about in a React or Material-UI environment?

I am working with an <input> tag that has the attribute type="number", and I want to disable keyboard input so that users are required to adjust the value using the spinner (up and down arrows). This will allow me to consume the input value on each c ...

Techniques for ensuring input validity using JavaScript

One of my form inputs is required: <input type="text" id="input-id" required> After the user submits it, I send the value using ajax and then clear it with $("#input-id").val(""). However, after this action, the input appears invalid. I want to ...

Stop unwanted overrides of CSS3 blinking background colors

I am facing an issue with a programmatically created table that has n rows. To distinguish between odd and even rows, I programatically add classes to them. Some of these rows have "special" content that needs to be highlighted. Currently, I am accomplishi ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

What is the best way to showcase a div on top of all other elements in an HTML page?

As a newcomer to html and css, I have successfully created a div that contains city names. The issue I am currently facing is when I click on a button to display the div, it gets hidden behind the next section of my page. Take a look at the image below for ...

Developing a transparent "cutout" within a colored container using CSS in React Native (Layout design for a QR code scanner)

I'm currently utilizing react-native-camera for QR scanning, which is functioning properly. However, I want to implement a white screen with opacity above the camera, with a blank square in the middle to indicate where the user should scan the QR code ...

Discover the method for inserting a title attribute value into a span element

Utilizing AngularJS to retrieve and display data within a span element. I am now aiming to utilize this value as the title for another span element. The current code being used is shown below: <span style="display: inline-block; width: 160px">{{acti ...

Obtaining JSON data using Jquery results in the output of "undefined"

I am struggling to extract the correct values from a JSON document in order to display schedules in list view. Currently, when accessing the JSON document, I keep receiving "undefined" as the result. Below is an example of the JSON document: { "Ferries": ...

Ways to display an icon with an underline effect upon hovering over text

I have implemented a CSS effect where hovering over text creates an underline that expands and contracts in size. However, I now want to display an icon alongside the text that appears and disappears along with the underline effect. When I try using displa ...

Automatically adjust sizes with div elements

How can I automatically resize this iframe within a div? My current method is not effective. <iframe src="http://www.gamepuma.com/external.html?http://data.gamepuma.com/games/06/crash-bandicoot.swf" width="600" height="400" frameborder="0" margin ...

Is there a way to create more space between the cards in my project?

Could someone help me with separating the cards in my HTML project? I'm new to coding and struggling with this. The cards are too close together for my liking, is there a way to adjust the spacing? Below is the code I currently have: * { box-sizin ...

The Datatable fails to render the JSON data

Currently, I am dynamically binding a DataTable from a JSON URL and also generating headers dynamically. However, I am facing some issues in passing the JSON data to aaData in the DataTable. Can you please take a look at my code snippet below and provide m ...

The UI does not display the html code (specifically svg) added through jquery appending

I am working with the following HTML code: <svg> <g> <rect></rect> <text> abc </text> </g> <g> <rect></rect> <text> def </text> </g& ...

Using DataTable with backend processing

Has anyone successfully implemented pagination in DataTable to dynamically generate the lengthMenu (Showing 1 to 10 of 57 entries) and only load data when the next page is clicked? I'm currently facing this challenge and would appreciate some guidance ...

Spotting the Visible Element; Detecting the Element in

Currently, my webpage has a fixed header and I am facing the challenge of dynamically changing the styling of the li elements within the navigation based on the user's scrolling position. To achieve this, I believe the most effective approach would b ...

Having issues with the sidebar malfunctioning and unsure of the cause

<html> <head> <title></title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> I've been working on creating a sidebar for my website, but it's not functioning as expect ...