tags:

views:

126

answers:

5

I have a parent div id=A which has a width of 100%. I want that all the elements of the div A should be placed to the right.

So I added another div id=B inside A and did a text-align=right on the div A. The width of B is 600px.

However the controls appear left aligned in A. Any suggestions?

+4  A: 

You should do a float: right on the div B

Guillaume Flandre
Thanks a lot for the quick answer
ScG
Additional info: Check out the CSS display attribute. A DIV is, by default, a block element, so will not follow the rules of text-align, only inline elements will follow this.
Steve H
If you want "that all the elements of the div A should be placed to the right", take a look at my answer (#A *{float:right})
marcgg
+1  A: 

Just go :

#A * {
  text-align: right;
}

If you want the actual div to be right align and not just the text, use float:right instead.

#A *{
 float:right;
}

You might need to specify a width for #B. If you don't want to do that here's a solution:

#B{display:inline-block;}
marcgg
The width of B is set (cf; question). And inline-block won't work on IE if it's applied to a div element.
Guillaume Flandre
inline-block is not a real option... besides, div's are already "block" elements. You can specify a width already.
Timothy Khouri
if you don't want to set the width, inline block is nice. the OP said that he wanted "all the elements" floating right, hence the idea of using inline-block
marcgg
Then B should be a span, that way it would work.
Guillaume Flandre
A: 

Do you want this:

<style type="text/css">
body {
 direction:rtl;
}
</style>
<h3>Welcome to the real-time HTML editor!</h3>
<p>Type HTML in the textarea above, and it will magically appear in the frame below.</p>
Nimbuz
I doubt that this is what the OP is looking for
Carson Myers
ok, a comment was enough in that case, downvote was unnecessary.
Nimbuz
it's nothing personal, I just didn't think this answer was helpful to the question. In any case, I added a space to your question so I could remove it in case someone lands on this page and decides to go with your answer
Carson Myers
A: 

Works for me... ;)

<div id="a" style="width:100%; text-align:right; border: 1px solid blue">
    <div id="b" style="width:600px; border:1px solid red">
        hi
    </div>
</div>
Ehrann Mehdan
In IE 5 maybe :P ... a "DIV" is a "Block" element (by default), so "text-align" ***shouldn't*** move it to the right. So, all you've done is found a way to move it to the right in some crappy old browsers :)
Timothy Khouri
Works in IE 8...
Ehrann Mehdan
A: 

Just specify the width you want and make the margin-right: 0 and margin-left: auto

<div id="a">
<div id="b" style="width:600px; margin-right: 0; margin-left: auto;">
    If ID:A has a width of say 1000 then ID:B will have a left margin of 400px
</div>
</div>

or if ID:A is already inside a div anyway, you just need this:

<div id="a" style="width:600px; margin-right: 0; margin-left: auto;">
        If ID:A's wrapper has a width of say 1000 then ID:A 
        will have a left margin of 400px
</div>

The total width of a block element inside a block element is always the width of the container anyway.

Gazzer