Understand Virtual Rendering

Why Do We Need Virtual Rendering?

Virtual rendering is essential when displaying a large volume of information on a single web page, especially for long list-like datasets. Here are some reasons to implement virtual rendering:

Key Features for Virtual Render

A Dead Simple Implementation

There are various ways to achieve virtual rendering. Below is a minimal example that demonstrates the concept in under 100 lines of code. For production, consider using libraries like React-Window.

Minimal Virtual List - Codesandbox

A minimal implementation in React to show how virtual list works.

CSS Content-Visibility

As of September 2024, the content-visibility property is still a working draft with limited support across major browsers. However, for applications targeting modern browsers (e.g., Electron-based desktop apps), it can be used in production.

You can check the latest compatibility here:

CSS content-visibility | Can I use... Support tables for HTML5, CSS3, etc

CSS content-visibility | Can I use... Support tables for HTML5, CSS3, etc

Also Here’s a simple example that achieves a virtual-list-like feature using content-visibility: auto:

Content-Visibility Based Virtual List - Codesandbox

A minimal virtual render example using CSS Content-Visibility property.

Compare to the javascript implementation, this approach enhances accessibility, allowing content to be indexed by the browser's built-in search.

However, based on my testing, using content-visibility: auto with many items containing simple children can lead to worse performance. This may occur because content-visibility introduces additional overhead as it tracks which elements are currently in view, potentially resulting in increased computational demands.

Thus, the best scenario for using content-visibility might be to skip rendering complex DOMs with a manageable number of items (e.g., paragraphs in an article). For datasets with a large number of list items, traditional virtual list implementations or pagination should be considered, at least for now.

Conclusion

The new CSS content-visibility attribute is not a silver bullet for virtual rendering challenges; it has a narrowed use case. To effectively render large datasets on a web page, you should still use scroll-based virtual lists or consider implementing a time slicing strategy or pagination to enhance usability.