Looking for assistance with aligning an image in a <td> cell that doubles as a link?

How can I center a play icon in the middle of a td cell and make the entire content clickable as a link, including the background image? I've tried various methods but can't seem to get it right without breaking the alignment. Any suggestions would be greatly appreciated!

<table>
 <tbody>
  <tr>
   <td width="244" height="186" style="background:bg-image.png no-repeat center;background-size:100%;text-align:middle">
    <a href="action.htm" style="height:100%;width:100%;" >
     <img src="icon_play.png"/>
    </a>
   </td>
  </tr>
 </tbody>
</table>

https://i.stack.imgur.com/7pzdk.png

Answer №1

Is the use of tables for layouts recommended?

Before proceeding, it's important to confirm whether a table is being used for layout purposes in this case.
If so, it's not advisable.

1. Sizing Recommendations

It is suggested to set the size of the anchor element rather than the cell as shown in the snippet below.

2. Achieving Vertical Centering

In the past, achieving vertical centering involved using a specific hack. However, now you can utilize the calc feature which offers better support:

top: calc(50% - 35px);

3. Utilize the :after Pseudo Element

An alternative method involves utilizing the pseudo-element :after without nesting an image within each anchor tag.

a:after {
  background-image: url("https://i.stack.imgur.com/5b8Bn.png");
  /* Additional styles required, refer to the snippet */
}

Experiment with the following snippet...

a {
  display: block;
  width: 244px;
  height: 186px;
  background: url("https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg/1024px-Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg") no-repeat center;
  background-size: 100%;
  text-align: center;
}
a:after {
  display: block;
  width: 70px;
  height: 70px;
  position: relative;
  top: calc(50% - 35px);
  left: calc(50% - 35px);
  content: " ";
  background-image: url("https://i.stack.imgur.com/5b8Bn.png");
}
<table>
  <tbody>
    <tr>
      <td>
        <a href="#">
        </a>
      </td>
    </tr>
  </tbody>
</table>

Enhanced Approach: Consider implementing the :after technique along with using an actual <img> for your background for improved results:

a {
  display: inline-block;
  position: relative;
}
img {
  display: block;
  width: 244px;
  height: 186px;
}
a:after {
  display: block;
  width: 70px;
  height: 70px;
  content: " ";
  position: absolute;
  top: calc(50% - 35px);
  left: calc(50% - 35px);
  background-image: url("https://i.stack.imgur.com/5b8Bn.png");
}
<a>
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg/1024px-Amy_Grant_on_Stage_at_the_Peppermill_Concert_Hall_in_West_Wendover,_Nevada.jpg" />
</a>

Answer №2

align-text: center;

To align content to the center, use this property.

Have you verified that the image size matches the td cell size?

Answer №3

It appears that the issue lies in the lack of kittens in your background image selection.

Furthermore, it is worth noting that using text-align: middle is not a valid CSS property. The correct way to center text would be text-align: center.

To address this, you may consider employing absolute positioning while considering the dimensions of your icons. Personally, I prefer utilizing calc for this purpose, although there are numerous other methods available as outlined in various responses. Here is how I resolved the matter:

<table>
 <tbody>
  <tr>
   <td style="background:url('http://zuma-design.com/shared/so/kittens.jpg') no-repeat center;background-size:100%;position:relative;width: 700px; height: 500px;">
    <a href="#" onclick="alert('meow')" style="width: 100%; position: absolute; height: 100%; right: 0px; bottom: 0px;" >
    <img style="position: absolute; top: calc(50% - 95px); left: calc(50% - 95px);" src="http://zuma-design.com/shared/so/play.png"/>
    </a>
   </td>
  </tr>
 </tbody>
</table>

Answer №4

For your <a> style, ensure you include the following: display:inline-block

<a href="action.htm" style="display:inline-block;height:100%;width:100%;" >

By default, <a> tags have a display property of inline.

Answer №5

I utilized the band image as a placeholder for the video and then enclosed the video element within an anchor tag. To exit the video, simply use the browser's back button or the back key.

var anchor = document.querySelector('a');
var img = document.querySelector('img');
anchor.addEventListener('click', play, true);
var test = document.getElementById('test');

function play(event) {

  event.preventDefault();
  if (test.paused) {

    test.play();
    img.style.left = "-9999px";
    a.style.backgroundSize = '0%';
  } else {
    test.pause();
  }
}
a {
  display: block;
  width: 100%;
  height: 85%;
  background: url("https://glpjt.s3.amazonaws.com/so/av/photo.png") no-repeat center center;
  background-size: 150%;
  text-align: center;
}
img {
  position: absolute;
  margin-top: 0;
  top: 0;
  width: 25%;
}
video {
  top: -20px;
  position: relative;
}
table {
  table-layout: fixed;
  width: 100vw;
}
<table>
  <tbody>
    <tr>

      <td>
        <a href="https://glpjt.s3.amazonaws.com/so/av/vs2s3.mp4" target='_self'>
          <video id="test" poster="https://i.stack.imgur.com/7pzdk.png" controls height="auto" width="100%">
            < source src="https://glpjt.s3.amazonaws.com/so/av/vs2s3.mp4" />



          </video>
        </a>
      </td>

    </tr>
  </tbody>
