6 ms·
Two examples of hover styles on images
- Spivak 2y agoAren't you describing https://developer.mozilla.org/en-US/docs/Web/CSS/outline https://developer.mozilla.org/en-US/docs/Web/CSS/outline? > Outline is a line outside of the element's border. Unlike other areas of the box, outlines don't take up space, so they don't affect the layout of the document in any way. If you s/border/outline in the dev console on the image you get what you want.
- benatkin 2y agoOutline is used for :focus and :focus-visible, so box-shadow is nice to have for hover so you don't have to repurpose the outline for it.
- the_other 2y agoit seems to me that :hover and :focus are doing the same job in this case - indicating the target of the next action. I don’t see this as “repurposing”.
- Maken 2y agoIs there any directive against defining an outline for :hover?
- benatkin 2y agoNo, but when tabbing it's often more usable to have the outline look the same across different types of elements, and having a separate focus style from hover style makes it easier.
- nomdep 2y agoThat’s why do you use `border-color: transparent` instead
- KTibow 2y agoThat can make the image take up excessive space, or as the article says: > ..., or an always-on transparent border that only changes colour when you hover – but those can interfere with other CSS rules
- oneeyedpigeon 2y agooutline would seem to be the most appropriate solution here, unless I'm missing something.
- nomdep 2y agoYes, you are right
- markdown 2y agoAlways nice to see CSS on here, but we figured this out 15yrs ago. Are the kids rediscovering hover styles?
- wackget 2y agoYou haven't noticed? Literally everything in web development has been in a constant cycle. Everything is reinvented and made significantly worse. Next week: How to run your Docker React Node Babel Webpack Svelte grocery list application at the EDGE using Kubernetes on AWS serverless EC2 instances with Cloudflare workers. EDIT: I forgot we're gonna use Bootstrap too.
- arcanemachiner 2y agoBootstrap? So passé. Just sprinkle in a dash of Tailwind: <button class="bg-blue-600 px-4 py-3 text-center text-sm font-semibold inline-block text-white cursor-pointer uppercase transition duration-200 ease-in-out rounded-md hover:bg-blue-600 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 focus-visible:ring-offset-2 active:scale-95">Hello world!</button>
- mock-possum 2y agoOh my god it’s so modern and innovative, can you imagine living in the past when you couldn’t simply define an element’s entire style block right there in the tag itself??
- KTibow 2y agoThe social icons are neat, but using filter would be shorter than manually setting normal and hover styles, at the cost of having inconsistent gray colors: .icon:not(:hover) { filter: grayscale(1); }
- wackget 2y agoWhy go for the simple method when you can instead simply hand-edit SVG code then add two extra HTML elements per icon?
- lolinder 2y ago> at the cost of having inconsistent gray colors Which, to be fair, is a pretty high cost. The consistency of the greys that only differentiate on hover is half the aesthetic value of the effect.
- hnbad 2y agoAlso this would not work for GitHub, for example, where the hover color is black, which is already "grayscale".
- afloatboat 2y agoSVG elements also support currentColor for the fill property, which is linked to CSS’s color property. <svg><path d=“” fill=“currentColor” /></svg> element { color: hsl(0 0% 0%) } One advantage is that you can target the parent element and the SVG will change automatically. You can also create multiple “shades” by having currentColor on multiple paths with different opacity.
- wruza 2y agoA good hover style reminds me of how fast and responsive our computers can be, if we let them. For example, I add a thicker underline when you hover over a link on this site, and it appears/disappears almost instantly as I move my cursor around. It feels snappy; it makes me smile. Sigh. We managed to raise a generation for which an instant interaction on a 1000+MHz multi-core gpu-accelerated 1000+MB/s io system feels snappy and that’s special. Good job, goob job. you could add a negative margin to offset the border, or an always-on transparent border that only changes colour when you hover – but those can interfere with other CSS rules The worst part of CSS is its globalness. You can’t have two paddings sourced from different subsystems, can’t name a border so you could address it specifically (e.g. as in el:border(myhover) {background: <gradient>}). CSS is so poor and antipattern and nobody bats an eye. You write an article like this and already see all these fragile hacks advices coming in, normalized by years of stockholming through even most basic layouts.
- wackget 2y agoWe managed to raise a generation who think it's acceptable for JavaScript to be required in order to render a single page. For some "web developers", this is literally how they create a page: import React from 'react'; function App() { return ( <div> <h1>Hello world!</h1> <p>This is my HTML page.</p> </div> ); } export default App;
- Spivak 2y agoI can't really knock it when no one is ever going to ask them to do anything else. HTML might as well be assembly. It's starting to feel pretty vestigial at this point, it's all just different colored divs and aria labels. Elements on the screen as a human would perceive them stopped mapping to single elements ages ago. For some "systems developers" this is literally how they create a program: #include <stdio.h> int main(int argc, char *argv[]) { println("Hello world."); } GTK/Unity/WinForms, everyone has their working level of abstraction.
- 2y ago
- 6510 2y agolike so img{border: 10px solid transparent;transition:border 0.3s} a:hover img{border: 10px solid red;}
- Rugu16 2y agoI really miss hover effects on mobile, wish there was a way for this !
- Cthulhu_ 2y agoThere were / are / will be devices that support detecting hovering a finger or stylus away from the screen, so engineering-wise it's possible; it's just not included in the UX guidelines for the respective platforms.
- bauerd 2y agoIt could also be implemented with eye tracking.
- rererereferred 2y agoYou can use :active, which should work when you press the button. You can still move your finger and release outside the button to avoid the interaction. The only problem is the finger covering the effect.
- wruza 2y agoShort-press and scroll a little, this usually starts :hover mode. You’ve probably seen it accidentally triggering previews on various video platforms. It works in TFA.
- zooi 2y agoUse outline instead: outline: solid 10px red; Nearly identical to border, but without affecting layout at all.
- Timwi 2y agoIt's already been mentioned that instead of box-shadow perhaps outline is a better choice for the image frame. For the icons, I don't understand why the author believes that they need to have two copies of the icon inside the SVG. You can style every property of every element using CSS. If your icon is a single path element, it should remain a path element and be styled something like this: svg.social path { fill: gray; } svg.social:hover path { fill: #8df; } Of course it's possible that I'm missing something, but if all you want is changing color on hover, duplicating the item and using a mask is overkill.
- Izkata 2y agoI think that only works when the svg is embedded in the html, not when it's pulled in from a separate file through something like an <img> tag.
- chrisandchris 2y agoYep, does only work with embedded SVG and not for foreign resources.
- Timwi 2y agoAlthough that's a good call as I hadn't considered that, I still don't see why you want to use a mask. Surely you just need two tiny SVG files and show only one or the other. And since they're tiny, I would absolutely embed them directly, which is why I didn't consider the case of an img tag.
- JimDabell 2y agoToggling between greyscale and colour is nice, but often you want the colour to be visible by default, and greying something out when you hover over it incorrectly gives the impression something is disabled. It’s the opposite you want. In these circumstances, when there’s a collection of items, I find that it’s a nice effect to show the collection in colour to begin with, and when you hover over an item, the other items get slightly greyed out.