Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align text center and right

Tags:

html

css

I am 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 the correct way or is there any other way to handle this scenario.

Thanks

like image 648
Mayur Avatar asked Jun 09 '10 06:06

Mayur


People also ask

How do I align text to the center in HTML?

One way to center text or put it in the middle of the page is to enclose it within <center></center> tags. Inserting this text within HTML code would yield the following result: Center this text!


2 Answers

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>
like image 190
Dan Heberden Avatar answered Oct 06 '22 14:10

Dan Heberden


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>
like image 39
superUntitled Avatar answered Oct 06 '22 14:10

superUntitled