</table>

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 create three buttons for selecting various parameters?

I have a code snippet where I want to assign different parameters to each button when clicked. However, despite my logic, the functionality is not working as expected. Can someone help me with the correct syntax? For example, if I click the "Start (Easy) ...

What could be the reason for my CSS selector with :target not functioning properly?

I tried to implement the instructions I found online for using :target, but it's not working as expected. My goal is to change the background color and font color of #div1 when the first link is clicked, and to change the border of #div2 when the seco ...

Adjust the appearance of input fields that exclusively have the value "1"

When designing my website, I encountered a problem with the style I chose where the number "1" looks like a large "I" or a small "L." I am wondering if there is a way to use JavaScript to dynamically change the font style for input fields that only contai ...

What steps are involved in creating a mobile web app using Visual Studio 2012, jQuery, and ASP.NET MVC?

As a beginner in asp.net, I am tasked with creating a mobile web app project. Before diving into development, I would like to first focus on designing my app. Can anyone provide guidance on how I can achieve this? Any help is appreciated. ...

Deselect the checkbox if you are checking a different checkbox

Struggling with my code, it's hit or miss. Feeling defeated. Seeking assistance to uncheck a checkbox when another checkbox is checked. Check out the code below: JavaScript: $("#1 #checkAll").change(function() { if ($("#1 #checkAll").is(' ...

Retrieve the height of a div element in an AngularJS framework, and assign this value to a corresponding attribute of another element

Is it possible to retrieve the height of an element using AngularJS and assign it to another div's attribute? In my Bootstrap website, I have a fixed sidebar that needs to stop before reaching the footer. To achieve this, I currently have to manually ...

How can I make Bootstrap fit my Custom Grid?

I have received specific measurements from a customer for a customized grid layout, and I am unsure of how to implement it. Here are the measurements provided: **Width: 320px** Sides: (26px) (2 sides) Column: (52px) (4 columns) Gutter: (20px) (3 gutter ...

Hover over the hidden DIV

I have a div containing an image that is overlapping multiple other divs. It looks something like this: <div style='width:100%;height:100%'><img src='someImage.png'></div> <div id='covered'>I'm un ...

Issue with AJAX MVC HTML: jQuery value is not blocking API call

Upon clicking a button, I am triggering a web API call using AJAX. My form is utilizing JqueryVal for form validations based on my viewmodel data annotations. The issue I am facing is that when I click the "Listo" button in my form, it executes the API ca ...

Incorporating Bootstrap content directories into ASP.NET website projects

I've been experimenting with Bootstrap for website design, but I'm facing an issue with adding files to my project. When creating a project in Visual Studio 2015 using File/New/Web Site..., I'm able to simply Copy/Paste the css, fonts, js fo ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

How about a CSS grid featuring squares with square images?

Just like the inquiry found on this page, I am striving to construct a grid of perfect squares, each containing an image with 100% height. The issue arises from utilizing the padding-bottom: 100%; height: 0 method, which prevents height: 100% from functio ...

Issues arise when attempting to insert data into an HTML file using PHP

Good evening, I am having a problem where when I attempt to use PHP to replace "hello world" in an HTML file, it ends up completely empty each time. Can anyone point out what mistake I might be making? Here are the HTML and PHP files: <!DOCTYPE html&g ...

Java and Selenium: Struggling to Find Element based on Attribute

Attempting to click a button on a webpage with the given HTML code: <html lang="en" webdriver="true"> <head> <body class="scbody" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAKCAYAAAB10jRKAAAAGXRFWHRTb2 ...

Is there an issue with the newline character ` ` not functioning properly in TypeScript when used with the `<br/>` tag?

Having trouble with using New Line '\n' ' ' in Typescript Here is an example of my typescript code: this.custPartyAddress = estimation.partyName + ',' + '\n' + estimation.partyAddress + ',' + ...

What is the best way to override default CSS properties for a:hover on linked images?

I have a printer-friendly/PDF image that looks like this: Simple enough. However, when hovering over it, the background turns grey due to default hyperlink styles set as follows: #footer a:hover { color: white; background-color: #636363; -moz-transition: ...

Examining the false positive detection of an apparent visibility issue with

My goal is to check if the document is NOT scrollable using this code snippet: $el = document.documentElement const noscroll = $el.clientHeight === $el.scrollHeight // false console.log($el.clientHeight) // 977 console.log($el.scrollHeight) // 991 consol ...

Issues with CSS filters affecting the layout of webpage elements

Is there a way to keep a div pinned to the bottom of the viewport while applying a filter to the body in CSS? I tried using position: fixed; bottom: 0px, but it seems to mess up the positioning when a filter is added to the body. body { filter: bright ...

What is the best way to mark a MenuItem as active within a Material UI Drawer component?

Here is the code snippet I am working with: <Drawer docked = {false} width = {330} open = {this.state.drawerOpen} onRequestChange = {(drawerOpen) => this.setState({drawerOp ...

Exploring Ancestors with Jquery to Find Input Fields

In my current project, I am working on a function that needs to extract the values from two input fields within a specific div element. These values will then be added to an array and posted as JSON data. However, I am facing difficulties in navigating thr ...