Search code examples
htmlcssgallery

How to center image gallery?


I've used the following code to create an image gallery:http://www.w3schools.com/css/css_image_gallery.asp

It aligns it to the left. I have 9 images in the gallery. What I want to do is have 3 rows by 3 columns of images aligned in the center of the page with margins on both sides but I can't seem to figure it out.


Solution

  • try this . . you can change the width of the #container

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    #container{
    width: 500px; 
    margin: 0 auto;
    
    }
    div.img {
        margin: 5px;
        padding: 5px;
        border: 1px solid #0000ff;
        height: auto;
        width: auto;
        float: left;
        text-align: center;
    }   
    
    div.img img {
        display: inline;
        margin: 5px;
        border: 1px solid #ffffff;
    }
    
    div.img a:hover img {
        border: 1px solid #0000ff;
    }
    
    div.desc {
      text-align: center;
      font-weight: normal;
      width: 120px;
      margin: 5px;
    }
    </style>
    </head>
    <body>
    <div id="container">
    <div class="img">
     <a target="_blank" href="klematis_big.htm"><img src="klematis_small.jpg" alt="Klematis" width="110" height="90"></a>
     <div class="desc">Add a description of the image here</div>
    </div>
    <div class="img">
     <a target="_blank" href="klematis2_big.htm"><img src="klematis2_small.jpg" alt="Klematis" width="110" height="90"></a>
     <div class="desc">Add a description of the image here</div>
    </div>
    <div class="img">
     <a target="_blank" href="klematis3_big.htm"><img src="klematis3_small.jpg" alt="Klematis" width="110" height="90"></a>
     <div class="desc">Add a description of the image here</div>
    </div>
    <div class="img">
     <a target="_blank" href="klematis4_big.htm"><img src="klematis4_small.jpg" alt="Klematis" width="110" height="90"></a>
     <div class="desc">Add a description of the image here</div>
    </div>
    <div>
    </body>
    </html>