Search code examples
htmlcsscss-tables

How to change position of a specfic <td> element inside a HTML table


So, I have this table in which I need to place the td that contains the profile info further to the right as following:

Example

this is my HTML:

<table>
        <tr>
           <td>
                <asp:Label ID="****" runat="server" CssClass="FieldLabelRqrd" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:Label ID="***" runat="server" Text="ID=****"></asp:Label>
            </td>

            <td colspan="4">
                <div id="cssProfile">
                    <asp:Label ID="lblProfile" runat="server" Text="Profile:" CssClass="lblProfile"></asp:Label>                         
                    <asp:HyperLink ID="hlGoProfile" runat="server" ForeColor="blue" Target="_blank" CssClass="hlGoProfile" Text="Go"></asp:HyperLink>
                </div>
            </td>
         </tr>
     </table>

I tried colspan and rowspan but this does no work and here's the CSS

#cssProfile{    
    display:flex;
    justify-content: start-flex;}

.lblProfile{    
margin-left: auto;}

.hlGoProfile{   
margin-left: auto;}

Solution

  • Give your table 100% width and add text-align: right to the cell in question.

    BTW, you could remove that colspan, if you don't need it for any other purpose.

    table {
      width: 100%;
    }
    #cssProfile {
      border: 1px solid red;
      text-align: right;
    }
    <table>
      <tr>
        <td>
          <asp:Label ID="****" runat="server" CssClass="FieldLabelRqrd" Text="Name">A</asp:Label>
        </td>
        <td>
          <asp:Label ID="***" runat="server" Text="ID=****">B</asp:Label>
        </td>
    
        <td colspan="4">
          <div id="cssProfile">
            <asp:Label ID="lblProfile" runat="server" Text="Profile:" CssClass="lblProfile">C</asp:Label>
            <asp:HyperLink ID="hlGoProfile" runat="server" ForeColor="blue" Target="_blank" CssClass="hlGoProfile" Text="Go">D</asp:HyperLink>
          </div>
        </td>
      </tr>
    </table>