Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 290
op1001 Avatar asked Dec 24 '13 07:12

op1001


2 Answers

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!

like image 112
NoobEditor Avatar answered Oct 06 '22 22:10

NoobEditor


Your button needs to be a block level element. Simplest fix:

a.button-large{  
    padding : 15px 30px 14px 30px;
    margin  : 5px auto;
    display : block;
    width   : 100px;
}

http://jsfiddle.net/jqvbM/

I would also delete the 10px right margin on a.button-large i to center the "Learn More" text.

like image 43
AlienWebguy Avatar answered Oct 06 '22 22:10

AlienWebguy