Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center an <a> element

Tags:

css

I have an element in my header that is getting data from a js file.

I'm trying to center that element in my page but all i do dosent seem to work.

I tried margin-left: auto and margin-right: auto but still is not centering.

The element code is:

<a href="http://[ip]/tunein/tranceilfm.pls" id="cc_strinfo_song_tranceilfm" class="cc_streaminfo" style="margin: 0px; display: block; ">Loading...</a>

http://tranceil.fm

(the blue song info in the frame below the menu)

Any thoughts?

like image 900
Trance84 Avatar asked Aug 04 '12 19:08

Trance84


People also ask

How do you center an element?

To just center the text inside an element, use text-align: center; This text is centered.

How do you center an A in a div?

Using CSS, you can center text in a div in multiple ways. The most common way is to use the text-align property to center text horizontally. Another way is to use the line-height and vertical-align properties. The last way exclusively applies to flex items and requires the justify-content and align-items properties.

How do I center an element in HTML?

Applying text-align :center to a <div> or <p> element centers the contents of those elements while leaving their overall dimensions unchanged.

How do I center an element in CSS?

Like last time, you must know the width and height of the element you want to center. Set the position property of the parent element to relative . Then set the child's position property to absolute , top to 50% , and left to 50% . This just centers the top left corner of the child element vertically and horizontally.


1 Answers

This should do it (center the link using text-align: center in the parent container):

<div style="margin: 0 auto; text-align: center">
    <a href="http://94.23.250.14:2199/tunein/tranceilfm.pls" 
    id="cc_strinfo_song_tranceilfm" class="cc_streaminfo" 
    style="margin: 0px; display:block;">Loading...</a>
</div>

Or simply add text-align: center to the element itself:

    <a href="http://94.23.250.14:2199/tunein/tranceilfm.pls" 
    id="cc_strinfo_song_tranceilfm" class="cc_streaminfo" 
    style="margin: 0 auto; display:block; text-align: center">Loading...</a>

<!-- text-align: center directly on element -->
<a href="http://94.23.250.14:2199/tunein/tranceilfm.pls" id="cc_strinfo_song_tranceilfm" class="cc_streaminfo" style="margin: 0 auto; display:block; text-align: center">Loading...</a>

<!-- text-align: center on parent container -->
<div style="margin: 0 auto; text-align: center">
  <a href="http://94.23.250.14:2199/tunein/tranceilfm.pls" id="cc_strinfo_song_tranceilfm" class="cc_streaminfo" style="margin: 0px; display:block;">Loading...</a>
</div>

Fiddle

like image 113
Chris Clower Avatar answered Oct 13 '22 11:10

Chris Clower