Search code examples
htmlcssbuttoncenteringflat

why won't this css button center inside div?


So... this is starting to annoy me ... and I thought i would come here and get some help..

The main div around the box has a width... then there is text... and a button that i want in the center of the div..

it is a <a href> button.. but it wont center.

I didn't think I would need to specify a width since the outside div has a width.. i tried margin:0 auto; text-align:center etc nothing works

<h2 class="service-style color_service">Annoyed</h2>
<div class="service-text text1">
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Lorem ipsum dolor sit amet…
    </p>

    <a href="" class="button button-large"><i class="fa fa-credit-card" style="padding-right:10px;"></i> Learn More</a>
</div>

here is the fiddle link

http://jsfiddle.net/2gMQ9/


Solution

  • You have the problem because of the display:inline-block...see here

    In this section of css,add:

    Remove all the margin's set at buttonclass and amend as below:

    /*  --------------------------------------------------
        :: Button Configuration
        -------------------------------------------------- */
     .button {
        display:block; /* change this from inline-block */
        width:20%; /* setting the width */
        margin:0 auto; /* this will center  it */
        position:relative;
        font-style:normal;
        font-weight:normal;
        font-family:"Open Sans";
        font-size:14px;
        outline:none;
        color:#fff;
        border:none;
        text-decoration:none;
        text-align:center;
        cursor:pointer;
        -webkit-border-radius:2px;
        -moz-border-radius:2px;
        border-radius:2px;
        background-color:#96aa39;
        border-bottom:1px solid #7b8b2f;
    }
    

    EDIT : to use inline-block without setting a width

    inline-block demo

    How to do only solution to center inline-block element is to use text-align:center

    in your css class:

    .service-text {
    text-align: center; /* add this to center the button */
    }
    

    and then add:

    .service-text p{
     text-align:left /* and then add this to left align the text inside the para*/
    }
    

    this way you wont need to give width and only padding would solve your problem!