Leveraging background images in HTML and CSS to create interactive clickable links

I'm attempting to create the illusion of a clickable background image using HTML and CSS, following a tutorial I found here. Unfortunately, I'm encountering two issues:

1) The link is not fully covering the background image

2) The link does not move off the screen as intended

I am working with an HTML code integration block on a Weebly website. My knowledge of HTML and CSS is still at a beginner level.

<a href="website.net/link" title="photo" id=«photo»>photo</a>

<a class="photo"></a>

<style type="text/css">

.photo {
    background-image: url(myImageLink.jpg);
    background-size: 300px;
    background-repeat: no-repeat;
    background-position: center;
    border-radius: 50%;
    background-clip: border-box;
    transition: background-size 0.2s;
    transition-timing-function: cubic-bezier(.07,1.41,.82,1.41);

    display: block;
    width: 190px;
    height: 190px;
    text-decoration: none;
    cursor: pointer;
    overflow: hidden;
    text-indent: 100%;
    white-space:nowrap;
}

.photo:hover {
    background-size: 500px;
}

</style>

Answer №1

Check out the demo here

Create a stylish link using just one <a> tag. Add a background, set the necessary href attribute, and ensure it has a block display.

Here is the HTML snippet:

<a class="photo" href="website.net/link" title="photo" id="photo">photo</a>

And here is the accompanying CSS code:

.photo {
    background-image: url('http://www.thinksnaps.com/wp-content/uploads/2014/07/images-background.jpg');
    background-size: 300px;
    background-repeat: no-repeat;
    background-position: center;
    border-radius: 50%;
    background-clip: border-box;
    transition: background-size 0.2s;
    transition-timing-function: cubic-bezier(.07,1.41,.82,1.41);

    display: block;
    width: 190px;
    height: 190px;
    text-decoration: none;
    cursor: pointer;
    overflow: hidden;
    text-indent: 100%;
    white-space:nowrap;
}

.photo:hover {
    background-size: 500px;
}

Answer №2

Make sure to review your HTML code and ensure it matches the following structure:

<a class="image" href="website.com/link" title="image" id="image">image</a>

Answer №3

Illustrating how .svg images can be utilized as clickable backgrounds. Note that the links' text is concealed by css but can be displayed along with clickable .svg images following the text. To have the images shown before the text, modify the ::after pseudo-element to ::before in the code snippet below.

UPDATE: Included a demonstration using material design Google font icons as clickable background examples.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Clickable Background Demo</title>
  <!-- Quoting from http://google.github.io/material-design-icons/#setup-method-1-using-via-google-web-fonts
  As with other Google Web Fonts, the correct CSS will be provided to enable the 'Material Icons' font specific to the browser. A new CSS
  class named .material-icons will be declared. Any element utilizing this
  class will have the appropriate CSS to display these icons from the web font. -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  
  <style type="text/css" media="screen">
  /* Hiding clickable text on links */
  .clickable-background {
    font-size:0;
  }

  .clickable-background:hover ::after, .clickable-background ::after {
    content: '';
    display:inline-block;
    width: 48px;
    height: 48px;

    background: url('data:image/svg+xml;utf8,<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="24" height="24" fill="rgb(255, 0, 0)" viewBox="0 0 24 24"><path d="M11,1.07C7.05,1.56 4,4.92 4,9H11M4,15A8,8 0 0,0 12,23A8,8 0 0,0 20,15V11H4M13,1.07V9H20C20,4.92 16.94,1.56 13,1.07Z" /></svg>');
  }

  ...

  </style>

</head>
<body>
  <p>Note that the links below do not redirect you to the linked sites due to the default link behavior being disabled in the stackoverflow.com code snippet. Check the browser status bar and cursor changes on hover for actual testing use the entire snippet in your own test.html file.</p>
  <span class="clickable-background stackoverflow-icon">
    <a href="http://stackoverflow.com" title="Stack Overflow" target="_blank">Stack Overflow - sounds good, but the icon is better.</a>
  </span>
  <span class="clickable-background github-icon">
    <a href="https://github.com/" title="Github" target="_blank">Github - sounds good, but the icon is better.</a>
  </span>
  <p> Another fine option is using clickable font symbols. Explore more about this feature through the links below.</p>
  </span>
  <span class="material-icons clickable-home">
    <a href="http://google.github.io/material-design-icons/" title="Material Design Icons" target="_blank">Material Design Icons</a>
  </span>
  <span class="material-icons clickable-thumb_up">
    <a href="https://design.google.com/icons/" title="Download Material Icons" target="_blank">Download Material Icons</a>
  </span>  
  
  <span class="material-icons clickable-android">
    <a href="https://developers.google.com/fonts/" title="Google Fonts" target="_blank">Google Fonts</a>
  </span>
  <span class="material-icons clickable-import_contacts">
    <a href="http://alistapart.com/article/the-era-of-symbol-fonts/" title="Symbol Fonts" target="_blank">The Era of Symbol Fonts</a>
  </span>
  <span class="material-icons clickable-import_contacts">
    <a href="https://material.google.com/style/icons.html" title="Material Icons Geometric Shapes" target="_blank">Material Icons Geometric Shapes</a>
  </span>
  <span class="material-icons clickable-phonelink_setup">
    <a href="https://github.com/google/material-design-icons/blob/master/iconfont/codepoints/" title="Code Points Reference" target="_blank">Code Points Reference</a>
  </span>
