Vertically Center Image in a Div

I have been struggling to center an image within a div, but all the usual methods like using margin: auto; do not seem to work. I have tried various popular techniques with no success.

<style>
/* Add a black background color to the top navigation */
.topnav {
  background-color: #1F363D;
  overflow: hidden;

}

/* Style the links inside the navigation bar */
.topnav a {
  float: left;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
  background-color: #BBBBBB;
  color: #1F363D;
}

/* Add a color to the active/current link */
.topnav a.active {
  background-color: #808782;
  color: white;

}

body{
    margin: 0px;
}

a{
    font-family: monospace;
}

h1 {
    font-family: monospace;
}

.header {
    text-align: center;
    background-color: #000088;
    color: white;
    padding: 5px;
}

img{
    width: 100px;
    height: 100px;

    border-style: solid;
    border-color: black;
    border-radius: 10%;
    border-weight: 1px;
    vertical-align: middle;
}

.grid-holder {
  display: grid;
  grid-template-columns: 100px 100px 100px 100px;
    grid-templete-rows: 100px 100px 100px 100px;
    gap: 20px;
    height: 100%;
    width: fit-content;
    margin: auto;
    pading: 5px;

    border-style: solid;
  border-width: 1px;
    border-color: black;
    border-radius: 10px;
}

#mastermind-holder {
    grid-row: 1 / span 1;
    grid-column: 1 / span 1;

}

#simon-holder {
    grid-row: 1 / span 1;
    grid-column: 2 / span 1;
}

.grid-item{
    text-align: center;
    padding: 5px;
    border-style: solid;
    border-color: black;
    border-radius: 10%;
    border-weight: 1px;
    width: 100px;

}

.grid-item a {
    border-style: solid;
    border-color: black;
    border-radius: 10%;
    border-weight: 1px;
}
</style>
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Portfolio - Projects</title>
  <link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>
    <div class="header">
        <h1>Portfolio</h1>
    </div>
  <div class="topnav">
    <a href="/index.html">Home</a>
    <a class="active" href="/projects.html">Projects</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
    </div>




    <div class="grid-holder">

        <div class="grid-item" id="mastermind-holder">
            <div class="image-holder">
                <img src="/mastermind-icon.png">
            </div>
            <a>Mastermind</a>
        </div>

        <div class="grid-item" id="simon-holder">
            <img href="">
            <a>Simon</a>
        </div>


    </div
  <script src="script.js"></script>
</body>

</html>

Answer №1

To vertically align the image inside the parent container image-holder, you can use display:flex on the parent container and apply align-item:center.


I made a slight adjustment to the width of the image for better display.

/* Styling for top navigation bar */
.topnav {
  background-color: #1F363D;
  overflow: hidden;    
}

