Adjust the background color and transparency of a specific section using HTML

Is there a way to modify the background color or opacity of a specific area within an image?

Take a look at my HTML, JavaScript, and CSS:

function changeColor()
{
    document.getElementById('testid').setAttribute("class", "style1");
}
.style1{
background: red
}
<img src="http://mridulahuja.com/uploads/1/3/8/6/13860206/_____2659642_orig.jpg" alt="" usemap="#Map" />
<map name="Map" id="Map" href="www.mridulahuja.com">
    <area alt="" title="" href="#" shape="poly" id="testid" onlcick="changeColor()" coords="117,36,162,20,193,34,189,55,108,55" />

 <area alt="" title="" href="#" shape="poly" coords="33,93,33,119,32,143,32,169,32,186,31,191,94,194,123,194,149,193,164,193,158,119,186,119,216,118,227,115,224,82,103,83,31,82" />
    
</map>

I experimented with using opacity but nothing seemed to have any effect. Any suggestions on how to achieve this?

Check it out on jsfiddle

Answer №1

Image maps do not allow for the CSS properties you desire. Consider using a CSS map instead, where absolutely positioned divs or anchors are placed over a positioned img. There are various online tools available to assist with this process, such as this one.

An example of how your code should be structured:

<div style="position:relative; width..., height... background:url(..your image...)">

    <a class="area" style="display:block; top:..., left:..., height:..., width:..."></a>
    <a class="area" style="display:block; top:..., left:..., height:..., width:..."></a>
    <a class="area" style="display:block; top:..., left:..., height:..., width:..."></a>

</div>

Add the following CSS:

a.area:hover{background:red;}

CSS maps are effective for highlighting areas with borders or background effects, and can even incorporate CSS3 features like glow effects. They are particularly useful in situations where traditional image maps are not supported, such as Facebook FBML tabs.

Answer №2

Incorporating a SVG clip-path generated from clip-path-generator onto an image along with a blurred duplicate devoid of the clip-path, both contained within a larger container. To further emphasize the effect, a translucent color layer has been added. Check out the demonstration on - jsfiddle -

Note: You can easily transfer images into the canvas by dragging and dropping in this clip-path generator. Additionally, there are other similar clip-path generators available online like this one.

Moreover, you can also create SVG shapes using graphic design software such as Inkscape or Adobe Illustrator.

body {
  margin: 0;
  background: honeydew;
}

.container {
  position: relative;
  vertical-align: bottom;
  display: inline-block;
  border: 4px solid indigo;
  box-sizing: border-box;
  overflow: hidden;
}

.color {
  position: absolute;
  top: 0;
  left: 0;
  width:100%;
  height:100%;
  background-color: indigo;
  opacity: 0.2;
}

.back {
  -webkit-filter: blur(2px); /* Chrome, Safari, Opera */
  filter: blur(2px);  
}

.clipped {
  position: absolute;
  top:0;
  left:0;
}

#clip1 {
/*Chrome,Safari*/
-webkit-clip-path: polygon(25px 250px,70px 250px,70px 285px,260px 285px,260px 250px,160px 250px,160px 115px,225px 115px,225px 90px,160px 90px,160px 70px,25px 70px);

/*Firefox*/
clip-path: url("#clipPolygon");

/* iOS support inline encoded svg file*/
-webkit-mask: url(data:image/svg+xml;charset=utf-8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy...
}
<div class=container>
<img class=back src="http://i.imgur.com/rjFPR2d.jpg" alt=img>
  <div class=color></div>
<img id=clip1 class=clipped src="http://i.imgur.com/rjFPR2d.jpg" alt=img>

</div>
  
<svg width="0" height="0">
  <clipPath id="clipPolygon">
    <polygon points="25 250,70 250,70 285,260 285,260 250,160 250,160 115,225 115,225 90,160 90,160 70,25 70">
    </polygon>
  </clipPath>
</svg>

An alternative rendition featuring a solitary image, no filters or clip-paths, instead employing a solid SVG shape crafted using Adobe Illustrator:

.container {
  position: relative;  
}

svg {
  position: absolute;
  top: 0;
  left: 0;
}
polygon {
  cursor: pointer;
  opacity: 0.6;
  fill: green;
}

polygon:hover {
  opacity: 0.8;
  fill: red;
}
<div class=container>
  
<img class=back src="http://i.imgur.com/rjFPR2d.jpg" alt=img>
  
<svg x="0px" y="0px" width="317px" height="327px" viewBox="0 0 317 327">
<polygon fill="#FF0000" points="24,252 24,70 164,70 164,91 223,91 223,119 164,119 164,252 "/>
</svg>
  
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that pressing the ENTER key on a form will only activate a specific submit button, even when there are multiple buttons present

I have a form containing two submit buttons - name="submit_button" and name="search_me_logo". Depending on which button is pressed, the form should perform different actions when it reaches the action location. Currently, when the enter key is pressed, th ...

If you refer to a function, are you personally calling the function or asking the reference to call it?

From what I understand, and please correct me if I'm mistaken, when a variable is assigned to a function in the form of a function expression, it doesn't hold the function in the same way that it holds a primitive value. The variable simply refer ...

Update DataTable 1.9 while preserving existing rows

I'm currently using dataTables js version 1.9 Periodically, an ajax call is made to the server to retrieve information that should be displayed in a table every 60 seconds or so. Although I can easily clear and repopulate the table like this: $(id) ...

JSF CommandLink malfunctions on Firefox when reRendering an entire form

I am currently working on a JSF 1.2 application (Sun RI, Facelets, Richfaces) that was previously designed only for IE6 browsers. However, we now need to extend our support to include Firefox as well. On one of the pages, there is a form with a button tha ...

Ways to Retrieve the Value of an Object

Here is some sample data: { "id": 1, "title": "Title one", "category_data": { "2": "Team", "7": "Queries" } }, A function is used to remove duplicate categories from the data: remove_category_duplicates: function () ...

Instructions for resetting a DataTable in JQuery that was originally built using an append operation

Encountered a problem with restarting my datatable after populating it with data from a web service. Everything fills up correctly, but upon clicking the Search Button again, the old table header remains and the function fails to include the new results in ...

Guide to transmitting and managing a JSON document utilizing JavaScript

When working on the server side, I receive a simple JSON file via REST that contains various IDs. Here is an example: [ { "_id": "5825a49dasdasdasd8417c1b6d5", } "_id": "dfsdfsdf4960932218417c1b6d5", } "_id": "23434344960932218417c1b6d5", },] To handle t ...

Gulp does not work well with Android APK compilation

Greetings, I am facing an issue while trying to compile my Android app using gulp with the command: gulp --prod -p android. The problem arises when comparing the file size of the generated APK between myself and a colleague. When my colleague compiles, the ...

the php renderer fails to take into account the styles

Trying to convey this as clearly as possible. I have the variables below: $name = main channel and $childs = sub channels. When I remove the childs from the HTML, only the main channel shows up - which is expected. However, when attempting to style the su ...

Injecting Ajax-loaded content into an HTML modal

Hey there! I'm currently working on a project involving the IMDb API. The idea is that when you click on a film title, a popup should appear with some details about the movie. I've made some progress, but I'm stuck on how to transfer the mov ...

Modifying the content of the front-end HTML file according to the data retrieved from express-session

I've been working on creating a login page for my web application using express-session. So far, I've successfully implemented the functionality to validate login credentials. After confirming a valid login, I execute the following code within t ...

update the markers on a leafletjs map

I am aiming to create a dynamic map with markers that update every 10 minutes. The marker positions are stored in a spreadsheet document that is regularly updated. After successfully retrieving the data from the spreadsheet and positioning the markers in ...

Error encountered in Storybook: The value is not iterable (property Symbol(Symbol.iterator) cannot be read)

I recently created a React library using and opted for the React + TypeScript + Storybook template. You can find the entire codebase here → https://github.com/deadcoder0904/react-typical I encountered the following error: undefined is not iterable ( ...

Querying GraphQL: Retrieving partial string matches

I have set up a connection to a mongoDB collection using graphQL. Here is the data from the DB: { "_id" : ObjectId("59ee1be762494b1df1dfe30c"), "itemId" : 1, "item" : "texture", "__v" : 0 } { "_id" : ObjectId("59ee1bee62494b1df1dfe30d" ...

Enlarge an image when hovering over it

I am currently in the process of creating a website and I have encountered an issue. When I hover over an image, I want it to zoom in, but unfortunately it does not zoom within the designated div area. How can I fix this problem? <div class="container" ...

Embark on a journey through a preorder traversal of a Binary Tree using TypeScript

Hello! I've been tasked with creating a function that iterates over a binary tree and returns all its values in pre-order. Here is the code snippet: interface BinTree { root: number; left?: BinTree; right?: BinTree; }; const TreePreArray ...

Is the input disabled when clicked on?

To ensure the end-to-end functionality of my application, I have implemented a scenario where upon clicking a spinner button, both Username and Password input fields are disabled before being directed to a new page. My testing methodology involves verif ...

Do elements containing {visibility:hidden} properties exist within the HTML DOM structure?

Do HTML elements that have the css property visibility:hidden still exist within the DOM tree? ...

Connecting MySQL to HTML: Step-by-step guide

I am currently working on building a website through coding in HTML using gedit. My goal is to have a login or registration feature on the homepage, which will then direct users to their own personalized page on the site. On this page, they should be abl ...

Tips for preventing text wrapping in off-canvas design

Seeking advice for my Web Application prototype - looking to prevent text wrapping in off-canvas menu paragraphs using CSS or JS. New to building this type of menu, I used an example from W3Schools to achieve the current design. <!DOCTYPE html> ...