Solution 1 :

You can add position relative to td and position absolute to image and then, set righ=0 in the image

<table style="width:100%">
  <tr>
   <td style="text-align:center;position:relative">    
    text
     <img src="~/images/pencil1.png" height="25" width="25" style="position:absolute;right:0" />
   </td>
 </tr>
</table>

Also, if you want your table become 100%, you must set the style in the table, not in the cell.

Take in mind that your text may be under the image. You can put the text over the image if you want avoid this or use a semitransparent image.

Solution 2 :

I would wrap your text and your image with a div which assigned flex. Then you can use flex-grow: 1;.

td {
  width:50%;  
}
.tdflex {      
  display: flex;  
}

.text {
  flex-grow: 1;  
  text-align: center;
}
<table border>
  <tr>
   <td>
     <div class="tdflex">
       <div class="text">text</div>
       <div>
         <img src="https://via.placeholder.com/500" height="25" width="25" />
       </div>
     </div>
   </td>
 </tr>
</table>

Update

td {
  width:200px; 
  height: 50px;
}
.tdpos {        
  position: relative; 
}

.tdpos img {
  position: absolute;
  top:0;
  right:0;
}
.text {
  margin-top: -24px;    
  background: gray;
  text-align: center;  
}
<table border>
  <tr>
   <td>
     <div class="tdpos">
       <div class="text">text</div>
       
         <img src="https://via.placeholder.com/500" height="50" width="50" />
       
     </div>
   </td>
 </tr>
</table>

Problem :

I would like to right align an image in a cell AND still keep the text centered in the cell. I am using a float:right on the image which displays it correctly, however it pushes the text to the left the width of the image so it is no longer centered. Is there a way to keep the text centered?

<table>
  <tr>
   <td style="text-align:center" width:"100">text
     <img src="~/images/pencil1.png" height="25" width="25" style="float: right;" />
   </td>
 </tr>
</table>

Comments

Comment posted by Paulie_D

No, even with float the image still takes up space to you might want to re-think your strucuture

Comment posted by Rupesh Jain

You will have to absolutely position your image if you want your text to be aligned to the center. However, your structure is not correct as @paulie_d has pointed out.

Comment posted by Maik Lowrey

Your solution with position is the clean solution. Therefore a +1Up from me. Thanks for the comments.Merci!

Comment posted by Victor

Here the text is not centered

Comment posted by Maik Lowrey

@Victor Thank you. You are right! I updated. Now the text is aligned. Merci!

Comment posted by Victor

You are welcome. I think that the question say that text must be centered in the whole cell, not in the free space after put image at right.

Comment posted by Maik Lowrey

@Victor that is unfortunately not clear. From UX view it would be horrible if that expected. Or not?

Comment posted by Victor

The code of the question (apart of width problem of the cell) have the image floated at right and the text centered in the remain space. So I think that it’s requesting to do that. I don’t know why and I warning about that problems in my response, but I don’t know all the details either. Maybe has a sense

By