CSS Magic: Creating Responsive Image Anchor Tags with a Touch of Image Maps

While working on a responsive website, I encountered an issue with Image Maps not functioning properly with percentage-based co-ordinates. After researching online, I came across a JavaScript workaround at this link. However, my goal is to make the site functional even with JavaScript disabled.

After exploring various options, I decided to investigate using relatively positioned Anchor tags over the images as an alternative. In my opinion, this method offers better results. Although, I faced challenges when trying to position the anchor tags over the image using percentage-based size and positions - they moved disproportionately every time I resized the browser window.

HTML:

<div id="block">
  <div>
    <img src="http://www.wpclipart.com/toys/blocks/abc_blocks.png">
  </div>
  <a href="#" class="one"></a>
  <a href="#" class="two"></a>
</div>

CSS:

#block img {
  max-width: 100%;
  display: inline-block;
}

a.one{ 
  height:28%;
  width:19%;
  top:-36%;
  left:1%;
  position:relative;
  display:block;
}
a.two{
  height:28%;
  width:19%;
  top:37%;
  left:36%;
  position:absolute;
}

Check out the jsFiddle here for a visual representation of the issue - http://jsfiddle.net/wAf3b/10/. You can see that everything gets skewed when resizing the HTML box.

Any assistance would be greatly appreciated.

Answer №1

There were a few issues with the CSS in your code snippet, along with a missing closing div tag. I adjusted the positioning of #block to be relatively positioned instead of 100% height, and made sure the anchors were block/absolutely positioned so that the tags moved correctly with the blocks.

Here is the link to the updated fiddle:

http://jsfiddle.net/wAf3b/24/

CSS

html, body {
  height: 100%;
}

#block{ float:left; width:100%; max-width: 400px; position: relative; }

#content{
  height: 100%;
  min-height: 100%;
}

#block img {
  max-width: 100%;
  display: inline-block;
}
a.one{ height:28%; width:25%; position: absolute; top:55%; left:5%; display:block; background:rgba(0,255,0,0.5);}
a.two{ height:28%; width:25%; position: absolute; top:60%; left:70%; display: block; background:rgba(255,0,0,0.5);}

HTML

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link href="stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" />
    <title>Bulky Waste</title>
</head>
<body>
    <div id="content">
        <div id="block">
            <div>
                <img src="http://www.wpclipart.com/toys/blocks/abc_blocks.png">
            </div>
            <a href="#" class="one"></a>
            <a href="#" class="two"></a>
        </div>
    </div><!--/content-->
</body>
</html>

It's worth mentioning that some browsers may have trouble if the DOCTYPE declaration is not capitalized in the HTML file.

Answer №2

Elements with absolute positioning are now excluded from the overall layout, which means they cannot pick up relative size properties from their parent elements. Using JavaScript is necessary to achieve your desired outcome.

Users who have disabled JavaScript typically anticipate a less optimal browsing experience anyway.

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

Why isn't the float left property working as expected?

Why won't my image float to the left? I'm using a class called align-left with float: left but it's not working. You can see the live version at - (check out the review grid halfway down under the heading 'High customer satisfaction r ...

Transform CSS into React.js styling techniques

My current setup involves using Elementor as a REST Api, which is providing me with a collection of strings in React that are formatted like this: selector a { width: 189px; line-height: 29px; } Is there a tool or library available that can conver ...

Is there a way for JavaScript to generate an image and smoothly transition it from its initial state to its final style?

I am currently working on a JavaScript game that involves moving IMG elements by adjusting their x/y/width/height/opacity properties and utilizing CSS transitions to smoothly move them to their target locations, triggering an event upon arrival. Everything ...

Analyzing data visualization within CSS styling

I have the desire to create something similar to this, but I am unsure of where to start. Although I have a concept in mind, I am struggling to make it functional and visually appealing. <div id="data"> <div id="men" class="shape"></di ...

Inspecting HTML elements with Capybara for class and ID attributes

