Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Center Image in the middle of the page

I’m attempting to place an image in the center of the page. vertically and horizontally, regardless of screen size. How is this possible?

<label class="img-wrapper">
    <img src="logo.jpg">
</label>
img {
    margin-top: 25%;
    margin-left: 25%;
    margin-right: 25%;
    margin-bottom: 25%;
    padding: 10px;
}

Here’s an image explaining what I’m trying to do. https://imgur.com/a/eVtfGdi

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can make use of flexbox to align elements. See the snippet below:

*{
  margin: 0;
  padding: 0;
}
.img-wrapper{
  height: 100vh;
  display: flex;
  align-items: center; /*align items vertically*/
  justify-content: center; /*align item horizontally*/
}

img {
   width: 50vh;
   height: 50vh;
}
<label class="img-wrapper">
    <img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fm.media-amazon.com%2Fimages%2FI%2F61yBDRJCKBL._SL500_.jpg&f=1&nofb=1">
</label>

More on flexbox here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading