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?
To just center the text inside an element, use text-align: center; This text is centered.
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.
Applying text-align :center to a <div> or <p> element centers the contents of those elements while leaving their overall dimensions unchanged.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With