hyperlinks along the edges surrounding a centrally positioned image

I'm facing an issue with an image that is also a link. I used the following code:

<a href="link.html"><img src="img.png" id="cloud"></img></a>

After, I centered and resized it with the help of this CSS:

#cloud
{
display: block;
margin-left:auto;
margin-right:auto;
height:45%;
}

The problem is that the clickable area for the link extends beyond the image itself and covers the entire web page. How can I go about fixing this issue?

Answer №1

Consider using absolute positioning for optimal results.

HTML:

<a href="link.html" >
    <img id="centerimage" src="https://pbs.twimg.com/profile_images/378800000442759461/b483cdd049f470928e7b20051f95b8cc.jpeg" />
</a>

CSS:

#centerimage {
    position: absolute;
    left: 0;
    right: 0;
    top:0;
    margin: auto;
}

To ensure the element stays within the structure of your document, utilize a wrapper with a relative CSS class.

HTML

<div class="wrapper">
    <a href="link.html" >
        <img id="centerimage" src="https://pbs.twimg.com/profile_images/378800000442759461/b483cdd049f470928e7b20051f95b8cc.jpeg" />
    </a>
</div>

CSS

.wrapper {
    position:relative;
    margin-top:25px;
}
#centerimage {
    position: absolute;
    left: 0;
    right: 0;
    top:0;
    margin: auto;
}

Enhancement: The issue with your link stretching across the page was due to using display:block; without a proper containment. A wrapper is essential, along with defining an absolute position for your img inside it to maintain control over its location within the document structure.

Simply wrapping it and styling the wrapper element while setting absolute position for the img will keep it contained, allowing you to position the wrapper as needed in your document through regular means, hence the use of relative style.

Answer №2

Unsure if this method will be effective, but consider trying the following:

<img src="img.png" id="centerimage" href = "link.html"/>

If the above code does not have the desired effect, you may want to explore using JavaScript or jQuery:

<img src="img.png" id="centerimage" onClick = "click" href = "link.html"/>

<script>

function click() {
window.location.href = "link.html";
}

</script>

Additionally, include the following CSS code:

cursor: pointer;

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 reveal CSS files downloaded via npm?

I'm currently working on a Sass/CSS library that I have hosted on npm. I am exploring ways to make it easier for clients to access it, such as using: @import 'library_name' Instead of the less appealing option: @import 'node-modules/ ...

Incorporating a HTML layout with a JS backdrop

I have successfully implemented a JavaScript background and now I want to apply this background across all my PHP files. The issue is that the HTML code either overlaps with the JS content or appears behind it. How can I resolve this? Below is the JS fil ...

Text alignment in the horizontal direction does not function as expected within the `<span>` element in Bootstrap 4

The code snippet below is what I am currently working with: <div class="card" > <div class="card-block"> <h4 class="card-title"> <span class="text-left"> ...

"Quotes are essential in Javastript syntax for specifying string values

I need to implement a JavaScript syntax to generate unique URLs for each image. The Robohash website provides random robot images based on different URL endings. I tried the code below, but it seems like ${props.id} is being interpreted as part of the UR ...

`Custom transitions between sections using fullpage.js`

Has anyone used the fullpage.js extension to achieve an animation similar to this one, where sections are destroyed on scroll? ...

Using Polymer and D3: A guide to transferring JSON data from element attributes to templates

As a newcomer to polymer, I wanted to integrate D3 with it. After modifying an existing Gist, I encountered some challenges. My goal is to pass JSON from a Polymer element attribute (barData) in HTML to the polymer template. The JSON is passed as a String ...

The CSS background gradient appears distorted on larger screens

https://i.sstatic.net/SN1j8.jpg This layout design is my own creation. It's structured like this: <div class="outer"> <div class="container"> <div class="content"></div> <aside></aside> </div> < ...

Arrange image, icon, and title in Bootstrap navigation bar

index.html <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> </ul> <span class="navbar-text" s> <ul class="navbar-nav mr-auto ...

How can I ensure that my CSS code functions seamlessly across all versions of Internet Explorer from IE6 to the latest release?

I need a solution for getting this CSS code to function properly across all versions of Internet Explorer, from IE6 to the most recent version. body:first-child * { margin-top:0 !important; } body:last-child * { margin-bottom:0 !important; } Any ...

Align Bootstrap list items vertically

I'm facing a challenge with aligning the yellow list item blocks vertically in my design. The issue arises because the black dog item is not aligning properly due to its image being shorter than the other two blocks. I've been struggling to find ...

Div scrolling allows for parallel viewing of PDFs in a side by side arrangement

I have two scrollable elements on my webpage: one positioned on the left side and the other on the right side. The left element is a regular div, while the right element is an object containing an embedded PDF. <object width="100%" height=&quo ...

What is the best way to access the width property within the style attribute of a span element with a specific class

https://i.sstatic.net/1YQdP.png Currently, I'm attempting to extract the content inside "width: 100%" and specifically the numerical part. My approach involves using beautifulsoup in Python, and you can see the snippet of my code below: rat ...

The browser is able to cache the selected state of a select tag and will disregard any instances where selected=

When rendering a drop down box, I include a currently selected value using selected="true". <select id="dropDown"> <option selected="true" value="1">value2</option> <option value="2">value3</option> <option va ...

Implementing CSS keyframes when a specified PHP condition is satisfied

I am looking to implement an opening animation on my website that should only play when a user visits the site for the first time. I want to avoid displaying the animation every time the page is reloaded, so it should only run if a cookie indicating the us ...

Using the HTML <span> and svg elements together does not allow them to be inline with each other

In my current scenario, I have a fieldset that is inline with another one on the page. The specific fieldset in question contains an SVG circle and some text. My objective is to have both elements inline with each other, while allowing the fieldset to adj ...

Navigation Bar Dropdown Menu Not Responding to Clicks

I'm having trouble implementing a dropdown menu in my navigation bar that should appear when the user clicks and disappear when they click anywhere outside of it, similar to Facebook's dropdown menu with options like logout. However, for some rea ...

Is the PHP Form receiving various POST values during submission?

I'm currently facing an issue while trying to create a signup form using HTML/PHP. When I enter my password, it gets replaced with a previously saved password upon submission, and I'm unsure about the reason behind this. Here is the code for my ...

Broken href link on background image with absolute positioning

Having trouble adding a link over a background image of the head-bg-image div into ul li. The link is not functioning properly. <div id="product_banner"> <div class="head-bg"> <div class="head-bg-img"></div> <ul class="d-inline ...

What is the method to activate/deactivate a button depending on a specified input value?

A simple application features an input field for a URL and a button. Upon clicking the button, the JavaScript code within it replaces the domain of the entered URL with a new domain. For instance, if a user enters "www.old.company.com/products," the appl ...

What is the best way to retrieve a value from a database and display it in a radio

Can you help me figure out how to implement this functionality? I have a database with a "boolean" field that stores 0 or 1 values. On an HTML page, there is a radioButton with options for OK or NO. I need to dynamically load the boolean value from the dat ...