Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical align div inside div is not working - CSS

JS fiddle : http://jsfiddle.net/Rkh8L/

I am trying to vertically middle div inside div. The class i want to be vertically middles is MonsterImage.

Here the whole code

            <div style="float: left; text-align: center; vertical-align: middle; margin:10px;">

                <asp:RadioButton  ID="RdButtonMonsterImages" ClientIDMode="Static" runat="server" />

                <div class="permonster" >
                    <div class="MonsterImage"></div>            
                </div>
        </div>



            .permonster
        {
            width: 130px;
            height: 120px;
            text-align: center;
            vertical-align: middle;
            border-top: 1px solid #f7fcff;
            background: #ababab;
            background: -webkit-gradient(linear, left top, left bottom, from(#e3e6e8), to(#ababab));
            background: -webkit-linear-gradient(top, #e3e6e8, #ababab);
            background: -moz-linear-gradient(top, #e3e6e8, #ababab);
            background: -ms-linear-gradient(top, #e3e6e8, #ababab);
            background: -o-linear-gradient(top, #e3e6e8, #ababab);
            -webkit-border-radius: 8px;
            -moz-border-radius: 8px;
            border-radius: 8px;
            -webkit-box-shadow: rgba(0,0,0,1) 0 1px 0;
            -moz-box-shadow: rgba(0,0,0,1) 0 1px 0;
            box-shadow: rgba(0,0,0,1) 0 1px 0;
            text-decoration: none;
            padding:2px;
        }


            .MonsterImage
            { 
border-width: 0px; border-style: none; 
background-image: url(http://static.monstermmorpg.com/images/csssprites/RegisterCSS.png); 
background-color: transparent; 
margin:auto; 
background-repeat: no-repeat; 
background-position: -0px -120px;  
width: 130px; 
height: 96px;   
            }
like image 353
MonsterMMORPG Avatar asked Dec 12 '22 09:12

MonsterMMORPG


2 Answers

You can center (vertical and horizontal align) a div inside a div as below

HTML

<div id="parent">
    <div id="child">
    </div>
</div>

CSS

#parent {
    background-color: #333333;

    position: relative;
    height: 300px; 
    width:300px;
}

#child {
    background-color: #cccccc;

    position: absolute;
    top: 50%;
    left: 50%;
    height: 30%;
    width: 50%;
    margin: -15% 0 0 -25%;
}

See this article which explains how it works.
Note: Background color is only for illustration purposes only.

See the result below.

enter image description here

like image 185
Nadeeja Bomiriya Avatar answered Jan 14 '23 12:01

Nadeeja Bomiriya


You can't vertical-align that element, just add some margin to the top of your .MonsterImage class, something like margin-top:13px; should do it.

like image 42
Andres Ilich Avatar answered Jan 14 '23 14:01

Andres Ilich