Trying to locate an element and use Capybara for validation. There's a specific element that I want to check for using page.should have_css. <i class="ui-grid-icon-cancel" ui-grid-one-bind-aria-label="aria.removeFilter" aria-lab ...

Shift the inline form towards the right within the navigation bar

I have the code located at this link. How can I align the inline form to the right? I am using Bootstrap 4 and have tried using the float-right class, but it doesn't seem to be working. Is there something I am missing here? <!DOCTYPE html> < ...

Utilizing the text field feature within Twitter Bootstrap 3.0 in-line styling

I am attempting to create a horizontal form with an inline text field split into two sections: one on the left and one on the right. The left side will contain horizontal text fields, some of which are inline, while the right side will have a mix of inline ...

Exploring the origins of CSS through patch design

Want to learn how to create a background like this using CSS? Check out this link for inspiration! ...

The fixed sidebar refuses to pause before reaching the footer

I am facing an issue while building my blog. I am trying to figure out how to make my sidebar widget sticky and stop scrolling before reaching the #footer section. My widget is overlapping my footer and it would be really helpful if someone could assist m ...

Opacity filter malfunctioning

It seems that the filter: opacity property is not functioning as expected in IE8. Despite having used it successfully before, I am currently facing issues with its implementation. Strangely enough, Firebug does not display the filter rule at all; only norm ...

Adjust the font sizes in Bootstrap4 to be in proportion with the Scss $font-size-base

I am currently customizing Bootstrap4 and facing challenges in understanding why certain elements are not functioning as anticipated. When modifying the original _variables.scss file by adjusting the $font-size-base, ideally, it should reflect across rela ...

Is the accuracy of the alsoResize functionality in Jquery UI Resizable questionable?

After removing the 'alsoResize' option, I found that the behavior remains unchanged. Upon resizing the main element, I noticed that the black border of the bottom element, 'marquee,' often shifts out of alignment with the dashed white ...

Script modifies div dimensions independently of CSS styling

Recently, I received assistance from this community in creating a script that displays random images from an array within 4 divs. Each image changes upon refresh or with a timer. HTML: <section> <div id="square_1" class='box'> ...

The method of stamping masonry is not encircling

I recently discovered a new method in Masonry 3 called "stamp" that allows you to fix an element in place. However, I am finding that it is not working as expected. Check out this example from David DeSandro: http://codepen.io/desandro/pen/wKpjs Initial ...

Tips for incorporating a zoom feature into a bootstrap carousel

I've been attempting to incorporate a zoom effect into a bootstrap carousel, but for some reason, I can't seem to get it to work :( I suspect that I may need to insert the syntax into the core _carousel.scss file (or maybe not?). Despite my atte ...

The conceal feature doesn't appear to be functioning properly on jQuery Mobile

I am facing an issue with a small mobile app built using jQuery Mobile. Within a div, I have three buttons and other content. My goal is to hide these three buttons if the user's device is running on Windows (WP). The buttons are defined as follows: ...

The rendering of the font appears distinct when viewed on an iPad compared to when displayed

Visit this website. The menu displays correctly on desktop, but on iPads, the font appears larger leading to a line break. Is it because the selected font isn't loading and defaulting to Times New Roman instead? It seems likely since I experience the ...

Utilize ReactJS to remove the img element when it is not present

Is there a way in my react component to replace a non-existing image with nothing using the onError method? render() { return ( <img src={myImage} onError={(e)=>{/*Code to handle replacing the image*/}}/> ) } Alternatively, I attempted t ...

The font color in Bootstrap is set to be lighter than the background color

Bootstrap has made great improvements in displaying navbar item colours that blend well with the background colour of the navbar. Can we apply a similar technique to body text as well? For example, if the container background is purple, can we have the t ...

JavaScript function to toggle visibility and scroll back to top of the page

I have been an avid reader on this platform, and I am hoping that my question has not already been addressed. I am struggling to find a solution to a problem I am facing. There are two DIVs in my code that I toggle between showing and hiding using Javascr ...