Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning multiple images horizontally in the center of a div

Tags:

I have a div and I want to align in the center of that div multiple images. All of the images have the same height and width of 16px. The problem is that I can either center them and have the extra space below but when I use the display:block to remove it, they are aligned to the left again. Here's my code:

div which I want to contain the images:

.cell{     position: relative;     float: left;     width: 300px;     min-height: 22px;     border-bottom: 1px solid #000;     border-right: 1px solid #000;      line-height: 22px;     font-family: Arial, Verdana;     font-size: 12px;     color: #000;     text-decoration: none;     text-align: center;      margin-bottom: 2px;     margin-right: 2px; } 

The above class has the properties needed in general. So I want to create a class for the img elements so that they can be aligned one next to each other and all together aligned horizontally.

Any working suggestions?! :)

like image 486
Dimitris Damilos Avatar asked Feb 24 '12 16:02

Dimitris Damilos


People also ask

How do I center multiple images in HTML?

"display:inline-block" on the IMG. And remove the float and position statements. Then "text-align:center" for the container div. I used a div as a fake img but it should work the same.

How do I horizontally align an image in a div?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.

How do I align a div in center horizontally?

To center a div horizontally on a page, simply set the width of the element and the margin property to auto. That way, the div will take up whatever width is specified in the CSS and the browser will ensure the remaining space is split equally between the two margins.


2 Answers

Floating a block level item will push it to the left or right. "display:inline-block" on the IMG. And remove the float and position statements. Then "text-align:center" for the container div.

http://jsfiddle.net/B6Jsy/

I used a div as a fake img but it should work the same.

like image 111
jmbertucci Avatar answered Sep 18 '22 14:09

jmbertucci


<div class="Image">FIRST</div> <div class="Image">SECOND</div> 

.ImageHolder{ text-align:center; }  .Image{ display:inline-block; } 
like image 29
molu2008 Avatar answered Sep 18 '22 14:09

molu2008