Skip to main content

Two examples of hover styles on images

I enjoy adding :hover styles to my websites. A 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.

I want to show you a pair of hover states I’ve been trying for images.

Adding an image border on hover

If I’m showing a small preview of an image that’s a clickable link to the full-sized version, I like to add a coloured border when you hover over it. It’s a visual clue that something will happen when you click, and “see the big version of the image” is a pretty normal thing to happen.

Initially I implemented this by adding a border property on hover, for example:

a:hover img {
  border: 10px solid red;  /* don't do this */
}

But a border takes up room on the page, which causes everything to get rearranged around it. Everything moves to make space for the border that just appeared, which is precisely what I don’t want. I want a subtle hover, not a disruptive one!

There are ways you can prevent this movement, but I couldn’t get them to work in a way I found satisfactory. For example, 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. It became a game of whack-a-mole to make all my margins work in a consistent way.

The better approach I’ve found is to add a box-shadow with no blur – this looks like a border, but it’s purely visual and doesn’t take up any space on the page. This is the rule I use:

a:hover img {
  /* Three length values and a colour */
  /* <offset-x> | <offset-y> | <blur-radius> | <colour> */
  box-shadow: 0 0 0 10px red;
}

Here’s a demo of both approaches. Notice how the rest of the page moves around when you add a border, but not when you add a box-shadow:

border

box-shadow

(If you’re on a device that doesn’t support hovering, you can toggle the hover styles manually.)

Changing the colour of icons on hover

A while ago I added social media links to the footer of this website. I displayed them as subtle, monochrome icons, to avoid overwhelming the footer with an explosion of different brand colours. I thought it would be fun to show the site’s brand colour when you hovered over the icon. If, say, you hover over the bird icon and see Twitter’s shade of blue, it’s a subtle confirmation that this is indeed a link to my Twitter profile.

Here are all the icons I had in this system:

(If you’re on a device that doesn’t support hovering, you can toggle the hover styles manually.)

The brand icons come from the websites themselves; the generic icons are from the Noun Project. When I downloaded the icon, I typically got an SVG file with one or more path elements that defined the icon’s shape.

For example, this is what I got in the SVG file for the email icon:

<?xml version="1.0" encoding="UTF-8"?>
<svg width="1200pt" height="1200pt" version="1.1" viewBox="0 0 1200 1200" xmlns="http://www.w3.org/2000/svg">
 <path d="m323.46 411.07 …"/>
</svg>

To turn this into my footer icon, I wrapped the path in a slightly more complex SVG:

<svg width="30px" height="30px" version="1.1" viewBox="0 0 950 950" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>

    <!-- Wrap the original envelope shape in a named group -->
    <g id="envelopeIcon">
      <svg x="-125" y="-125">
        <path d="m323.46 411.07 …"/>
      </svg>
    </g>

    <!-- Define a mask that blocks out the envelope shape -->
    <mask id="envelope">
      <rect x="0" y="0" width="950" height="950" fill="white"/>
      <use xlink:href="#envelopeIcon" fill="black"/>
    </mask>
  </defs>

  <!-- Define two shapes that actually get drawn -->
  <circle cx="475" cy="475" r="450" class="background"/>
  <circle cx="475" cy="475" r="475" class="foreground" mask="url(#envelope)"/>
</svg>

First I define a shape envelopeIcon which uses that original path, and defines the shape of the element as a reusable group. Then I define an SVG mask envelope that blocks out the envelope shape. Finally, I define two shapes that actually get drawn: a foreground and a background.

Here’s a 3D view of the two shapes, so you can see more clearly how they form a set of layers:

foreground background

Now I have two elements that I can style independently. For example:

.email .foreground { fill: gray; }
.email .background { fill: none; }

a:hover .email .foreground { fill: blue;  }
a:hover .email .background { fill: white; }

This technique can be extended to more complex icons – split it into multiple elements, and style each one independently.

It took me a while to get these icons working as I wanted, and I remember trying some quite fiddly hacks. As I was writing this post, I was pleasantly surprised to discover that the hover styles aren’t as complicated as I thought. I just had to figure out the general approach.

One thing I’m proud of is how readable this site is without CSS. I made these icons look good on a sans-CSS page by adding inline fill attributes to the background/foreground elements (e.g. <circle fill="white" …>). These inline styles get applied even if CSS is broken or disabled. I lose the interactivity, but the icon is still legible.

By the time you read this, those icons will have vanished from the footer. They were fun for me to work on, but almost nobody used them and they added almost 8KB of HTML to every page. That might not seem like much, until I tell you the average page size was a slender 21.1KB – nearly 40% of my HTML was spent on social media links nobody was clicking!