0

This is what I'm trying to do:

dd

Make the icon vertically centered inside div and the text vertically centered too!

I tried this:

<div id="mainDiv">
   <div id="top">
      <span><img src="path/img.png" class="myImage" /></span>
      <span>My Title</span>
   </div>
</div>

And css:

#mainDiv {
   width:100%;
   height:30px;
}

#top {
   width:50%;
   height:30px;
   display:table;
}

.myImage {
   vertical-align: middle;
   display: table-cell;
}

span {
   vertical-align: middle;
   display: table-cell;
}

But this is what I get:

image

Flávio Alencar
  • 192
  • 1
  • 2
  • 9

5 Answers5

0

It is already vertically centered. If you want text touch with the icon. you can do easily with display:flex.

like:

#top {
    align-items: center;
    background: blue none repeat scroll 0 0;
    display: flex;
    height: 30px;
    width: 50%;
}

.myImage {
   vertical-align: middle;
}

Working Fiddle

ketan
  • 19,129
  • 42
  • 60
  • 98
0

Add display:table-cell; to span

Add Following css

span {
   vertical-align: middle;
   display: table-cell;
   background:#ccc;
   border: 1px solid black;
   display: table-cell;
}

https://jsfiddle.net/hLs0mLp6/1/

Johncena365
  • 146
  • 3
0
   height:80px;
   line-height:80px;

try this snippet

#mainDiv {
   width:100%;

}

#top {
   width:50%;
   height:80px;
   background:yellow;
   line-height:80px;
}
<div id="mainDiv">
   <div id="top">
      <span><img src="path/img.png" class="myImage" /></span>
      <span>My Title</span>
   </div>
</div>
Gautam Jha
  • 1,445
  • 1
  • 15
  • 24
0

Try this,

#mainDiv {
width:100%;  
}

#top {
width:50%;
height:30px;
display:table;  
}

.myImage {
vertical-align: middle;
background-color:red;
}

span {
vertical-align: middle;
line-height:30px;  
}
Sunil Bharath
  • 206
  • 1
  • 7
0
#top {
   width: 50%;
   height: 40px;
   display: table; /* display: flex is a good option.*/
   float: left;
 } 

.myImage {
       vertical-align: middle;
   }
rb4bhushan
  • 115
  • 2
  • 11