Tips for aligning a div containing an image in the center of another div

Trying to perfectly center an image inside a div both vertically and horizontally using the display inline-block property.

HTML

<div class="center-wrapper">
   <div class="image-holder">
     <img src="picture.jpeg" alt="Centered Image">
   </div>
</div>

CSS

.image-holder {
    display: inline-block;
    width: 100px;
    height: 100px;
    border: 1px solid #ccc;
    margin-left: auto;
    margin-right: auto;
}

Answer №1

This technique is ideal for effortlessly centering dynamic content with unknown dimensions.

View jsFiddle demonstration

Here, we utilize vertical-align:middle to vertically center elements, and text-align:center for horizontal centering. To make vertical-align:middle function within a div, the parent elements must be set as display:table, while the targeted child div should have display:table-cell. While you could use absolute positioning for centering, it would require knowledge of the element's dimensions. This method ensures proper alignment even with unknown dimensions.

HTML

<div class="container">
   <div class="box">
       <img src="http://placehold.it/200x200">
   </div>
</div>

CSS

html, body {
    height:100%;
    width:100%;
    margin:0px;
}
.container {
    display:table;
    vertical-align:middle;
    text-align:center;
    height:100%;
    width:100%;
}
.box {
    display:table-cell;
    vertical-align:middle;
}

Answer №2

When aligning elements horizontally, you can use the following CSS:

.box {
display: inline-block;
width: 32px;
height: 32px;
border: 1px solid #f2f2f2;  
margin:auto;
text-align:center;
}

For vertical alignment, one way is to apply the following CSS properties:

#content {position:absolute; top:50%; height:240px; margin-top:-120px; /* negative half of the height */}

For more information on vertical centering using CSS, check out this resource:

Answer №3

If you want to display an image in a table, you can follow this example:

  <table class="image-table">
     <tr class="image-row">
         <td class="img-cell"><img src="example.jpg"></td>
     </tr>
  </table>

To style the image in the table, add the following CSS:

.img-cell {
   vertical-align: middle;
   text-align: center;
}

Check out this JSFiddle for more reference

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

Retrieving a unique custom data-attribute using select2 on a <select> element

If you were presented with the following HTML5 snippet: <select id="example"> <option value="AA" data-id="143">AA</option> <option value="BB" data-id="344">BB</option> </select> $("#example").select2(); What i ...

Click to dynamically toggle classes with jQuery

I am trying to apply a class called 'select' when I click on a paragraph tag. However, the code I have written doesn't seem to be working. Can someone please provide some suggestions? Here is the code: <style type="text/css"> #el ...

php - Can you identify the issue?

File Name: sms1.php Located at: Here is the PHP code for sms1.php: <?php //Variables to POST $user = "hidden"; $password = "hidden"; $mobiles = "$_POST[phone]"; $message = "$_POST[msg]"; $sender = "$_POST[sender]"; //Setting up CURL to send data via ...

Ways to extract the coordinates for slices positioned around the circumference of a pie chart

Currently, I am working on designing a pie chart with D3 using d3.layout.pie(). The image resembles the one displayed below, but without the black dots. These dots were added manually in Photoshop to highlight an issue that I am facing. I am curious about ...

Issues with Carousel Plugin Functionality

**Hey everyone, I could really use some help. As a beginner coder, please forgive any errors in my code. I am currently working on a webpage where I need to incorporate a carousel plugin within a panel body to display the latest photos. The code provided ...

Experiencing an issue with referrer: The global symbol needs an explicit package name

my $refer = $ENV{HTTP_REFERER}; my $gk1 = substr($str1, -4); if ($gk1 = .swf) { my $antigk = "gk detected"; } else { my $antigk = $link; } I am encountering issues with this code after making some modifications. What could be causing the errors? I ...

Position the Bootstrap icon to float to the far right

My icon is always positioned to the right instead of beside the input field. Take a look at the image below: https://i.stack.imgur.com/Bb1eH.png The icon does not align next to the input field. This is my code snippet : <div class="form-horizontal"& ...

Incorporate negative padding in order to smoothly scroll to a specific ID

When I jump to an ID using domain.com/#section, the scroll goes too far down and needs a negative margin added. Is there a way to achieve this directly in the URL without relying on CSS, jQuery, or JavaScript? ...

Achieving a frosted glass effect for your background without relying on a background image

I am currently working on creating a modal window in React with a blurred backdrop. My goal is to blur the backdrop content while keeping the background image clear, as there is no image behind the modal window, just content. I have explored various soluti ...

How to use CSS to dynamically adjust the maximum width of an overflow container based on its children's content

I have a container with an overflowing <div> that displays multiple <ul> <li> lists. Each list always contains the same number of bordered <li> items, resembling a table. However, there may be instances where a <ul> has only ...

The text becomes jumbled and messy when the browser is resized

I came across this fantastic header design > https://codepen.io/rm/pen/ldhon <. However, I noticed that when the window is resized, the text ends up overlapping. In the demo provided, resizing the window too much causes the issue to become apparent, but ...

"Comparison: Utilizing HTML5 Video Player with IE8 Embed Fallback versus Implementing Fixed

This dilemma has me at my wit's end. In an HTML5 Video element, I've included an mp4, ogg, and an embedded mp4 fallback. However, on the embed fallback in IE8, all my attempts to position the fixed element (#fixed) above it with z-indexing have f ...

Creating a CSS key animation for resizing an image: What is the correct way to make an image expand and contract

Check out this CSS key animation example: http://jsfiddle.net/ryurage/Q2ELp/ I'm attempting to make an image grow and occupy most of the window when triggered by user interaction like hovering or click-and-hold. Subsequently, I want the image to shr ...

How can I modify the custom CSS to adjust the color of a Font Awesome icon?

Hello everyone! I am new to coding and I have a question for the Stack Overflow community. I am trying to customize the color of my Font Awesome icons on the homepage of my WordPress theme (Arcade Pro) using the custom CSS editor. Can anyone provide me w ...

Choose or deselect images from a selection

I am currently working on a feature for an album creation tool where users can select photos from a pool of images and assign them to a specific folder. However, I'm facing difficulty in selecting individual photos and applying customized attributes t ...

Display 'Div 1' and hide 'Div 2' when clicked, then reveal 'Div 2' and hide 'Div 1' when a different button is clicked

I need help figuring out how to make two separate buttons work. One button should display 'Div 1' and hide 'Div 2', while the other button should show 'Div 2' and hide 'Div 1' when clicked. My knowledge of jquery an ...

What is the most efficient way to load a PHP page once the webpage has completely finished loading?

I'm relatively inexperienced when it comes to PHP and JQuery, so please bear with me if my knowledge is limited. Currently, I'm facing an issue where loading a small HTML table that contains information queried from game servers (like shown in t ...

Transmit data from the Windows Communication Foundation to JavaScript

Looking to invoke the WCF service through JavaScript, utilizing AJAX? Check out this resource: Calling WCF Services using jQuery Here's my query: Is there a method to retain some JavaScript-related data after making a request, and then transmit inf ...

Utilizing data attributes in E2E testing for optimal results

We have decided to transition from using tag and classname selectors to data attributes in Cypress for our E2E testing. This change is intended to make the selectors more robust and less prone to breaking. Specifically, Cypress recommends using data-cy, d ...

Creating a blur effect on an image tag when hovering using CSS and HTML

I am currently developing a Portal website for one of my classes, and the template provided by my professor utilizes the <picture> tag to adjust images depending on the browser's size. My goal is to add a blur effect and darken the image while d ...