game on
game on

Reputation: 357

how to align 2 div inside a div?

I have this JSFiddle: Click here

<div id="header-parent">
    <div id=header>     
            <div id=header-title><img src="${pageContext.request.contextPath}/icon/pizza2.png"><span>this is a title</span></div>
            <div id=header-cart><div id="cart-title">
                                    <div><span>0</span></div>
                                    <div><span>pizza cart</span></div>
                                </div>
                                <div id="cart-icon"><img src="${pageContext.request.contextPath}/icon/shopping-cart.PNG"></div>
            </div>
    </div>
</div>

If you go to that page youll see that the right div is actually align under the header itself.

How can I set the right div to be inside the header?

Upvotes: 0

Views: 44

Answers (2)

Wayne Smith
Wayne Smith

Reputation: 4868

http://jsfiddle.net/usnmLuy5/3/

Use display:block-inline to keep the blocks from using 100% width. Use a float on the cart, and set the height of the header so that the cart will fit. spell check height, there is a heigth in the CSS which is being ignored and is not needed.

#header-parent {
    padding-top:5px;
    padding-bottom:5px;
    border-bottom: 1px solid White;
    background: #45484d; /* Old browsers */
    background: -moz-linear-gradient(top, #45484d 43%, #000000 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(43%,#45484d), color-stop(100%,#000000)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #45484d 43%,#000000 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #45484d 43%,#000000 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #45484d 43%,#000000 100%); /* IE10+ */
    background: linear-gradient(to bottom, #45484d 43%,#000000 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#45484d', endColorstr='#000000',GradientType=0 ); /* IE6-9 */
}

#header {
    position: relative;
    margin-left: auto;
    margin-right: auto;
    cursor: pointer;
    height: 36px; /* same as cart */
}

 #header-child{
    padding:5px 0 5px 0;
    display:inline-block;
}
#header-title {
    display:inline-block;
}
#header-title img{
    width: 25px;
    heigth: 25px;
    margin-right:5px;
    vertical-align: middle;
}

 #header-cart{
    display: inline-block;
    text-align:center;
    background-color:red;
    float:right;
    color:white;
    font-size: 15px;
}

 #header-cart img{
    width: 30px;
    heigth: 30px;
}

 #header-cart #cart-icon{
    float:left;
}
 #header-cart #cart-title{
    float:left;
}

 #header-cart #header-title{
    display: inline-block;
     background-color:blue;
}

#header-title span{

    color: white;
    font-family: Georgia, Times, "Times New Roman", serif;
    font-size:20px;
    margin-top: 0;
    margin-bottom: 0;
    vertical-align: middle;
}

Upvotes: 0

LcSalazar
LcSalazar

Reputation: 16841

A float always float above the next element. So place the header-cart before the cart-title.

Updated JsFiddle

Obs: I didn't fix the fiddle, there's some serious adjustments you have to make. You didn't quoted the ids, among other things...

Upvotes: 2

Related Questions