Black
Black

Reputation: 20232

Align icon to the right in a table cell?

I have a simple table cell which contains a text and an icon. I want to align the text on the left and the icon on the right. I tried it like this, but it does not work. The only solution i know would be to create another td where i put the icon inside. But i want both, text and icon in one td

    table {
        border: 1px solid red;
    }

    td {
      border: 1px solid black;
      width: 200px;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <table>
    <tr>
    <td>Text <span align="right"><i class="fa fa-toggle-on"></i></span></td>
      <td>Test</td>
    </tr>
    </table>

Upvotes: 13

Views: 57211

Answers (6)

mithu
mithu

Reputation: 247

    table {
        border: 1px solid red;
    }

    td {
      border: 1px solid black;
      width: 200px;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <table>
    <tr>
    <td>Text <span ><i class="fa fa-toggle-on pull-right"></i></span></td>
      <td>Test</td>
    </tr>
    </table>

this is the best way i think...

Upvotes: 3

Prashant
Prashant

Reputation: 385

Try This:

<style>
/*If not using bootstrap add this to you css*/
 .pull.right{
   float:right;
 }


 <table width="100%">
  <tr>
  <td>Text <span><i class="fa fa-toggle-on pull-right">      </i></span></td>
  <td>Test</td>
 </tr>
 </table>

Upvotes: 1

ketan
ketan

Reputation: 19341

Just add will make your icon to right side.

span {
    float: right;
}

Your updated example:

table {
    border: 1px solid red;
}

td {
  border: 1px solid black;
  width: 200px;
}

td > span {
    float: right;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<table>
<tr>
<td>Text <span><i class="fa fa-toggle-on"></i></span></td>
  <td>Test</td>
</tr>
</table>

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172428

Try to use

style="float:right;"

like this:

table {
    border: 1px solid red;
}

td {
  border: 1px solid black;
  width: 200px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<table>
<tr>
<td>Text <span style="float:right;"><i class="fa fa-toggle-on"></i></span></td>
  <td>Test</td>
</tr>
</table>

Also as webeno has correctly pointed out in comments, the align attribute has been deprecated now. So try to avoid it.

Upvotes: 25

PHPExpert
PHPExpert

Reputation: 943

add one more class in your css

table i.fa{
    float: right;
}

Upvotes: 1

If you want them in same td(not adding another for icon) you can try this:

<td>
  <div style="float:left;width:50%;">I stay at left</div>
  <div style="float:right;width:50%;">And I stay at right</div>
</td>

Upvotes: 3

Related Questions