content‑visibility: auto Benchmark - Online Render Cost
Toggle content‑visibility: auto on long blocks and see the rendering time difference. Understand this powerful property.
UD5 Toolkit
正在生成内容卡片...
content-visibility: auto 是一个CSS属性,它告诉浏览器:如果元素不在用户视口附近,就跳过该元素的渲染(包括布局和绘制)。浏览器会自动检测元素是否接近视口,当元素即将进入视口时才会渲染它。这类似于"懒渲染"——屏幕外的元素占据空间(通过 contain-intrinsic-size 定义),但其内部内容不会立即被处理,从而显著减少初始页面渲染的工作量,提升性能。
核心技术:它结合了CSS Containment(尺寸 containment),使浏览器能够安全地跳过屏幕外元素的布局计算。
display: none:元素完全从文档流中移除,不占据任何空间,也无法通过搜索(Ctrl+F)找到。
visibility: hidden:元素隐藏但仍占据空间,其布局和绘制仍然进行,只是不可见。
content-visibility: auto:元素仍然占据空间(通过contain-intrinsic-size),滚动条位置保持准确,内容可被搜索和访问。浏览器只是延迟渲染屏幕外的元素。当用户滚动到附近时,浏览器自动渲染它们。这是三者中唯一能带来性能提升的方案。
contain-intrinsic-size 指定了元素在未渲染时的预估尺寸。当 content-visibility: auto 跳过屏幕外元素的渲染时,浏览器需要知道该元素大约占多大空间,以保持滚动条的正确性和页面布局稳定。如果没有设置这个属性,元素在未渲染时的高度可能为0,导致滚动条跳动。
推荐写法:contain-intrinsic-size: auto 300px; —— auto 关键字让浏览器记住元素最后一次渲染时的实际尺寸,而 300px 是首次渲染前的回退值。
性能提升取决于页面内容量和复杂度。根据Chrome团队的测试:
本工具可帮助你直观感受这一效果:开启content-visibility后,视口外的卡片处于"延迟"状态,浏览器不会消耗资源渲染它们。
桌面端:Chrome 85+、Edge 85+、Opera 71+ 完全支持。Firefox从版本125开始支持。Safari从版本18(2024年)开始支持。
移动端:Android Chrome 85+、iOS Safari 18+ 支持。对于不支持的浏览器,content-visibility: auto 会被忽略,元素正常渲染,不会造成功能问题——这是一个优雅降级的特性。
建议使用渐进增强策略:在不支持的浏览器中,页面仍可正常使用,只是缺少性能优化。
SEO:没有负面影响。搜索引擎爬虫通常会渲染整个页面(包括屏幕外内容),content-visibility不会阻止爬虫索引内容。Google明确表示他们能够正确处理使用content-visibility的页面。内容仍然存在于DOM中,可被完整抓取。
可访问性:屏幕阅读器可以正常访问所有内容,因为元素仍然存在于DOM中。Ctrl+F搜索功能也能正常找到屏幕外的文本内容。这与虚拟滚动(只保留视口内DOM节点)有本质区别。
注意:确保 contain-intrinsic-size 设置合理,避免屏幕阅读器的导航顺序出现混乱。
最适合以下场景:
不太适合:需要精确渲染计时的动画元素、需要始终可见的固定元素、视口内元素数量本来就很少的简单页面。
本工具使用Intersection Observer实时追踪每个卡片的可见性:
关闭"auto"开关后,所有卡片都会正常渲染(蓝色边框),可对比性能差异。
Toggle content‑visibility: auto on long blocks and see the rendering time difference. Understand this powerful property.
Render 1000 styled elements using inline styles vs. CSS classes and compare time. Understand CSS‑in‑JS trade‑offs.
Add many complex CSS styles and measure frames per second. Isolate expensive properties. Practical performance lab.
Create CSS mask‑image effects with custom shapes, gradients, and SVGs. Visually see the mask applied to an image. Copy the CSS.
Design a layout where nested grids share track sizes with their parent via subgrid. Copy the complete CSS.
Create a pure CSS scroll progress indicator using animation‑timeline: scroll(). No JavaScript. Copy the complete code.
Paste your CSS and see warnings for properties that have limited browser support. Links to CanIUse. Modernize safely.
Paste an SVG and get an optimized, URL‑encoded data URI for use in CSS backgrounds. Reduces file size and escapes characters.
Limit browser gestures on an element: pan‑x, pinch‑zoom, manipulation. Draw on a canvas to test. Mobile dev helper.
Create complex linear gradients with any number of color stops, exact angles, and length units. Live preview and CSS code.
Style underlines, overlines, and strike-throughs with colors, wavy styles, and thickness. Modern CSS text‑decoration.
Generate a random HTML/CSS card with different box model properties. Inspect and guess the size. For learners.
Design a button or card that glows on hover. Adjust shadow color, spread, and transition. Copy the CSS. Rich UI.
Create a realistic letterpress (debossed) text effect using CSS text‑shadow and background. Adjust depth and light direction. Copy code.
Generate a subtle noise/grain texture as a CSS background pattern. Adjust opacity and size. For that film look.
Create a customizable checkerboard or grid background using pure CSS gradients. Adjust cell size and colors. Copy the code.
Upload two images and apply CSS blend modes (multiply, screen, overlay, etc.). See the result and copy the filter CSS. Pure frontend.
Add print styles like removing backgrounds, adding page breaks, setting margins. See print preview instantly.
Enter a URL and get a one‑page report of titles, description, headings, image alts, and broken links. All from browser.
Create a pure CSS countdown timer with a flipping number effect. Adjust duration and style. No JavaScript needed for display.
Create a glowing or rotating border animation around an element. Copy the CSS keyframes. Pure CSS magic.
Create a custom HTML/CSS progress bar with percentage, colors, and animation. Copy the code. Modern UI element.
Design a realistic neon sign text with multiple layers of glow. Copy the CSS and HTML. Perfect for headers.
A replica of the famous Flexbox Froggy game: solve alignment puzzles by writing CSS. Progress saved locally. Fun frontend learning.
Fetch a website's CSS and extract :root custom properties (‑‑color) to reveal its design token palette. For learning and inspiration.
Recreate the target CSS linear gradient by adjusting stops and colors. A unique game for front‑end developers to master gradients.
Enter a URL and get a rough client-side performance simulation: request count, DOM size, and potential speed tips. No real Lighthouse.
Browse and search all Font Awesome 6 icons with preview, class name, and unicode. Copy the HTML snippet. Perfect for web developers.
Create text with a gradient fill using CSS background-clip. Configure colors and direction. Copy the code. Works in modern browsers.
A complete, searchable list of all 140+ named HTML/CSS colors with their hex codes and color previews. Click to copy code. Essential web reference.