Home > Web Front-end > CSS Tutorial > Can CSS Be Used to Style Mouseover Effects on Image Maps?

Can CSS Be Used to Style Mouseover Effects on Image Maps?

Susan Sarandon
Release: 2024-11-08 04:04:02
Original
921 people have browsed it

Can CSS Be Used to Style Mouseover Effects on Image Maps?

Can CSS Be Used to Style Mouseover Effects on Image Maps?

Creating a webpage with an image that includes links can be achieved using an image map. However, adding a touch of interactivity by styling the area shapes on mouseover can enhance the user experience. Is this possible using CSS?

Let's consider the following code:

<img src="{main_photo}" alt="locations map" usemap="#location-map" />
<map name="location-map">
    <area shape="rect" coords="208,230,290,245" href="{site_url}locations/grand_bay_al" />
    <area shape="rect" coords="307,214,364,226" href="{site_url}locations/mobile_al" />
    <area shape="rect" coords="317,276,375,290" href="{site_url}locations/loxley_al" />
</map>
Copy after login
area { border: 1px solid #d5d5d5; }
Copy after login

Despite attempts, this code fails to style the area shapes on mouseover.

CSS-Only Solution:

Instead of using the image map approach, an alternative method using CSS's :hover pseudo-class is available. Here's an example:

<div class="area"></div>
<div class="area"></div>
<img src="image.jpg" />
Copy after login
.area {
    background: #fff;
    display: block;
    height: [desired height];
    opacity: 0;
    position: absolute;
    width: [desired width];
}
.area:hover {
    opacity: 0.2;
}
Copy after login

Explanation:

  • The image is placed at the bottom layer.
  • Two div elements are positioned absolutely on top of the image with an opacity of 0.
  • When the mouse hovers over these div elements, the opacity increases to 0.2, revealing the content behind them.

This method provides a simple and elegant way to add mouseover effects to specific areas of an image using CSS.

The above is the detailed content of Can CSS Be Used to Style Mouseover Effects on Image Maps?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template