-2

I've been trying to center an image horizontally, but everything I've tried hasn't been working. I need this image to be at the center of the div no matter the screen size, so it can appear centered on PC screens and phones as well ( it's for a project). If anybody can help me, I'd greatly appreciate it.

HTML code:

 <div class = "main">
    <img src = "kidme.jpg" alt ="Me!" >
 </div>

CSS code :

.main {
  background-color: #36454F;
  width:100%;
  height:345px;
}

img {
  height:326px;
  width:380px;
  position: absolute;
  margin-top: 20px;
}
Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41
Sinan Sinan
  • 15
  • 1
  • 3

2 Answers2

0

This could works :

 .main {
  background-color: #36454F;
  width:100%;
  height:345px;
 }

img{
 height:326px;
 width:380px;
 display: block;
 margin:auto;
 margin-top: 20px;
}
Teshtek
  • 1,212
  • 1
  • 12
  • 20
0

Check this link

https://plnkr.co/edit/LsuxKAtvScM4znT1ROci?p=preview

                <div class = "main">
                <div class="imgwrap">
                <img src = "kidme.jpg" alt ="Me!" >
                </div>
            </div>
Sharmila
  • 785
  • 4
  • 11