Align the text in the center and the image to the right within a table cell <td>

Is there a way to keep text centered in a cell while right aligning an image within the same cell? When I use float:right on the image, it displays correctly but pushes the text to the left by the width of the image. Any solutions?

<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>

Answer №1

To incorporate relative position to the td element and absolute position to the image, you can set the right attribute to 0 on the image itself.

<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>

Furthermore, if you wish for your table to occupy 100% of the width, it is essential to apply the style to the entire table instead of individual cells.

Keep in mind that text may end up underneath the image. To prevent this, you can position the text above the image or utilize a slightly transparent image.

Answer №2

To create a responsive design for your text and image, wrap them in a div with the assigned flex property. You can then utilize 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>

Enhancement

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>

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 process for attaching a dynamically generated object to the document's readiness event?

One of my tasks involves dynamically generating a slider element using the code snippet below: function NewDiv(){ for (var count=0; count < parseFloat(sessvars.storey_count); count++) { var string="<br><div ...

What is the best way to extract text from a list item in an unordered list (

I am trying to search a ul list that populates a ddSlick dropdown box. ddSlick adds pictures to list items, making it a visually appealing choice. To see more about ddSlick, you can visit their website here: Here is the code I am using to loop through the ...

How can I customize the color of the title and property value in Handlebars?

Here is the list I am working with: <li>Equipment Area:{{equipment_area}}</li> <li>Equipment Type: {{equipment_type}}</li> <li>Manufacturer: {{manufacturer}}</li> <li>Model Number: {{model_number}}</li> < ...

Adding text on top of an image

I am having trouble with the master slider plugin as slide1, 2, and 3 each have unique classes. I attempted to overlay text on the image but it is not showing up. I tried using the following CSS code but nothing appeared on the screen. .classnameslide1: ...

Determine the width of the window and adjust the positioning of jQuery UI tooltips accordingly

Struggling to adjust the jQuery UI tooltip position based on screen width, but can't seem to figure it out. Can someone assist me in detecting the browser's width and changing the tooltip position accordingly? [fiddle] $(function () { $(doc ...

jQuery Counter Effect encountering isNaN error when trying to display a number retrieved from a get API object

I am looking to display the numerical data retrieved from an API using JSON. I want to incorporate a counter effect that displays "isNaN" if necessary. The API URL returns an object with the total number saved in data.data. Can anyone assist me with achi ...

The margin set in Bootstrap isn't making any difference

I'm currently utilizing bootstrap 4 in my react project, and while the bootstrap components seem to be functional, I am facing an issue where setting margins does not work as expected. Here is a snippet of code from one of my pages: return ( < ...

How to access and display nested JSON array items within an Angular 5 template

How can I display the value of line_donation from the meta_data array in this JSON data on my template? Here is the JSON data retrieved using ngFor: "line_items": [ { "id": 1003, "name": "Fresh Mango- 1 kg", ...

Is it possible to utilize AngularJS ngAnimate to perform cross-fading transitions on list items?

I am currently exploring the ins and outs of angularJS with my simple message ticker example. This ticker displays messages by toggling the CSS display property of one of the li elements. <div id="ngtickerMessage" class="ngtickerMessage"> ...

How can I start or stop an animation by simply pressing and releasing any key?

I'm struggling to make this cool "rotating ball" animation start and stop based on any key being held down. The animation should only play while a key is actively pressed. I've done some research, but I keep coming across jQuery solutions that I ...

Navigating through dropdown browser menus using VBA

I am looking to automate the process of looping through a dropdown list on a specific website and downloading the latest file for each entry. I have successfully opened the website and clicked the "submit" button, but I am struggling to find a solution to ...

Using Angular 2 to toggle the visibility of a component when hovering over and moving away from it

I have developed a custom filtering component in Angular 2, which I have integrated into the table header. My goal is to show the filter when the mouse hovers over a specific span, and hide it when the mouse moves away. <th> <span class="headCol ...

Add-on Group for Form Inputs - Is a style element missing? (Bootstrap 4)

I am looking for a way to incorporate a Bootstrap4 input group label between two input fields. While there are "prepend" and "append" styles available, in this scenario, I require a "center" style which seems to be missing. Does anyone have a solution for ...

I'm working with Angular 12, Bootstrap 5, and ngPrime, and I'm looking to overlap a p-dialog with another element in my project

Is there a way in Angular 12 with Bootstrap 5 using ngPrime to overlap a p-dialog over any other element without using z-index and CSS, solely relying on PrimeNG? I have tried using z-index with: .my-class{ z-index: 2147483647 !important; } However, ...

The styling of JSX input elements is not performing as anticipated

I am facing an issue with a component that is responsible for mapping input elements. These input elements have a flag indicating whether they should be displayed inline or not. Despite my efforts, I am struggling to make the flex property work as intended ...

A method for expanding the menu upwards to make room for newly added items

Looking at the images below, I have a menu that needs new items added to it while maintaining the position of the lower-left corner. Essentially, each time an entry is added, the menu should expand upwards from "the upper-left corner" while keepi ...

Show the content of a div inside a tooltip message box in an MVC application

I have a MVC application where I need to show tooltip messages when hovering over my HTML div. HTML: <li class="Limenu" data-placement="right"> <span class="LessMenuspan btn-primary" pid="@i.ConsumerNo_" onmouseover="FacebookprofileTip(1, 0,thi ...

Encountering a 500 Internal Server Error while attempting to insert data into a MYSQL database using

I am experiencing an issue while trying to insert radio checked data values into a MySQL database using PHP, jQuery, and AJAX. Whenever I attempt to insert the values, I encounter an internal server 500 error... Any assistance would be greatly appreciated ...

Working with HTML5 Canvas to Clip Images

Is there a way to implement a tileset image in canvas like this one? I am trying to figure out how to make it so that clicking on the first tile returns 0, clicking on the tenth tile returns 9, and so on... Any suggestions on how to clip a tileset on an ...

The webpage appears to be operating smoothly when tested locally and on JSFiddle, however it is encountering

Just joined and brand new to this game. Completed a course at work over the last few weeks, and I must say, I'm really enjoying it. However, when I tried to create a page using Bootstrap styling and added an accordion and a carousel, both features fai ...