</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 split an array into four columns and allocate 10 <li> items from the array to each column?

If I have a dynamic array with 40 elements that changes on every render, how can I loop through the array and return 4 groups of elements, each containing 10 items without any repetition? I want to style these objects in the array using a parent flex con ...

Automatically load file and play audio when the page is loaded

I have created code that allows me to input an mp3 file, output the audio, and display a visual representation of the music. Currently, loading the file is done manually through an input type="file". Now, I want to change it so that the music automaticall ...

Adjust the size of an Angular component or directive based on the variable being passed in

I'm looking to customize the size of my spinner when loading data. Is it possible to have predefined sizes for the spinner? For example: <spinner small> would create a 50px x 50px spinner <spinner large> would create a 300px x 300p ...

Why does tagging P trigger popups in the DIV when the DIV contains another DIV?

JSBIN HTML <p> <div>123</div> </p> CSS p div{ background:#f00; } Encountering an odd issue here. When I input the HTML structure as shown below: <p></p><div>123</div> The CSS code fails to produce ...

Troubleshooting: Jquery Toggle Issue When Used with Adjacent Div Element

I've been puzzling over this issue for hours now. The intention of this code is to display a div, but it's just not cooperating. <div id="butbr"> <div id="sup_nav"> <div id="stup" class="bxt1"></div> <div id= ...

Passing references between multiple components

I'm currently working on an application using Material-UI in conjunction with styled-components. I am faced with the challenge of passing a ref down to the root <button> node that is created by Material-UI. Material-UI provides a buttonRef prop ...

Adjust the width of the <li> element based on the quantity of its children

If the number of list items can be changed dynamically, how can I adjust the width of each item so that the total width is always 100%? Here is an example code snippet: <nav> <ul> <li>A</li> <li>B</li> &l ...

Creating Table Rows on-the-fly

Seeking assistance and guidance from the community, I have modified code from an online tutorial to allow users to dynamically add rows to a table when data is entered in the row above. However, I am facing an issue where I can only insert one additional ...

What is the best way to reveal a hidden div using JavaScript after a form submission?

jQuery is not a language I am very familiar with, but I am attempting to display a simple animated gif when a form is submitted. When users click 'submit' to upload an image, I want the gif to show to indicate that the image is being uploaded. Th ...

Issues with html5 dropdown menu not displaying properly when hovered over

As a beginner in programming, I have been tasked with creating a website using HTML5 and CSS. The main issue I am encountering is with my horizontal menu bar that has vertical sub-menus appearing on hover. The menu consists of 3 main tabs: Home, Health, a ...

Tips for ensuring uniform button height across all cards

I am seeking advice on how to align all the buttons in different cards, despite varying text lengths, to be in the same position at the end of each card. Here is an example of what I am aiming for. Below is my HTML and CSS code: * { margin: 0px; pad ...

Attempting to showcase the text within an <a> element on a <canvas> element while ensuring there are no noticeable visual distinctions

Imagine having this snippet of code on a webpage: elit ac <a href="http://www.ducksanddos/bh.php" id='link'>Hello world!</a> nunc Now, picture wanting to replace the <a> tag with a <canvas> element that displays the same ...

What causes the unexpected behavior of JQuery's .animate() function?

I am currently working on a project that involves creating a div element for the purpose of dragging and dropping files. My goal is to have the height of the div increase when a file is dragged into it, and decrease when the file is dragged out. To achiev ...

Maintain HOVER State in CSS Drop Down Menu upon Clicking

I have implemented a CSS Drop Down Menu on my website. In a specific section of the site, I am using the menu links to trigger AJAX-jQuery calls that dynamically change the content of a DIV without the need for page reloading. <script type="text/javasc ...

AngularJS - Organize Item Hierarchy with Separate Containers for Each Group

My goal is to incorporate a $scope variable within an AngularJS controller that has the following structure: $scope.hierarchy = { name: 'bob', selected: true, children: [ { name: 'frank' }, { name: 'spike' ...

Learn the process of transferring data-target IDs to another JSP using Bootstrap

Currently, I am encountering an issue where I am trying to use bootstrap to display a dialog box by clicking on a button. The dialog box is created in HTML (.jsp) and the submit button is in another .jsp file. However, when I click on the button, instead o ...

Preventing duplicate submissions in Ajax by serializing form data

I'm having trouble preventing double submissions on my form when I rapidly press the button that triggers a click event. I attempted to disable the button after a successful submission, but it still submits twice. Here is my code: <script> $(do ...

Issue with EaselJS: mouse events are no longer functional

I'm currently working on adding a touch animation using the EaselJs library. Interestingly, when I load an image from a local folder, all mouse events work as expected, such as onPress. However, things take a different turn when I opt to use an imag ...

Identifying CSS on iPhone and iPad: A Guide

I need to adjust the CSS style based on different devices. This is how I currently have it set up: <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/Destop.css")" media="only screen and (min-width: 1224px)"/> <link rel="s ...

Utilizing Python and Selenium to Locate an Element Based on Text Within a Span Class

My challenge is locating an element without any ID or distinctive identifiers. The HTML code structure resembles the following: <div class="item_select"> <span class="item_selection_number">A </span> </div> <div class="item_ ...