views:

204

answers:

4

The problem:

We have a page that ordinarily has two elements arranged side-by-side.

<div id="container">
  <div id="element1">content</div><div id="element2">content</div>
</div>

But in some conditions only element2 is on the page e.g.:

<div id="container">
 <div id="element2">content</div>
</div>

Despite exploring a few angles for this, we can't seem to make it work. We're not averse to using JS, it just feels that a css based solution ought to be possible.

Can you think of a way of using just css(and possibly extra markup if definitely necessary) to make element 2 center when it appears on its own?

+5  A: 

There is a pure css solution, however it won't work in versions of IE less than 7 because it won't understand the sibling selector (+), for that you may want to consider a JavaScript solution (perhaps Dean Edwards' IE7). Anyway, some example css:

div#element2{
  width:100px;
  margin:0 auto;
}
div#element1{
  width:50px;
  float:left;
}
div#element1 + div#element2{
  width:50px;
  float:left;
  margin:0;
}

The key is the line div#element1 + div#element2 which selects div#element2 given that it directly follows div#element1.

Phil
Ah, great thinking! This might well be the closest solution with just css. Clever use of the pseudo selector I like it.
Sam Murray-Sutton
A: 

A strict CSS2 solution:

#container {
    text-align:center;
}
#element1, #element2 {
    display:inline-block;
}

The inner elements should layout like inline text inside #container, but remain blocks inside.

This is standard CSS, but getting browser support might take some trickery.

mrclay
A: 

it's not cool solution becouse tables are not "trendy" anymore but it solves the problem completly (under all ie)

<style>
    #container {
        margin:0 auto;
        width:100px;
    }

    #container table{
        width: 100%;
        text-align:center;
    }

    #element1{ 
        background-color:#0000ff;
    }

    #element2 {
        background-color: #ff0000;
    }
</style>

<div id=container>
    <table cellspacing=0 cellpadding=0>
        <tr>
            <td id="element1">content</td>
            <td id="element2">content</td>
        </tr>
    </table>
</div>
+2  A: 

I think Phil was on the right track, but you should try using the CSS last-child pseudo-class. As far as I know, first-child and last-child are the only way in CSS to approximate an if construct.

div#container div#element2:last-child {
    width:100px;
    margin:0 auto;
}

div#element1{
    width:50px;
    float:left;
}

div##element2{
    width:50px;
    float:left;
    margin:0;
}

The CSS above basically says "if element2 is the last child element of its parent use this set of styles, otherwise use these other styles.

This should even work in IE7.

Bill the Lizard
My method should work in IE7 too, however both methods fail in IE6. It's a matter of personal preference which one is chosen, but good alternative.
Phil
D'oh! I stopped testing for IE6. *embarassed* :)
Bill the Lizard
Actually, I just looked this up, and last-child is from CSS2. It should work in IE6. What version of IE are they on now anyway? Can someone check? I should post this as a question on... oh, yeah.
Bill the Lizard
There are hardly any pseudoclasses that work in IE6! We're lucky we can do hover styles on links (you can't on any other element)!The current released version of IE is 7 and 8 is in beta 2, however, there are still a fair few IE6 users out there refusing to change.
Phil
Hey bill, thanks for the suggestion, I think it's a pretty good way to tackle this question too. Regarding compatibility, I think you'll find IE6 only supports :link :hover :visited and :active.
Sam Murray-Sutton