views:

72

answers:

6
+2  Q: 

Simple CSS Query

<div class="priceBox">

    <a href="#">
        <div class="priceBtn">
            <span class="priceBtnTxt">Order Now</span>
        </div>
    </a>  

</div>

What CSS properties would any of the classes need so that priceBtn is centered within priceBox ?

+4  A: 

It really depends on the outcome you are looking for. To center that div you could use:

.priceBtn
{ 
    margin: 0 auto;
}
Dustin Laine
Thanks! Any explanation why this works?
Tom Gullen
the zero means that the top and the bottom margin is 0, and the `auto` means that the left and the right margins are being calculated at runtime depending on the width of the container. The left and the right margins are equal to `(container_width - box_width) / 2`
Pavel Nikolov
Thanks for the added explanation Pavel!
Dustin Laine
No idea why you got 2 downvotes for this it answered the question perfectly.
Tom Gullen
+1  A: 

Normally you would just need .priceBtn { margin: auto; } and reduce .priceBtn element's width. It might be more complicated than that depending on the context, though.

Reinis I.
+2  A: 

You HTML is invalid, so we're going to fix that first. An anchor <a> is an inline element nd a <div> a block level element. You can't put a block level element in an inline element.

This would result in inconsistent behavior in different browsers. In particular, you might only be able to click the text of your hyperlink in some browsers, and be able to click its entire area in others.

So instead you'll use more CSS.

HTML

<div class="priceBox">

    <a href="#" class="priceBtn">
        <span class="priceBtnTxt">Order Now</span>
    </a>  

</div>

CSS

You now have two options:

.priceBox {
    text-align:center;
}

or:

.priceBox a.priceBtn {
    display:block;
    margin:auto;
    /* These you can toy with but they're mostly there for demo purposes */
    width:100px;
    borde:1px solid black;
}
LeguRi
@Tom Gullen - if you can explain why you had a `<div>` inside an anchor `<a>` I'll try and provide you with more explanation/alternatives.
LeguRi
Thanks for pointing it out to me, I've changed it now so it's valid. It was just an old habbit I always did.
Tom Gullen
@Tom Gullen: Time to break the habit instead of the code. :)
Guffa
+1  A: 

Dependant on the size of priceBox

.priceBtn
{
    margin: 0 auto;
    width:100px;
}

given 100px as example as dont know the size of pricebox,

I would suggest putting the anchor inside priceBtn too

JamesStuddart
+1  A: 

First of all, the HTML code is invalid. You can't put a block element inside the anchor element as it is an inline element. You need to put inline elements inside the anchor element instead:

<div class="priceBox">

    <a href="#">
        <span class="priceBtn">
            <span class="priceBtnTxt">Order Now</span>
        </span>
    </a>  

</div>

Now you can make the anchor tag and the span inside it block elements. This works as the structure is valid both before and after the CSS is applied:

priceBox a, priceBox a > span { display: block; }

To center the button inside the box, you use auto margins on the button:

priceBox a { margin-left: auto; margin-right: auto; }
Guffa
+1  A: 

try

.priceBtn{
   margin: 0 auto;
   ...
}
katrin