Monday, January 16, 2017

CSS Rollover Image Effect - Change Image on Hover

Rollover image is a design feature where an image changes when your mouse hovers over it. Think of a light bulb that turns on and off when you move your mouse cursor into that area of a page. When a page is loading, rollover images are preloaded into it to ensure that the rollover effect is displayed quickly.

This used to be implemented using JavaScript, which is fairly easy with just a small amount of script involved. To make rollover images functional, onmouseover and onmouseout attributes are used to a link tag. The code is then added to a blog gadget or into a new post. It proved to have a number of disadvantages, however, which is why many web developers are using a CSS-only method.

css rollover image

How to create a rollover image using CSS

Here is how to implement a rollover image using CSS. Before getting started, we need to have two images ready, one in its initial/static state as well as its rollover state.

The Image

Place both the static and rollover image in one file and make sure that the rollover image is placed on top of the static one. To achieve the rollover effect, we'll write a code to display the static image and crop the hover image, so that only one image state is displayed at a time.

For this tutorial, we'll use the following as a CSS rollover image.

html rollover image

Creating an HTML Anchor Element for our Image

Instead of adding the image file in a <img> tag, we'll display it as a background image of an </a> (anchor) tag. Here's the HTML that we need to add:
<a class="rolloverimage" href="#URL">Rollover Image</a>
Note: if you want to make the image clickable, replace #URL with the url of the webpage where you want the link to point to.

Using CSS to Set a Background Image

To create the mouseover image effect, we'll use the :hover CSS pseudo-class. Then, we'll use the background-position property and set the values to 0 0 to move the background image to the upper left corner which will create the rollover effect.
<style type="text/css">
.rolloverimage{
display: block;
width: 56px;
height: 90px;
background: url('https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVC8yFUh8R90EKsIoCgh9wwsBXxGJkOydpofKZhlnw4a86PcD3G53hyphenhyphenEHc22RV9bmMBFOz3oCEqDWk_gPwNTKkgBmVVQT82J4lPIM2w_uYSMs6xFHkHuAVAuFHxo49F088OOTNu0JJy5Yo/s180/rollover-image-light-bulb-on-off.png') bottom;
text-indent: -99999px;
}
.rolloverimage:hover{
background-position: 0 0;
}
</style>
Note: Replace the text in blue with the url of your image file. Please pay attention on the width and height values marked in red, these should be different depending on your file, where the height value is for only one image and not the entire image file!

The result

Hover your mouse cursor over the light bulb to see the rollover image effect in action:

Rollover Image

Adding Rollover Image to Blogger

To add the rollover image as a gadget: copy both the HTML/CSS codes and go to 'Layout', click on the 'Add a Gadget' link > select HTML/JavaScript, then paste the codes in the 'Content' box.

Or, if you want to add it inside one of your posts, when you create a New Post, switch to the 'HTML' tab and paste the codes inside the empty box.

And this is how you make images swap on mouseover using CSS. Enjoy!
Disqus Comments
Powered by Blogger.

Label

