tags:

views:

34

answers:

3

I'm using CSS buttons from this tutorial:

http://www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html

I need to put a button in the middle of a DIV so it's centered. But I can't!

Here's the code of the button:

<a class="button" href="#"><span>Bring world peace</span></a>  

And here's CSS:

.clear { /* generic container (i.e. div) for floating buttons */
    overflow: hidden;
    width: 100%;
}

a.button {
    background: transparent url('bg_button_a.gif') no-repeat scroll top right;
    color: #444;
    display: block;
    float: left;
    font: normal 12px arial, sans-serif;
    height: 24px;
    margin-right: 6px;
    padding-right: 18px; /* sliding doors padding */
    text-decoration: none;
}

a.button span {
    background: transparent url('bg_button_span.gif') no-repeat;
    display: block;
    line-height: 14px;
    padding: 5px 0 5px 18px;
} 

Here's the code I'm trying to use:

<div align="center"><a class="button" href="#"><span>Bring world peace</span></a></div>
+2  A: 

The a.button is floated to the left. You could try float: none; on that element. margin: 0 auto; is also useful for center-aligning elements.

Does that help?

blob8108
+1. Remove the float from the button. Floated elements are removed from the normal page layout, thus are unaffected by the layout styles of their parent elements.
Chris
+1  A: 

Modify the button class for these properties:

.button{
  margin-left:50%;
  margin-right:50%;
  position: relative;
}

And wrap your link in the div like this:

<div align="center">
  <a class="button" href="#"><span>Bring world peace</span></a>  
</div>
Sarfraz
Thanks, it works only when there's one word. When there's more than 1 word, I getBRINGWORLD PEACE
Alex
@Alex: Try adding `text-align:center` to both `.button` class as well as the DIV.
Sarfraz
+2  A: 

the align attribute for the div element is deprecated. You're better off defining a class for that div, like so:

<div class="centerize">
  <a class="button" href="#"><span>Bring world peace</span></a>
</div>

And the CSS:

.centerize { 
    text-align: center;
}

Note however that setting the text-align will only affect the content inside the div. The div itself (should be) a block element, and depending on where it sits in the document structure, may not be centered itself.

Just to make a little more certain, you can do something like this:

.centerize {
    display: block;
    margin: 0 auto;
    text-align: center;
}

Now you can apply centerize to any element, and that element should take up the entire browser's width and center-align its content.

Chris