Ways to position a button in the center of an image

Just a quick query about HTML and CSS. How can I position a button on top of an image in the center?

Here is the current code:

.button-link{
  text-decoration:none !important;
  color:white;
}

.button-td{
  background:#FFA06A;
  text-align:center;
}
<table align="center" border="0" cellpadding="0" cellspacing="0" class="center-on-narrow" role="presentation" style="display:table !important;" mc:edit="imagewithcta">
  <tr>
    <td class="button-td">
      <img src="https://images.unsplash.com/photo-1598257006626-48b0c252070d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;ixlib=rb-1.2.1" style="max-width:600px;" alt="photo-1598257006626-48b0c252070d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;ixlib=rb-1.2.1"><a class="button-a sans text-white" href="*|ARCHIVE3|*">
      <span class="button-link">
        READ MORE
      </span>
    </a>
  </td>
</tr>
</table>

Answer №1

To utilize absolute positioning within td, you can follow these steps.

Below is the CSS code that needs to be added:

.button-a {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 2;
  transform: translate(-50%, -50%);
}

.button-link {
  text-decoration: none !important;
  color: white;
}

.button-a {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 2;
  transform: translate(-50%, -50%);
}

.button-td {
  background: #FFA06A;
  text-align: center;
  position: relative;
}
<table align="center" border="0" cellpadding="0" cellspacing="0" class="center-on-narrow" role="presentation" style="display:table !important;" mc:edit="imagewithcta">
  <tr>
    <td class="button-td">
      <img src="https://images.unsplash.com/photo-1598257006626-48b0c252070d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;ixlib=rb-1.2.1" style="max-width:600px;" alt="photo-1598257006626-48b0c252070d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;ixlib=rb-1.2.1">
      <a class="button-a sans text-white" href="*|ARCHIVE3|*">
        <span class="button-link">
                  READ MORE
                </span>
      </a>
    </td>
  </tr>
</table>

Answer №2

To center a button, you can utilize the position CSS property:

.button-container {
    position: relative;
}

