Using CSS to Align Two Images in a Relative Position

Imagine a scenario where you have one picture with a maximum width of 1200px and a relative width of 85%. Now, on top of this image lies another smaller picture that acts as a link. This second picture was cropped from the first to serve its link purpose.

The challenge is in positioning these images using absolute values. No matter how much I adjust the numbers, they only seem to fit perfectly at one specific point when resizing the viewport.

My goal is to create multiple clickable parts within an image, resembling a menu. The example I aspire to achieve can be seen here, but with resizable images.

Displayed below is my current code along with the image links: Link with the images

Any guidance or assistance on this matter would be greatly appreciated! Thank you!

body {
            width: 100%;
            background: black;
        }
        div#bgrImg {  
            max-width: 1200px;
            width: 85%;
            position: absolute;
            left: 7.5%;
            top: 8.3%;
        }
        #bgrImg img { 
            width: 100%;
        }
        div#signLink { 
            max-width: 1200px;
            width: 85%;
            position: absolute;
            left: 7.5%;
            top: 26%;
        }
        #signLink img { 
            width: 22%;
            position:absolute;
            left: 51.5%;
        }
        #signLink:hover {
            cursor: pointer;
        }
<html>
            <body>

            <div id="bgrImg">
            <img src="_images/hillBgr.jpg" />
            </div>

            <div id="signLink">
            <a href="index.htm"><img src="_images/hillSign.png"/></a>
            </div>

            </body>
            </html>

Answer №1

One method I discovered involves using SVG for image hotspots. By using Adobe Illustrator or another vector software, I saved my images as .svg files. In the .svg file, I created a vector path over the specific area of the image that I wanted to use as a hotspot link. Upon opening the .svg file in a text editor, I found a path-tag below the image-tag which defines the location of the 'hotspot-layer' on the image. You can copy the entire XML code from the svg file into the HTML. An example is provided below. Although I'm not an expert in this field, this technique worked perfectly for me. The svg-hotspot scales along with the image and can be styled using CSS.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>svg mapping</title>
</head>

<body>

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="-359 46.36 480 308.64" style="enable-background:new -359 46.36 480 308.64;" xml:space="preserve">
    <image style="overflow:visible;enable-background:new ;" width="2000" height="1288" xlink:href="yourImage.jpg" transform="matrix(0.24 0 0 0.2396 -359 46.36)">
    </image>
    <a xlink:href="https://www.yoursite.com">
        <path style="opacity:0;fill:#FFFFFF;" d="M-35.042,281.926c0,0,6.346,1.664,8.53-0.936s0.312-12.067,1.456-18.308
            c1.144-6.241,6.45-20.181,10.506-22.261c4.057-2.08,6.241-3.849,8.53-3.849s5.825-0.104,7.282-1.664
            c1.456-1.56,3.641-4.889,2.08-8.01s-2.08-8.114-5.201-8.842c-3.121-0.728-3.121-0.728-3.121-0.728s-3.953,1.04-5.305,0
            c-1.352-1.04-14.459-16.228-24.03-14.563s-17.684,2.08-20.909,9.674c-3.225,7.594-2.393,11.339-0.936,16.956
            c1.456,5.617,2.601,12.795,1.352,15.812c-1.248,3.017-4.057,12.483-1.248,17.684C-53.246,268.09-51.582,276.204-35.042,281.926z"/>
    </a>
</svg>


</body>
</html>

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

What is the best way to achieve a sophisticated layout using the flex grid in CSS3?

Looking to achieve a responsive layout using flex without the need for media queries. Can anyone provide the CSS code for a layout using flex or grid? I've attempted the following: .parent { display: flex; flex-wrap: wrap; height: 100vh; } ...

iOS Safari does not support CSS transforms when applied to parent elements

As a workaround, I decided to create a mockup of the scenario since developing a testable version wasn't straightforward. However, here is the gist of the issue: @keyframes mainFadeIn { 0% { opacity: 0; transform: translateY(-3rem); } ...

Error encountered when attempting to upload a canvas base64 image to parse.com with a broken image link

