Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to place a div link exactly on an image?

Featured Replies

Hi everyone,

 

I have an image link which is roughly in the centre of my page:

 

2i94m0.png

 

Using CSS, I shift the background position on hover so that the purple band brightens slightly.

 

 

I was wondering how I would recreate this link using just a plain version of the image and creating the purple band/text with HTML/CSS. It's easy enough for me to create a div box, fill it with a purple background, and add the text. But how would I exactly align this purple div on top of the image? And is there CSS hover code which would lighten the purple band on hover?

 

 

Thanks for your help everyone.

 

  • Author

If it helps, my current CSS code is:

.equalityresearchlink {
display: inline-block;
float: center;
width: 200px;
height: 525px;
text-decoration: none;
background: url("img/equality_research_link.png");
}

.equalityresearchlink:hover {
background-position: -200px 0;
}

Thanks

You will want to wrap your image and text inside a div with position relative. Then the text, wrap in a div with position absolute; Then you can position that second div with top:0px and left:0px; - something like:


<div class="image-container">
    <img src="my-image.jpg" />
    <div class="image-container--text">
        My Text
    </div>
</div>



.image-container { position: relative; }
.image-container--text { position: absolute; left: 0px; top: 300px; width: 200px; background-color:#ffffff; color:#000000; text-align:center; height: 60px; line-height: 60px; }

Edited by EpicWebs

  • Author

Cheers davep, really appreciate your input. I see now it's quite simple. When I tried it before, I think I was using the position:absolute/relative incorrectly.

 

 

Many thanks.

I forked @@davep 's pen and adjusted it so the text is positioned directly in the center of the image (image can be any size) http://codepen.io/rbrtsmith/pen/MYQrWm

 

Also made the whole image a link, I think that generally makes for better ux to have larger click area.

Edited by rbrtsmith

@@rbrtsmith you're such a forker sometimes :)

 

nice use of display table but from a design point of view i would normally go with something like the golden ratio/two-thirds rule ;)

after i did it i was thinking that whole image hover was better but it was going to take me longer to think how to do it...

 

btw i'm probably being dense but in your fork where's the closing pair of your anchor tag? or am i just missing something?

oops I forgot the closing </a>.

 

I do quite a bit of photography so I like the golden ratio, but that can prove a little tricky with responsive web, where we cannot determine what height the image will be at a given screen size. We can of course use JavaScript for this if really needed. Unfortunately table doesn't allow for vertical-align: two-thirds :(

 

Edit - that said, with some fiddling it could be possible to use the padding-bottom percentage trick to simulate the 2/3rds thing, but without testing I think we would still need to know the aspect ratio of the image. I'll have a play in a few hours and see if I can come up with something.

 

 

Edit, easier than I thought. Remove the tables, set the bottom property of the overlay to 33.333% and then the text element will need to be transformed to fit exactly over the center of this bottom position: transform: translateY(50%)

http://codepen.io/rbrtsmith/pen/dPdJpr

Edited by rbrtsmith

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.