:focus‑visible vs :focus Demo - Online Accessible Outlines
See the difference between :focus and :focus‑visible on interactive elements. Learn which to use for better UX.
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"开关后,所有卡片都会正常渲染(蓝色边框),可对比性能差异。
See the difference between :focus and :focus‑visible on interactive elements. Learn which to use for better UX.
Stack multiple background images and blend them with colors. Create unique textures. Copy the CSS code instantly.
Nest elements in 3D space with preserve‑3d vs flat. Rotate the parent and see children behave differently.
Simulate forced‑colors mode and see how your site looks. Adjust CSS system colors. Make your design accessible.
Paste a website's HTML and see which text is only visible to screen readers (e.g., .sr‑only). Preview the accessible layer.
Enter a URL and a user‑agent to see if it is allowed or blocked by the robots.txt file. Quick bot validation.
Open a test video in Picture‑in‑Picture mode. Control entering and leaving PiP. Copy the code snippet for your own app.
Toggle between light and dark mode for a demo page. See how to use the media query. Copy the pattern.
Toggle scrollbar‑gutter: stable to reserve space for the scrollbar and avoid content jumps. Visual demo with two columns.
Simulate reduced motion preference and test your animations. Copy the media query snippet. Keep your users safe.
Define a set of colors for light and dark themes. Get the CSS custom properties snippet. Toggle live.
See how grid-auto-flow: row vs column changes item placement. Add and remove items to understand the algorithm. Visual.
Start from a base color and use `oklch(from color l c h)` to create lighter or darker variants. Copy the code.
Paste your full CSS and HTML; automatically identify and extract the styles needed for the visible part. Reduce render‑blocking resources.
See how a full paragraph looks with your chosen text and background colors. Not just a ratio; the real appearance.
Highlight elements with aria‑describedby and see the linked description text. Verify a11y annotations.
Apply the drop‑shadow() filter and compare it with box‑shadow. See how it follows the contour of transparent images. Copy code.
Test overscroll‑behavior: contain to prevent background scroll or pull‑to‑refresh. See the effect in a live demo.
Visually create the glassmorphism effect: background blur, transparency, and border. Copy the complete CSS. Modern UI design.
Check if your site is cross‑origin isolated by examining the COOP and COEP headers. See if SharedArrayBuffer is available.
Visually name grid areas in a table and generate grid‑template‑areas CSS. Perfect for complex layouts. Drag‑free.
Check how many pages a PDF has without uploading it (uses FileReader). Also shows file size and title if available. Simple and fast client-side tool.
Create vertically oriented text layouts with `writing‑mode`. See the effect and copy the complete CSS. For vertical languages.
Resize the container and see the difference between repeat(auto‑fill, …) and auto‑fit. Understand responsive grid behavior.
Browse the fonts installed on your system and type a custom phrase to see how it looks. Uses Local Font Access API where available.
Animate elements as they enter and exit the viewport using view() timeline. Parallax and reveal effects without JS.
Apply dyslexia‑friendly fonts, spacing, and background to any text. Preview and copy the formatted version. Improve readability.
Build a complete font‑stack for your website. Choose a primary font and get the best fallback options for different OS. Copy the CSS.
Beautify and format your CSS stylesheets instantly. Organize, minify, or prettify CSS code for better readability. Processed securely on client-side.
Encode a hidden message using zero‑width characters. The message looks like normal text. Decode with the same tool.