0

Hi I am struggling to center an image in a div vertically, wondering if anyone could assist.

<div id="homepage">
    <img src="assets/img/home-logo.png" id="home-logo">
</div>

#home-logo{
  display: block;
  margin: 0 auto;
  vertical-align: middle;}

#homepage{height: 100vh;
  background-color:#fff}
Nick
  • 29
  • 5

4 Answers4

1

Try using flexbox:

#homepage {
    height: 100vh;
    background-color: #fff;
    display: flex;
    align-items: center;
}
Zoe
  • 23,712
  • 16
  • 99
  • 132
Vikas Jadhav
  • 4,130
  • 1
  • 13
  • 32
0

Try this

<img src="paris.jpg" alt="Paris" class="center"> 
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
}
Zoe
  • 23,712
  • 16
  • 99
  • 132
Anish Sharma
  • 177
  • 1
  • 11
0

Try this code. May It'll be helpful for you.

#home-logo{
    display: block;
    margin: 0 auto;
    padding: 0;
    text-align: center; 
}

#homepage{height: 100vh;
    background-color:#fff;
    margin: 0 auto;
    padding: 0;
    text-align: center; 
}
Nitin Vaghani
  • 300
  • 4
  • 14
0

use flex for the container and set justify-content: center and align-items: center

#home-logo {
  display: block;
  margin: 0 auto;
  width: 200px;
  height: 200px;
}

#homepage {
  display: flex;
  height: 100vh;
  background-color: #fff
  justify-content: center;
  align-items: center;
}
<div id="homepage">
  <img src="assets/img/home-logo.png" id="home-logo">
</div>
Itay Gal
  • 10,075
  • 6
  • 30
  • 68