如何仅用CSS实现鼠标悬停.tags元素时旁侧显示指定图片?
Got it, since you're stuck using only user stylesheets, we can make this work with pure CSS—no JavaScript or HTML edits needed. Here's a straightforward approach tailored to your needs:
Core Concept
We’ll use the ::after pseudo-element attached to your .tags cells to embed the legend image. By default, this element stays hidden; when someone hovers over a .tags cell, we’ll toggle it visible and position it perfectly aligned next to the target cell.
Full CSS Code
/* Anchor the pseudo-element to the .tags cell */ .tags { position: relative; } /* Create the hidden image container */ .tags::after { content: ''; /* Keep hidden by default */ display: none; /* Position relative to the parent .tags cell */ position: absolute; /* Align to the top-right edge of the cell (adjust values as needed) */ top: 0; left: calc(100% + 8px); /* Add a small gap between cell and image */ /* Match your image's native dimensions (tweak if your image is different) */ width: 320px; height: 240px; /* Load and scale the image properly */ background-image: url('https://mal-stats.glitch.me/tagslegend.png'); background-size: contain; background-repeat: no-repeat; /* Ensure the image sits above other page content */ z-index: 10; } /* Show the image when hovering over the .tags cell */ .tags:hover::after { display: block; }
Quick Adjustments for Perfect Alignment
- Vertical Centering: If you want the image to align vertically with the cell, replace
top: 0;withtop: 50%; transform: translateY(-50%);. - Gap Size: Modify the
8pxvalue inleft: calc(100% + 8px);to make the space between the cell and image larger or smaller. - Image Scaling: Use
width: auto; height: 100%;instead of fixed dimensions if you want the image to scale with the cell’s height.
This works entirely within user stylesheets, so you can drop it into your browser’s custom CSS tool (like Stylus, Stylish, or Firefox’s userContent.css) and it’ll behave exactly as you need.
内容的提问来源于stack exchange,提问作者user3310334
相关产品推荐
相关产品推荐

