0

I have an outer div and an inner div. Inner div has background-image.

How to vertically center align inner div image?
I dont want to use margin-top

Here is fiddle

ozil
  • 6,930
  • 9
  • 33
  • 56

6 Answers6

1

Use this CSS. You'll have that Google logo centered vertically.

#imgDiv {
    width: 300px;
    height: 100px;
    background-image: url("http://www.google.com/intl/en_com/images/srpr/logo3w.png");
    background-repeat: no-repeat;
    border: 1px solid blue;

    position:relative;
    top: 50%;
    -moz-transform: translatey(-50%);
    -ms-transform: translatey(-50%);
    -o-transform: translatey(-50%);
    -webkit-transform: translatey(-50%);
    transform: translatey(-50%);
}
Xahed Kamal
  • 2,203
  • 1
  • 20
  • 41
1

You can translate this with some simple CSS. Use the "position" property.

#parentDiv {
    position: relative;
}

#childDiv {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
}
1

Try like this: Demo

Updated demo as per your requirement: Note: If the text height increases, the background image will move upwards

div.outer {
display:table;
border: 1px solid red;
height:300px;
width:300px
}
.inner {
    display:table-cell;
    vertical-align:middle;
    line-height:normal;
    margin:0 auto;
    text-align:center;
}
#imgDiv {
    width: 300px;
    height: 100px;
    background-image: url("http://www.google.com/intl/en_com/images/srpr/logo3w.png");
    background-position: center;
    background-repeat: no-repeat;
    border: 1px solid blue;
}
#spanid {
    text-align: center;
}

HTML:

<div class="outer">
    <div class="inner">
        <div id="imgDiv"></div> <span id="spanid">Some text....</span>
    </div>
</div>
G.L.P
  • 7,119
  • 5
  • 25
  • 41
1

Easiest way would be to set the height of #imgDiv to height:100%; and then set the background-position to center, center.

#imgDiv {
    width: 300px;
    height: 100%;
    background-image: url("http://www.google.com/intl/en_com/images/srpr/logo3w.png");
    background-repeat: no-repeat;
    border: 1px solid blue;
    background-position:center,center;
}
Benneb10
  • 1,419
  • 1
  • 8
  • 13
0

You can use display flex to make it working! Here is the Demo.

#imgDiv {
        width: 500px;
        height: 100px;
        background-image: url("http://www.google.com/intl/en_com/images/srpr/logo3w.png");
        background-repeat: no-repeat;
        border: 1px solid blue;
        margin: auto;
    }
    .outerDiv {
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        border: 1px solid red; 
        height:300px; 
        width:300px;
    }
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
0

Parent to add display: table; Child to add display:table-cell; and vertical-align:middle;.If you want background center child to add background-position: center center;

#imgDiv {
    width: 300px;
    height: 100px;
    background-image: url("http://www.google.com/intl/en_com/images/srpr/logo3w.png");
    background-repeat: no-repeat;
    border: 1px solid blue;
    display: table-cell;
    vertical-align: middle;
}
<div style="position:relative; border: 1px solid red; height:300px; width:300px; display: table;">
    <div id="imgDiv"> Hello Vertical Middle !! </div>
</div>
lwinkyawmyat
  • 1,221
  • 1
  • 16
  • 34