My application consists only of HTML and JavaScript. Take a look at my code snippet below for saving the base64 imgData on Parse.com: var parseFile = new Parse.File("mypic.jpeg", {base64:imgData}); parseFile.save().then(function(data) { console.log(" ...

Creating a Delicious Earth Progress Pie

I'm looking to incorporate a unique progress bar design on my website, similar to this one: The progress pie Can anyone guide me on how to create a progress pie with an image inside it that changes color as it moves? ...

There seems to be an issue with vertically centering row divs in Bootstrap

Struggling to center a row div vertically in an ASP.NET MVC project? Despite numerous attempts, the content remains at the top. Here's a quick fix with some eye-catching colors to illustrate the issue: Site.css: html, body { height: 100%; } #mm ...

Connect various inputs in Vue.js so they can be updated simultaneously

I need to have multiple text inputs that are interconnected, so when I change the value of one input, the others should update with the same value. These inputs are being generated dynamically in a loop using v-for as shown below: <tbody> <varia ...

Looking to refresh the dropdown menu utilized in HTML with Django

I created a Django web application and included a drop down field using the following code: <select id="countrynamew" name="countrynamew" value="{{countrynamew}}"> <option value="Abkhazia">Abkhazia</ ...

Ways to display only text within an INPUT element in HTML

Is there a way to only display text inside an INPUT element? <p><input type="image" src="" border="0" name="submit" alt="Donate"></p> Although it works well, Safari and Chrome do not show the alt="Donate", instead displaying an empty sq ...

Guide on adjusting the CSS styling of elements in real-time from the backend using a user customization panel to modify the appearance of various web pages

Imagine a scenario where we have a website consisting of multiple pages, including a user account page. The user has the ability to modify the color, font size, and style of certain elements on other pages for their own viewing preferences. How can this fu ...

Activate the DropDown feature by utilizing the Three Dots span

When working with react-bootstrap, I encountered a challenge involving a button with a caret that opens a dropdown menu. https://i.sstatic.net/7cfN3.png However, my requirement is to replace the button and caret with vertical three dots styled differentl ...

Opacity is causing issues with hover effects in CSS

I'm facing an unusual CSS challenge. Check out this simple code snippet below that showcases the issue: <html> <head> <style> .hover { float: right; } .hover:hover { background-color: blue; ...

Exploring how to set dropdown menu width for Angular2 mat-select options

Currently, I am using the angular2 mat-select control and facing an issue with the width and position of its dropdown list menu. By default, it is wider and overflows the element on both sides by a few pixels. I have not found any option for adjusting the ...

How can I align all the columns in every row to the left except for the first one?

To achieve the goal of making all columns except the first of a row in a table to have a text alignment of left (with the first column having a defined text alignment in CSS), the following code is utilized: <script type="text/javascript" src="../js/jq ...

CSS: Adjusting the vertical alignment of numbers in an ordered list

Hey there, I have a question about the vertical alignment of numbering in an ordered list. It seems that the numbers are being pushed to the baseline when <li> elements contain floated divs only. Is there any way to prevent this and have the numbers ...

Making videos using base64 images captured in selenium

I am capturing screenshots using selenium headless mode and obtaining them as base64 images from the webdriver. My goal is to transform these screenshots, taken every 100ms, into a video format. Any assistance would be greatly appreciated. ...

Trouble accessing Google Maps due to map loading issues?

I have tried numerous solutions to fix my issue, but none of them seem to work. My map just won't load. Here is the HTML snippet causing trouble: <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> <script> ...

When a button is clicked, the event is not triggered within a FirefoxOS application

Despite functioning perfectly on Firefox for desktop, my application encounters issues when tested on my ZTE Open C (Firefox OS 1.3). Pressing the button does not trigger any action, while onmouseup and onclick events work flawlessly. Even attempts to bin ...

Multer in NodeJS is malfunctioning

I attempted to implement file upload functionality using NodeJS, ExpressJS, and Multer but encountered issues. The version of ExpressJS I am using is 4.12.3 Below is a snippet of my source code: server.js: var express = require('express'), ...

Attempting to transfer various variables from several windows using AJAX

Is it possible to pass multiple variables from two different windows into the same PHP script? If not, what would be the best approach to take? Thank you. verifyemail.html <script type = "text/javascript" src = "js/js_functions.js"></script> ...

Using the MVC framework, create a hyperlink with custom CSS styling by

Having trouble getting a specific class to override the default styles for a:link @Html.ActionLink("Back to List", "Index", null, new { @class = "htmlactionlink" }) CSS a:link, a:visited, a:active, a:hover { color: #333; } .htmlactionlink { co ...