tags:

views:

23

answers:

2

For example:

<caption>My Table <span>My Table Span</span> </caption>

I want to align the caption to the left and it's span to the right:

caption { text-align:left; }
caption span { text-align:right; }

That will not work. Is it possible to do what I'm trying to do?

+2  A: 

You could try using floats:

<table>
    <caption>My Table <span>My Table Span</span> </caption>
    <tr>
        <td>
            Cell 1
        </td>
    </tr>
</table>

CSS:

caption span { float:right; }

Just keep in mind this could wreak havoc on your elements below these in the rendered page. You may need to apply clear:both to the next element in the html.

Here's a basic example: http://jsfiddle.net/3PVnb/2/

You'll notice though that for some reason IE and Firefox render the caption slightly differently so this probably needs to be tweaked. Both do float the span on the right, but IE7 has a line break.

KP
Dammit, you beat me to it. ^^ Does `float` work on in-line elements, or on block elements only, or both? Also, you can also try clearing the element above it if the floats end up too high.
gablin
This doesn't quite work, as gablin suggests <caption> needs to be a block element. The following works: `caption { display:block; } span { float:right; }` [Demo 1](http://jsfiddle.net/K7TTn/1/). But for some reason trying to select only <span> tags contained inside a <caption> tag doesn't work: `caption { display:block; } caption span { float:right; }` [Demo 2](http://jsfiddle.net/K7TTn/2/)
irishbuzz
@irishbuzz: I think part of the issue with your demos is you don't include a surrounding `<table>` tag. `<caption>` must only be 1st element of `<table>`. Without that it causes issues in some browsers?
KP
@KP - right you are. [This works perfectly](http://jsfiddle.net/K7TTn/4/)
irishbuzz
A: 

Since you do not appear to have widths defined, you might just try a little padding on the span:

caption span {
margin-left: 5em;
}
Traingamer