#NgeblogburitBlibli (2) 12 Pahlawan Nasional (1) AC Bau (50) AC Bocor (50) AC Error (50) AC Mati (50) AC Tidak Dingin (50) ac yang benar (1) Admin (1) Adsense blogspot (1) Adsense hosted (1) AdsOptimal (2) Android (17) aplikasi (1) Artis (5) asuransi kesehatan masyarakat (2) AuthorPost (1) bagaimana pake ac yang benar (1) Bahasa Indonesia (49) Bahasa Inggris (1) Bahaya daging kambing (1) Bank (4) Bank Indonesia (1) banner (36) bca (1) beginner's guide (4) Belajar membuat website A sampai Z (1) Beli rumah pakai BPJS (1) bencana (1) Berita (3) Bernyanyi (2) Biduan (3) Bisnis (223) Bisnis Kuliner (162) Bisnis Online (10) BisnisOnline (6) blogger (3) blogger pages (1) blogger posts (2) Blogger Templates (2) Blogging (43) BlogNews (6) Bongkar AC (50) BPJS Kesehatan (1) buat web (637) buat website (637) Bulu mata (1) Business (1) Cara Membuat Website (637) Cara Mendapatkan Kartu Perlindungan Sosial (KPS) (1) cara mendapatkan penghasilan melalui blogspot (1) cara sehat menggunakan ac (1) Cara Sukses (3) Cicil rumah pakai BPJS (1) Cita Citata (1) Cms (1) Corona Virus (11) Covid-19 (12) Cpanel (1) css (1) Cuci AC (50) Daftar BPJS (1) Digital Marketing (20) digital printing (36) Discount Bikin Web (11) Discount Pandemi (10) Discount Pandemi Covid-19 (10) Domain (3) DUKCAPIL (20) E-Commerce (7) Ebook (1) Eyelash (1) Facebook (4) Fokus berita (1) Free web hosting (1) Gadget (1) Game Android (4) go mobile (1) gojek indonesia (1) google adsense (2) Google My Business (2) google search consol (1) gopay (1) Hidrolik (1) Hidrolik Cuci Mobil (1) Hidrolik Mobil (1) Hobi (1) homepage (1) Hosting (1) Hosting gratis (1) how to (2) HUMOR (2) huruf timbul (1) Idol (5) Image Effects (1) Indonesia Terbitkan Uang Baru 2016 (1) Intermezzo (6) Internet (52) IPA (2) IPS (59) Isi Freon (50) jago merah (1) jakarta (1) JAMKESDA (1) Jasa (639) jasa bikin web (640) jasa bikin website (639) Jasa Bongkar Gedung (1) Jasa Bongkar Gedung Tua (1) Jasa Bongkar Rumah (1) Jasa Bongkar Rumah Tua (1) jasa buat web (639) jasa buat website (639) Jasa Pembuat Website (20) jasa pembuatan web (659) jasa pembuatan website (660) Jasa Service AC (51) Jasa Service AC Kontrak (1) Kandungan daging kambing. (1) Karaoke (5) Karir (2) Kartu Kredit (2) Kartu Member (1) kebakaran pasar senen jakarta (2) Kecantikan (1) Kependudukan (20) Kerajinan (3) Keuangan (2520) Kewirausahaan (1) Kimia (2) Kisah Inspiratif (2) Kisah Sukses (2) Komputer (1) Komputer Dan Jaringan (1) konten (1) Kontraktor (1) Kredit Mobil (1) Kredit Motor (1) Kursus PHP-MySQL Paling Murah dan Mudah Dimengerti (4) LAIN - LAIN (2) letter embossed (1) letter timbul (1) LIFE STYLE (2) Manfaat daging kambing (1) marquee (1) Media Sosial (2) Memilih template blog (1) menggunakan ac yang benar (1) Mozaik Islam (3) mp3 (2) mp3 player (2) News (2) ovo (1) pake ac (1) pake ac yang sehat (1) Pandemi Corona (1) Panduan membuat blog dari nol sampai selesai. (1) Pasang AC (50) Pegadaian (3) Peluang Bisnis (163) Peluang Usaha (163) pembuatan website (637) Pendidikan (2) Penyanyi (5) percetakan (36) Persyaratan dan Cara Membuat e-KTP (1) Pet (1) PHOTOSHOP (1) Phpmyadmin (1) Pinjaman (7) Pkn (19) PopojiCms (1) post (1) Promo (3) promo bikin web (1) promo pembuatan website (1) reklame (1) Renungan (3) Ruang Pers Bang Indonesia (1) Selebritish (6) SEO (5) SEO Onpage Adalah (1) Seo Tool (2) Service AC (51) Service AC Kontrak (1) Service Cuci AC (50) Social Media (2) spanduk (36) StarMaker (9) Starmaker Error (3) Starmaker pakai VPN (3) Starmaker tidak bisa dibuka (3) static pages (1) Superstar (5) teks berjalan (1) tema blog (1) Template (3) Tentang Kartu Perlindungan Sosial (KPS) (1) The dangers of using Braces For Dental Care (1) Tips (1) Tips Dan Trik (14) tips mengenai ac (1) tips sehat (1) tips sehat memakai ac (1) TipsNTrick (1) trending news (1) Tutorial (3) Tutorial Blogspot (19) Tutorial membuat halaman kontak blogspot (1) Tutorial Wordpress (6) Uang Baru (1) Uang Baru 2016 (1) Umum (4) Uncategorized (260) Usaha Kuliner (162) verifykasi ownership (1) Viewen (1) Web Developer (88) Web Programing (20) Welding Shop Citra Raya (2) Widget / Gadget (9) Widgets (1) YouTube (2)

DiDUKUNG