Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center <a> tag in div

Tags:

html

css

button

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;
}
like image 612
Nazar Abubaker Avatar asked Feb 06 '17 10:02

Nazar Abubaker


People also ask

How do you center an a tag in a div?

Use the text-align: center; on a parent element to center align all the child elements inside it.

How do you center a tag in HTML?

HTML <center> TagThe <center> tag in HTML is used to set the alignment of text into the center. This tag is not supported in HTML5. CSS's property is used to set the alignment of the element instead of the center tag in HTML5. Attributes: This tag does not accept any attribute.

How do I center a div tag vertically?

you need to set the outer div to be displayed as a table and the inner div to be displayed as a table-cell — which can then be vertically centered. For Internet Explorer, you need to position the inner div absolutely within the outer div and then specify the top as 50%.

How do you center text in a tag?

The HTML <center> tag is used to center the text horizontally in the HTML document. Since this tag was removed in HTML5, it is recommended that you use the CSS text-align property to format the text horizontally in the document. This tag is also commonly referred to as the <center> element.


2 Answers

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>
like image 130
Pablo Salcedo T. Avatar answered Sep 20 '22 03:09

Pablo Salcedo T.


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

like image 27
YAMAN Avatar answered Sep 19 '22 03:09

YAMAN