Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a link in CSS?

I have tried:

  • text-align: center;
  • margin: auto;
  • margin-left: auto;
  • margin-right: auto;

This is my code:

HTML:

<a id="final" href="sooce2.html"></a>

CSS:

#final {
   text-align: center;
}
like image 803
Conlan Ryan Avatar asked Jan 20 '26 15:01

Conlan Ryan


2 Answers

anchor tags are an inline level element, therefore the things you have tried will not work.

either setting a { display: block; } with text-align: center;

or applying text-align: center; to its parent

.center-text {
  text-align: center;
}

a.center-text {
  display: block;
}
<div class="center-text">
<a href="#">Link goes here</a>
</div>


<a href="#" class="center-text">Link goes here</a>
like image 151
Tyler Fowle Avatar answered Jan 23 '26 03:01

Tyler Fowle


By default a anchor elemenet is inline element and does not have defined width .. so you can make text inside centered if you either spefify it's width explicitly like XX px, or like my example where ) make it block element, and block elements default to be full width of it's parent.

a{
  display: block;
  text-align: center;
}
<a href="#">My link</a>
like image 37
Kresimir Pendic Avatar answered Jan 23 '26 03:01

Kresimir Pendic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!