Miscellaneous

Easiest Way to Create a CSS Rollover for an Image

Easiest Way to Create a CSS Rollover for an Image

css rollover imageThe Easiest Way to Create a CSS Rule for Image Rollover

When wanting to be able to hover over an image and have it replaced by another image, CSS allows you to accomplish this without the complex ‘mousein’ javascript techniques. This I have found to be the most efficient way of creating an image rollover that doesn’t interfere or even need javascript.

First you want to make your CSS rule: (I use id rather than class or span in this instance)

#chads-image-swap-1 {

width: (width of original image); height: (height of original image); background: url (images/yourimagenamehere.jpg);

}

#chads-image-swap-1 img:hover {

opacity: 0;

}

*It is important to make sure the image background you use above is the exact same size and height of the image you’re wanting to swap out once you or someone hovers over the image, otherwise it won’t look correct and you’ll be able to tell immediately.

Now for the HTML:

<div id=”chads-image-swap-1″><img id=”swap-1″ src=”images/yourimagenamehere.jpg” alt=”ideaforgestudios” /></div>

 

That’s it, really simple. Just don’t forget to link your images to the correct folder, as one or both will not show up and you’ll recognize this immediately. CSS made easy.

An example is here below:

ideaforgestudios
Our Strongest Offering

Forge Your Next Website

Forged Sites are custom-built, static-first websites with a full AI content engine on board — no CMS to log into, no plugins to break, no builder to fight.

  • Near-perfect PageSpeed scores, static-first architecture
  • ADA + WCAG 2.2 AA accessibility, built in and re-checked on every deploy
  • MOG, an AI Site Director, lives inside your site and deploys changes in minutes
  • DraftDash auto-drafted blogs keep your content engine running
  • Ethel AI-powered forms filter spam and capture genuine leads
Explore Forged Sites Plans from $79.99/mo