tags:

views:

58

answers:

3

Hi All,

I m facing problem while aligning two text, one in center and other text in right;

I used a Div to align it:

<div style="text-align:center">
  <h1> Sample Heading</h1>

    <div style="float:right; text-align:center">

         <a href="#">sample link</a> 

    </div>
</div>

When i used this my heading comes left its not a centrally align properly please tell is this correct way or their any other to handle this scenario

Thanks

+1  A: 

You do not need to use div's to do this, you can style the elements themselves. The < a > tag by default does not understand text-align, as it is an inline element, so I have placed the link in a paragraph, which accepts text-align. I have placed the two lines in a < div > tag with a small width so it is easy to see what is going on.

<div style="width:400px;">
  <h1 style="text-align:center"> Sample Heading</h1>
<p  style="text-align:right"><a href="#">sample link</a> </p>
</div>
superUntitled
but if he wants the elements on one line...
Dan Heberden
if he wants the elements on one line, then he could float the h1 element. <h1 style="text-align:center;float:left"> Sample Heading</h1>
superUntitled
@superUntitled: No, you should really float the link to the right. Your main page content should be static.
animuson
A: 

If you want the item to not mess with the layout, try absolute:

<div id="header" style="position:relative;text-align:center;"><!-- define relative so child absolute's are based on this elements origin -->
    <div id="sampleLink" style="position:absolute; top:0px; right:0px; >Link</div>
    <h1 style="text-align:center;">Heading</h1>
</div>
Dan Heberden
Talk about making it way more complicated than it needs to be... What was wrong with floating it to the right? At least then overflow text won't go behind the link.
animuson
because he's working with a centered element. http://jsfiddle.net/RYq4F/ - is why. A floated element still affects text..
Dan Heberden
Thanks its working thanks a lot
Mayur
A: 

It works fine to me. But if you have some issues with positioning of h1, try make it block: h1 { display: block; }. On other hand, if you want to display h1 and a at the same line, you just have to put right-aligned a before h1.

tambourine