/* Styling for links in the navigation bar */
.topnav a {
  float: left;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Hover effect for links */
.topnav a:hover {
  background-color: #BBBBBB;
  color: #1F363D;
}

/* Styling for active/current link */
.topnav a.active {
  background-color: #808782;
  color: white;
}

body {
    margin: 0px;
}

a {
    font-family: monospace;
}

h1 {
    font-family: monospace;
}

.header {
    text-align: center;
    background-color: #000088;
    color: white;
    padding: 5px;
}

img {
    max-width: 100px;
    max-height: 100px;
    padding: 30px;
    border-style: solid;
    border-color: black;
    border-radius: 10%;
    border-weight: 1px;
    vertical-align: middle;   
}

.image-holder {
    display: flex;
    align-items: center;
}
<div class="header">
    <h1>Portfolio</h1>
</div>
<div class="topnav">
    <a href="/index.html">Home</a>
    <a class="active" href="/projects.html">Projects</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
</div>


<div class="grid-holder">

    <div class="grid-item" id="mastermind-holder">
        <div class="image-holder">
            <img src="/mastermind-icon.png">
        </div>
        <a>Mastermind</a>
    </div>

    <div class="grid-item" id="simon-holder">
        <img href="">
        <a>Simon</a>
    </div>
You can also use display:grid and apply place-item:center on the parent element for alignment.

/* Styling for top navigation bar */
.topnav {
  background-color: #1F363D;
  overflow: hidden;    
}

/* Styling for links in the navigation bar */
.topnav a {
  float: left;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

/* Hover effect for links */
.topnav a:hover {
  background-color: #BBBBBB;
  color: #1F363D;
}

/* Styling for active/current link */
.topnav a.active {
  background-color: #808782;
  color: white;
}

body {
    margin: 0px;
}

a {
    font-family: monospace;
}

h1 {
    font-family: monospace;
}

.header {
    text-align: center;
    background-color: #000088;
    color: white;
    padding: 5px;
}

img {
    max-width: 100px;
    max-height: 100px;
    padding: 30px;
    border-style: solid;
    border-color: black;
    border-radius: 10%;
    border-weight: 1px;
    vertical-align: middle;   
}

.image-holder {
    display: grid;
    place-content: center;
}
<div class="header">
    <h1>Portfolio</h1>
</div>
<div class="topnav">
    <a href="/index.html">Home</a>
    <a class="active" href="/projects.html">Projects</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
</div>


<div class="grid-holder">

    <div class="grid-item" id="mastermind-holder">
        <div class="image-holder">
            <img src="/mastermind-icon.png">
        </div>
        <a>Mastermind</a>
    </div>

    <div class="grid-item" id="simon-holder">
        <img href="">
        <a>Simon</a>
    </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you explain the differences between offsetHeight, clientHeight, and scrollHeight for me?

Have you ever wondered about the distinction between offsetHeight, clientHeight, and scrollHeight? What about offsetWidth, clientWidth, and scrollWidth? Understanding these differences is crucial for working effectively on the client side. Without this kn ...

Using div tags may cause rendering issues

I am trying to use multiple div tags with webkit borders, but for some reason only the one called 'Wrapper' is displaying properly. Here is my code: .wrapper { margin: 20px auto 20px auto; width: 800px; background: url('images/background_1. ...

Concealing a form after submission using JavaScript

After submitting the form, I am attempting to hide it and display a loading GIF. I've experimented with various methods, including changing ClassName to Id, but haven't had success. This is for a school project, and I've spent a significant ...

Having several horizontal scrollbars at once on a single webpage

I stumbled upon a great example showcasing how to scroll a div on my page: Check out this Codepen - https://codepen.io/zheisey/pen/xxGbEOx Although, I am looking to extend this functionality to allow multiple divs to scroll together. Any suggestions? I ...

Expanding the blank area on the right margin of the page

Recently, I took a responsive website template and converted it into an ASP.NET MVC app. However, I've encountered some peculiar behavior when resizing the browser window. As the width of the window decreases below 986 pixels, a mysterious white "spac ...

Leveraging HTML5 Canvas for blending multiple transformations

Is it possible to zoom out an image, rotate it by 30 degrees around its center, and then vertically flip the rotated image while maintaining the rotation? ...

What is the best way to superimpose text on a variety of images with varying sizes using CSS

I have a client who is requesting a sold text to be displayed on top of all images that have been sold, positioned in the bottom left corner of each image. While I have successfully implemented this feature, the issue arises when dealing with images of var ...

Display flex causing Mui LinearProgress Bar to disappear

Looking for a way to create a unique bullet graph using material UI? How about two MuiLinearProgress bars placed side by side with a vertical Divider in between them. Struggling to get them displayed next to each other? <div className={classes.bulletGra ...

Is there a way to incorporate a JavaScript variable as the value for CSS width?

I created a scholarship donation progress bar that dynamically adjusts its width based on the amount donated. While I used CSS to handle the basic functionality, I am now trying to implement JavaScript for more advanced features. My goal is to calculate ...

The jQuery div enclosure technique

I am trying to wrap HTML around an existing div, here is what I have attempted: HTML: <input type="text" data-placeholder="username" /> It should look like this when rendered: <div class="placeholding-input"> <input type="text" data-pl ...

Steps to transfer the content of a label when the onclick event occurs

Seeking advice on how to send the dynamically varying value of a label upon clicking an anchor tag. Can anyone recommend the best approach to passing the label value to a JavaScript function when the anchor is clicked? Here is a sample code snippet: < ...

I am currently working with CakePHP and am interested in learning how to expire the admin session

I'm having issues with the code in my UserController. When I open the admin panel in two tabs within the same browser and log out from one tab, I am unable to redirect to the login page from the other tab when clicking on any menu. function beforeFil ...

The appearance of responsive CSS is altered when deployed compared to when it is viewed on the

I'm a beginner in web development and facing an issue with my mobile CSS. The strange thing is that when I view it on localhost, everything looks great, but once deployed (tried both Heroku and GitHub), it appears distorted. Even when I make extreme c ...

triangle shape filled with a gradient that transitions between two colors

I have two triangles displayed at the bottom of my page - one on the left and one on the right. The right triangle is currently transparent but I want to add a gradient effect to it. For the triangle-bottom-right, I'd like the gradient to go from rgb ...

the label remains grounded even as the browser autocompletes passwords with the password manager

https://i.stack.imgur.com/vJMyg.png I noticed that the password label on my login form does not float when the browser's password manager auto-completes. Can someone help me fix this issue? Attached is an image for reference. /* login form ...

Select a specific element to apply a CSS selector for a button

One of the challenges I am facing involves a webpage with multiple submit buttons. My goal is to iterate through each button and click on them one by one. While I am aware that this can be achieved using xpath like this (//button[@class='submit' ...

Sort various divs using a list

I have multiple divs containing different content. On the left side, there is a list of various categories. When a category is clicked, I want to display the corresponding div for that category. Initially, I want the main category to be loaded, with no opt ...

The mouse scurries away once the div height has been adjusted

How can I make the height of #header change when hovering over #hoverme, and then revert back to its original height when the mouse leaves #hoverme? If anyone knows a solution, please check out my jsfiddle as it's not working as I intended. Here is ...

Problem with Flickity's is-selected feature

I am currently exploring Flickity and its functionality. I have a carousel that auto-plays, with the selected cell always placed in the middle and highlighted with a grey background. However, I would like to customize it so that the selected cell is positi ...

Take away the CSS class from an element after reCAPTCHA verification is complete in Next.js

I'm struggling with removing the CSS class btn-disabled from the input button element upon successful verification of a form entry. I have implemented a function called enableForm to remove the btn-disabled CSS class when reCAPTCHA is verified. Howe ...