.button-element {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

By applying this code, the button will be centered within the button-container.

Additionally, instead of using a span tag for the button, consider utilizing <button> or <a> tags for better accessibility and semantics.

Answer №3

Here's a solution that should work: set the container to relative positioning and the element to absolute positioning:

.link-btn-a{
   text-decoration: none !important;
   color: white;
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
}

.link-btn-td{
   background: #FFA06A;
   text-align: center;
   position: relative;
}

Answer №4

Hey there! Here is a code snippet where I demonstrate the proper use of the position property in CSS.

 .button-link{
    text-decoration:none !important;
    color:white;
  }

  .button-td{
    position: relative;
    background:#FFA06A;
    text-align:center;
  }
  a {
    position: absolute;
    top: 190px;
    left: 250px;
  }

Answer №5

To easily center items both horizontally and vertically, you can utilize the "display: flex" property and then stack the items on top of each other.

Not only is this method responsive across various screen widths and scenarios.

<!DOCTYPE html>
<html>
  <head>
    <title>Image Centering</title>
    <style type="text/css">
      #container {
        border: 1px solid black;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      img {
        margin: 10px;
        position: relative;
      }
      a {
        display: flex;
        border: 1px solid black;
        position: absolute;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <img
        src="https://images.unsplash.com/photo-1598257006626-48b0c252070d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;ixlib=rb-1.2.1"
        style="max-width: 600px"
        alt="photo-1598257006626-48b0c252070d?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;ixlib=rb-1.2.1"
      />
      <a class="button-a sans text-white" href="*|ARCHIVE3|*">
        <span class="button-link">READ MORE</span>
      </a>
    </div>
  </body>
</html>

This method should function perfectly.

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

Using Vuetify to display text fields in a single row

Check out this Vue component template (View it on CODEPEN): <div class="some-class"> <v-form > <v-container @click="someMethod()"> <v-row> <v-col cols="3" sm="3" v-for="p in placeholders" ...

What is the equal tr of CSS?

Is there a way to separate the menu bar from the body within a div, so that everything after 'contact' appears below it? Is there a specific code or command similar to a newline that can achieve this? Your help is greatly appreciated :) Thank you ...

Footer alignment issue when using react-full-page is causing it to not lineup at the bottom of the page

Currently, I have integrated the react-full-page package into my project. However, after adding the Footer to the routes, I noticed that the footer is only visible in the second section of the page and does not appear at the bottom as expected. If you wan ...

Issues with the background-image scroll feature not functioning as intended

Can anyone help me out with a CSS issue I'm having? I've set an image as my background on a webpage and I want it to scroll with the page. Even though I followed some online tutorials, it's still not working for me. It's been a while si ...

What steps should be taken to ensure that data can be transmitted from the input without allowing the user to edit the ID field?

Here is the sample HTML: <form action="/app02/user_edit-{{obj.id}}/" method="post" > <input type="text" name="id" value="{{obj.id}}" disabled> <input type="text" name="username" value="{{obj.username}}"> <i ...

Guide to making a sidebar navigation menu on the left using Bootstrap

How can I create a left navbar using Bootstrap? I managed to do it. <nav class="btn-group-vertical float-left navbar"> <button routerLink="product/home" routerLinkActive="is-active" class="btn btn-outline-dark">Home</button> ...

Is it possible for amCharts to show the data properly?

Starting out with amCharts and javascript can be a bit overwhelming. Here is the structure of my html file: <!DOCTYPE html> <html> <head> <link rel="shortcut icon" href=""> <title>chart created with amCharts | amChar ...

The card-group is off-center within the column

I am facing a challenge with the alignment of 3 cards within a card-group that are contained in a div with the col class, which is wrapped by a div with the row class, all within a div with the container class. Currently, the cards are not centered. I hav ...

Switching PHP include on an HTML page using JavaScript

I've been attempting to modify the content of the div with the ID "panel_alumno" using a JavaScript function that triggers when a button is clicked. My goal is to display a different table each time the button is pressed, but so far, I haven't be ...

What location is most ideal for incorporating JavaScript/Ajax within a document?

Sorry for the seemingly simple question, but I would appreciate some clarification from the experts. When it comes to the three options of placing JavaScript - head, $(document).ready, or body, which would be the optimal location for ajax that heavily rel ...

Troubleshooting: CSS3 transform issue unresolved

I'm attempting to add a 10-degree rotation to my menu items. While this CSS effect works in Firefox, I can't seem to get it to work in Chrome and Safari. I already know that IE doesn't support this particular CSS3 property, so that's no ...

The one-click button is functional on larger screens, while on mobile devices, only a double click will register

I am facing an issue in angular where a button works perfectly fine with one click on larger screens, such as Macbook. However, on iPhone, it requires a double click to function properly (it works fine on Android too). The alert triggers with a single cl ...

Click on the print icon in the modal window

I have been working on a receipt generator for a client. The client can add payment receipts using the "Add" button, and upon submission, they have the option to convert it to PDF or print it. However, there seems to be an issue with printing as the text f ...

Choosing from a variety of tr elements

I'm working with a table in HTML that has about 100 rows. I would like to apply different colors to specific groups of rows, such as making rows 1-10 red and rows 20-40 blue. Using nth-child seems like an option, but it can get quite verbose if I nee ...

Utilize Django's collapse feature in a loop to streamline your Bootstrap integration

As I navigate through the event_list, I aim to showcase details for each entry. However, my html only exhibits information for the initial entry regardless of which one I select. For instance, if I click on green event, it triggers the display of the my we ...

What HTML element can be used outside of a Bootstrap dropdown item to show a default image when the dropdown is open?

I'm working on my menu where I have two columns - one for submenu text and the other for respective images. Everything is working fine so far. However, I want to set a default image that will display when the user does not hover over any dropdown item ...

Adjust the dimensions of the initial cell

I need to adjust the size of the initial "generated" cell in a grid. The grid is not present in the HTML markup until JavaScript prints RSS information on it, making it difficult to target specific rows or cells directly. Note: The first element is hidden ...

Title Frame and Interactive Sidebar Content

Hello, I am a newcomer in the world of WordPress website design and I have been struggling with a minor CSS issue on my site (www.dimjaa.org) for the past couple of days. Despite my efforts, I have been unable to find a solution on my own. I am reaching ou ...

Using line breaks in JSX

In my application, I save all the text in a settings.ts file. However, I am facing an issue where \n does not seem to add line breaks as expected. Are there any alternative methods to include breaklines in my text? My tech stack includes Next.js, Reac ...

What is the best method to retrieve child elements from a class list object?

Seems like I have a similar content class <div class="parentclass"> <div class="childClass"> </div> <div class="childClass"> </div> <div class="childClass"> </d ...