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

Retrieve the title of the item by selecting it within the h1 tag using Beautiful Soup

import requests from bs4 import BeautifulSoup def fetch_webpage(url): response = requests.get(url) if not response.ok: print('Server Responded: ', response.status_code) else: soup = BeautifulSoup(response.text, 'lxml') ...

React Native - Scaling images dynamically

I have a responsive photo grid built within a mobile app. I want to display three images across the screen with equal spacing between them. All the images are squares. Unfortunately, using percentages for each image's width and height is not possible ...

Shorten certain text in Vuetify

Here's an example of a basic select component in Vuetify: <v-select :items="selectablePlaces" :label="$t('placeLabel')" v-model="placeId" required ></v-select> I'm looking to apply a specific style to all selec ...

Selecting just a single response as the correct solution

I am currently in the process of developing a Q&A website inspired by Stack Overflow. However, I have encountered an issue where when I try to mark an answer as accepted, it ends up marking every answer as accepted. My goal is to allow users to only ma ...

When the horizontal scroll is turned off, it also disables the functionality of my mobile-friendly

I came across a helpful post on StackOverflow that suggested using the following code to disable horizontal scrolling: html, body { overflow-x: hidden; } This solution did resolve my issue of horizontal scrolling, but unfortunately it caused problems ...

Utilize JavaScript to trigger a div pop-up directly beneath the input field

Here is the input box code: <input type='text' size='2' name='action_qty' onmouseup='showHideChangePopUp()'> Along with the pop-up div code: <div id='div_change_qty' name='div_change_qty&ap ...

Can anyone explain why my inner div's display is set to block and it is being pushed down when its parent div has

Below is a sample of my HTML+CSS code: <div> <div class="first">text</div> <div>more text</div> </div> div { display: inline; } .first { display: block; } What's interesting is that when the first ...

establish a distinct row color pattern for the initial column

I am currently working on a table that has sticky columns. I am trying to create alternating row colors in the first column only. Below is the code for the table: <div class="table-responsive"> <table class="table table-bordered table-stripe ...

Include a scroll bar for menu items that contain submenus, while ensuring that the submenu items remain visible

Looking for a way to add a scrollbar to the courses menu list due to its excessive length (refer to the image below). The fixed top navbar prevents scrolling, making it necessary to implement a scrollbar within the menu itself. After applying the followin ...

Row in Bootstrap 3 fails to display correctly on medium-sized devices

Having trouble with divs of different sizes? I want a row that shows 3-column wide divs on medium & large screens and 6-column wide divs on small devices. <div class="container"> <div class="row"> <div class="service col-sm-6 col-md-3"& ...

Dividing a string into an array and displaying it in a table using Laravel

Retrieving a string from the database and using the explode function to split the values. Below is the code snippet: $data = DoctorRegistration::select('products') ->where('doctorid','=',$doctorid) ->get(); ...

Vertically center the container

I am struggling to center my container div vertically, similar to how it is horizontally aligning with margin: auto;. Despite searching online for solutions, I have not been successful in finding a method that works for me. It is surprising to me that in t ...

What does "cascading" refer to in the context of Cascading Style Sheets?

Similar Query: Explaining the concept of "cascading" in CSS Can you clarify the term "cascading" within Cascading Style Sheets? ...

An unusual icon with three varying sizes in a single file

I have come across an interesting favicon file. Click here to download the favicon file Unfortunately, I am unable to upload this image file as it does not meet the required format standards. Upon viewing the file in Windows 7, a fascinating discovery w ...

When the text overflows the boundaries of a paragraph in CSS HTML

I am facing an issue with text overflowing in a paragraph <p style="width:200px;">Text goes here</p> As the text in the paragraph increases in width, it does not automatically wrap to a new line, causing it to overflow outside the paragraph ...

The RequiredFieldValidator and RegularExpressionValidator are not functioning properly

I have the following code snippet in one of my "C#" aspx pages. Despite having validation set up, the event attached to cmdPassword still fires even when the textbox is empty. Why are the validations not working? <tr> <td align="left" valign="t ...

React Fixed Footer Implementation against My Preferences

Here's an issue that I'm facing: https://i.stack.imgur.com/gtQqm.png The footer on my webpage is normally displayed at the bottom of the page. However, when the user performs certain actions that extend the size of the page: https://i.stack.im ...

How can I make CSS text-overflow ellipsis trigger a pop-up to display lengthy text when clicked?

I'm encountering an issue with a lengthy sentence in a table cell which I've truncated using text-overflow to display ellipsis. Is there a way to enable users to click on the dots (...) and view the full text in a popup window? table { wi ...

Tips for ensuring all my onclick event are clickable in iOS browser

Currently, I am developing a web page using HTML5 and JQuery Mobile. I have encountered an issue where the onclick function does not work on iOS device browsers. Is there a way to change all onclick events to be accessible through tapping instead? This wou ...

Dealing with empty POST request bodies in Node.js Express is a common challenge

In my Node.JS project using Express framework, I am encountering an issue while trying to access the body of a POST request. The POST request is being sent through an HTML form, and upon checking with WireShark, I can see that the request is indeed getting ...