6

Currently having an issue with my button. I want to be able to center my 'a' tag but at the moment it will only stick to the left side. I have tried using "display:block" but this will make my button take up the full width of any div it's been put in.

HTML:

<a href="#" class="button blue">Apply Now</a>

CSS:

.button {
    padding:1em;
    text-align: center;
    display:inline-block;
    text-decoration: none !important;
    margin:0 auto;

    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -ms-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}
user513951
  • 12,445
  • 7
  • 65
  • 82
Nazar Abubaker
  • 495
  • 3
  • 7
  • 17
  • 1
    Apply css on div for center-align and give width as required – rahul Feb 06 '17 at 10:23
  • text-align center should be applied to the parent element. @MHamzaJaved, that's some terrible advice - first one would make no difference, 2nd one, would start to make your css a maintenance nightmare – Pete Feb 06 '17 at 10:25
  • 2
    You need to set `text-align: center;` on the surrounding container. This is not a very useful question as this obviously lacks the most basic efforts of research by OP. – connexo Feb 06 '17 at 10:25

6 Answers6

20

Use a div to center the link

.button {
    padding:1em;
    text-align: center;
    display:inline-block;
    text-decoration: none !important;
    margin:0 auto;

    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -ms-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}

.container{
  width: 100%;
  text-align: center;
}
<div class="container">
  <a href="#" class="button blue">Apply Now</a>
</div>
Pablo Salcedo T.
  • 874
  • 1
  • 16
  • 27
4

Use the text-align: center; on a parent element to center align all the child elements inside it. They(child elements) do not have to be block level elements for the same. Your question has the concern that you do not want to take up the full space of the parent div by using display:block on your a tag.

You don't have to, even if you specify display:inline-block on your a tag and wrap it inside a parent with text-align: center;, it will solve your task.

Alternatively, you can use margin-left:25% or so, in case the above answer does not suit your need.

Feel free to drop in the code in case you need more help on this.

Thanks,
Yaman

YAMAN
  • 1,008
  • 11
  • 15
3

I agree with Yaman, the easiest way I used is a < p > tag, just put it before and after your link

<p align="center">
    <a href="#" class="button blue">Apply Now</a>
</p>
0

the issue is simple. Just add a paragraph tag before the button and inside the tag, add the alignment. This should work

<p align="center">
              <a href="Student Voice\Student-Voice (Hello).html" title="Just A button" class="btn btn-default">Hi</a>
0

If text-align: center does not work than You can add a style tag in the respective tag and use

justify-content: center;

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '22 at 10:36
-1
  a{
      display: block;
      text-align: center;
     }