views:

112

answers:

6

I am attempting to make two elements on different sides of their container. In my actual code these two elements are to be on opposite sides of a div, but for the sake of example, lets say I want them on opposite sides of the browser window.

So I did a simple

<html>
<head>
</head>
<body>
<table>
<tr>
<td style="width: 50%;text-align: left;">
This should go left
</td>
<td style="width: 50%;text-align: right;">
This should go right
</td>
</tr>
</table>
</body>
</html>

example at: http://jsbin.com/ocete/

Well, I'm not entirely for sure how to do this very well with divs even. Also in the right aligned table cell, there will be two elements in my actual code. One is an image and one is a piece of text. I want them to be on opposite sides of the <td> in which they are contained.

How can I do this the way I want? I am not seeing any straight forward way. (and please do not recommend fixed positioning)

+5  A: 

To your <table> element, add:

style="width: 100%"

Everything is working perfectly, your table just isn't big enough to see it.

jvenema
Yup. I feel dumb. lol
Earlz
+1, indeed. And turning the border on would have made the OP realise straight away what was going on.
Wim Hollebrandse
Set also `table-layout: fixed` to tell the browser you really mean it so that it doesn't “helpfully” change the cell widths for you. (This also renders faster.)
bobince
+1  A: 

You don't have a width specified for your table, so it is only as wide as it needs to be, therefore you can't see the text alignment making any difference.

Try this

<table style="width: 100%">

and take another look.

nickf
gah, I was not quick enough.
Agos
A: 

Any reason to use a table for the layout? Use two divs instead, put their width to 50% each, and float the second div to the right.

Konrad Rudolph
Add a 3rd cell to that table. Pretty trivial right? Not so trivial with floating divs.
Earlz
@earlz: well, depends. The HTML code is trivial. The CSS code is only *slightly* more complex – just add appropriate margins to the `div`s. True problems arise when you want to make all `div`s the same height, regardless of their content height – but (pure CSS) solutions exist for that, as well.
Konrad Rudolph
A: 

Add a width to your table..

<table style="width: 100%">

Edit: tooo Slow

Doc Snuggles
+1  A: 

If you're looking to put an image side by side with text on separate sides of a table you can use the following:

<table style="width: 100%">
<tr>
<td style="text-alight: left;">
text
</td>
<td style="text-align: right;">
  <table style="width: 100%">
    <tr>
      <td style="text-align: left;">image</td>
      <td style="text-align: right;">text</td>
    </tr>
  </table>
</td>
</tr>
</table>

That should get you something like

-----------------------------
|text    |image    |    text|
-----------------------------
Brian Hasden
A: 

Are you always going to be using a table? If so the above answers will work. But if you are using two divs within a container div, you need to float the inside divs to the left and right respectively and then clear your floats in the container div.

ryanulit