2

I am working on an image gallery and want have the image's container be completely centered on the page, but the images are left aligned.

This is my desired output:

example1

However, when I try to do a text-align: center on the container(id: gallery) I am getting the images displayed like this:

example 2

I tried following suit with a previous stack overflow question: CSS: Center block, but align contents to the left and wrap the images in another div then align it with display: inline-block; and text-align: left; but the images just seem to align left on the entire page:

example 3

What can I do to accomplish my desired output?

HTML

 <div id="gallery">
     <div id="images">
        <div class="container">
            <a href="images/gallery/image1.jpg" data-lightbox="mygallery">
                <img src="images/gallery/image1.jpg">           
            <div class="overlay">
                <img src="images/magnify.png">
            </div>
               </a>
        </div>
        <div class="container">
            <a href="images/gallery/image2.jpg" data-lightbox="mygallery">
            <img src="images/gallery/image2.jpg">
            <div class="overlay">
                <img src="images/magnify.png">
            </div>
            </a>
          </div>
        </div>
    </div>

CSS

#gallery{
    text-align: center;
}
#images{
    display: inline-block;
    text-align: left;
}
img{
    width: 300px;
    cursor: pointer;
}
.overlay  {
    position: absolute;
    right: 0;
    left: 0;
    cursor: pointer;
    visibility: hidden;
    color: transparent;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    transition: all ease-in .3s;
}
.overlay > img{
    height: 50%;
    width: 50%;
    top: 50%; 
    visibility: hidden;
    left: 50%;
    transform: translate(-50%,-50%);
    position: absolute;   
}
.overlay:hover > img{
    visibility: visible; 
}
.container {
    position: relative;
    display: inline-block;
    margin: 5px;
}
.container:hover .overlay  {
    visibility: visible;
    opacity: .6;
    background: black;
    color: white;   
}
Russ Wilkie
  • 133
  • 1
  • 7

4 Answers4

0

How about styling the image wrapper .images like

.images {
    width:80%;
    margin:0 auto;
    text-align:left;
}
Brainfeeder
  • 2,604
  • 2
  • 19
  • 37
0

Give your #gallery div a max-width, text-align: center, and margin:auto, then put your header in another div inside the #gallery, but outside the #images. Then put text-align: left on your #images div.

See example below:

#gallery {
  text-align: center;
  max-width: 420px;
  margin: auto;
}

img {
  width: 100px;
  cursor: pointer;
}

.container {
  display: inline-block;
}

#images {
  text-align: left
}
<div id="gallery">
  <div id="header">
    <h1>Header</h1>
  </div>
  <div id="images">
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=d42">
        <img src="http://thecatapi.com/api/images/get?id=d42">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=21o">
        <img src="http://thecatapi.com/api/images/get?id=21o">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=49e">
        <img src="http://thecatapi.com/api/images/get?id=49e">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=13v">
        <img src="http://thecatapi.com/api/images/get?id=13v">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=6e6">
        <img src="http://thecatapi.com/api/images/get?id=6e6">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=4bf">
        <img src="http://thecatapi.com/api/images/get?id=4bf">
      </a>
    </div>
  </div>
</div>
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102
0

this works

body{
            display: flex;
            flex-flow: row nowrap;  
            justify-content: center;
            align-content: center;
            align-items: center;
}
section{
height:400px;
width:400px;
background:grey;
}

img {
  margin:48px;
}
<section>
  <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
  <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
  <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
  <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
  <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
 
</section>
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
-2

HTML

<h2>HEADER</h2>
<div class="container">
  <img src=""/>
  <img src=""/>
  <img src=""/>
  <img src=""/>
  <img src=""/>
  <img src=""/>
</div>

CSS

h2 {
  text-align: center;
}
.container {
  float: left;
}

img {
  border: medium solid black;
  width: 200px;
  height: 350px;
  margin: 5% 2%;
}